c_src : Regenerated c_src to prevent use of String contructors
[nit.git] / c_src / file_nit.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 * Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
5 * Copyright 2008 Jean-Sébastien Gélinas <calestar@gmail.com>
6 *
7 * This file is free software, which comes along with NIT. This software is
8 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
11 * is kept unaltered, and a notification of the changes is added.
12 * You are allowed to redistribute it and sell it, alone or is a part of
13 * another product.
14 */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <poll.h>
24
25 #include "file_nit.h"
26
27 #ifndef NONITCNI
28 /*
29 C implementation of file::String::files
30
31 Imported methods signatures:
32 HashSet new_HashSet( ) for hash_collection::HashSet::init
33 void HashSet_add( HashSet recv, Object item ) for hash_collection::HashSet::(abstract_collection::SimpleCollection::add)
34 String NativeString_to_s() for string::NativeString::to_s
35 int HashSet_is_a_Set( HashSet value ) to check if a HashSet[String] is a Set[String]
36 Set HashSet_as_Set( HashSet value ) to cast from HashSet[String] to Set[String]
37 */
38 Set String_files___impl( String recv )
39 {
40 char *dir_path;
41 DIR *dir;
42
43 dir_path = String_to_cstring( recv );
44 if ((dir = opendir(dir_path)) == NULL)
45 {
46 perror( dir_path );
47 exit( 1 );
48 }
49 else
50 {
51 HashSet results;
52 String file_name;
53 struct dirent *de;
54
55 results = new_HashSet();
56
57 while ( ( de = readdir( dir ) ) != NULL )
58 if ( strcmp( de->d_name, ".." ) != 0 &&
59 strcmp( de->d_name, "." ) != 0 )
60 {
61 file_name = NativeString_to_s( strdup( de->d_name ) );
62 HashSet_add( results, String_as_Object( file_name ) );
63 }
64
65 closedir( dir );
66 return HashSet_as_Set( results );
67 }
68 }
69 #endif
70
71 int string_NativeString_NativeString_file_exists_0(char *f){
72 FILE *hdl = fopen(f,"r");
73 if(hdl != NULL){
74 fclose(hdl);
75 }
76 return hdl != NULL;
77 }
78
79 void *to_nit_file_stat(struct stat* st){
80 struct stat* stat_element;
81 stat_element = malloc(sizeof(struct stat));
82 return memcpy(stat_element, st, sizeof(struct stat));
83 }
84
85 void *string_NativeString_NativeString_file_stat_0(char *f){
86 struct stat buff;
87 if(stat(f, &buff) != -1)
88 return to_nit_file_stat(&buff);
89 return 0;
90 }
91
92 void *file_NativeFile_NativeFile_file_stat_0(FILE *f){
93 struct stat buff;
94 if(fstat(fileno(f), &buff) != -1)
95 return to_nit_file_stat(&buff);
96 return 0;
97 }
98
99 extern int string_NativeString_NativeString_file_delete_0(char *f){
100 return (remove(f) == 0);
101 }
102
103 int file_stdin_poll_in_(void) {
104 struct pollfd fd = {0, POLLIN, 0};
105 int res = poll(&fd, 1, 0);
106 if (res == -1) {
107 perror("Error poll stdin");
108 exit(EXIT_FAILURE);
109 }
110 return res > 0;
111 }