Merge: doc: fixed some typos and other misc. corrections
[nit.git] / c_src / core__file._ffi.c
1 /*
2 Extern implementation of Nit module file
3 */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdint.h>
7 #include "core__file._ffi.h"
8 #ifdef ANDROID
9 #include <android/log.h>
10 #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, "Nit", __VA_ARGS__)
11 #else
12 #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)
13 #endif
14 #define Array_of_Int_length core__file___Array_of_Int_length
15 #define Array_of_Int__index core__file___Array_of_Int__index
16 #line 36 "../lib/core/file.nit"
17
18
19 #ifdef _WIN32
20 #include <windows.h>
21 #endif
22 long core__file___NativeFile_io_close___impl( FILE* self )
23 {
24 #line 1524 "../lib/core/file.nit"
25
26 return fclose(self); }
27 long core__file___NativeFile_set_buffering_type___impl( FILE* self, long buf_length, long mode )
28 {
29 #line 1544 "../lib/core/file.nit"
30
31
32 return setvbuf(self, NULL, (int)mode, buf_length);
33 }
34 long core__file___NativeFile_io_read___impl( FILE* self, char* buf, long len )
35 {
36 #line 1506 "../lib/core/file.nit"
37
38
39 return fread(buf, 1, len, self);
40 }
41 int core__file___NativeFile_ferror___impl( FILE* self )
42 {
43 #line 1536 "../lib/core/file.nit"
44
45 return ferror(self); }
46 long core__file___NativeFile_io_write___impl( FILE* self, char* buf, long from, long len )
47 {
48 #line 1510 "../lib/core/file.nit"
49
50
51 size_t res = fwrite(buf+from, 1, len, self);
52 #ifdef _WIN32
53 // Force flushing buffer because end of line does not trigger a flush
54 fflush(self);
55 #endif
56 return (long)res;
57 }
58 long core__file___Sys_buffer_mode_line___impl( Sys self )
59 {
60 #line 1592 "../lib/core/file.nit"
61
62 return _IONBF; }
63 int core__file___NativeFileStat_is_dir___impl( struct stat * self )
64 {
65 #line 1474 "../lib/core/file.nit"
66
67 return S_ISDIR(self->st_mode); }
68 int core__file___CString_file_exists___impl( char* self )
69 {
70 #line 1387 "../lib/core/file.nit"
71
72
73 #ifdef _WIN32
74 DWORD attribs = GetFileAttributesA(self);
75 return attribs != INVALID_FILE_ATTRIBUTES;
76 #else
77 FILE *hdl = fopen(self,"r");
78 if(hdl != NULL){
79 fclose(hdl);
80 }
81 return hdl != NULL;
82 #endif
83 }
84 struct stat * core__file___CString_file_stat___impl( char* self )
85 {
86 #line 1400 "../lib/core/file.nit"
87
88
89 struct stat buff;
90 if(stat(self, &buff) != -1) {
91 struct stat* stat_element;
92 stat_element = malloc(sizeof(struct stat));
93 return memcpy(stat_element, &buff, sizeof(struct stat));
94 }
95 return 0;
96 }
97 char* core__file___CString_file_realpath___impl( char* self )
98 {
99 #line 1440 "../lib/core/file.nit"
100
101
102 #ifdef _WIN32
103 DWORD len = GetFullPathName(self, 0, NULL, NULL);
104 char *buf = malloc(len+1); // FIXME don't leak memory
105 len = GetFullPathName(self, len+1, buf, NULL);
106 return buf;
107 #else
108 return realpath(self, NULL);
109 #endif
110 }
111 int core__file___CString_file_mkdir___impl( char* self, long mode )
112 {
113 #line 1424 "../lib/core/file.nit"
114
115
116 #ifdef _WIN32
117 return !mkdir(self);
118 #else
119 return !mkdir(self, mode);
120 #endif
121 }
122 char* core__file___NativeDir_readdir___impl( DIR* self )
123 {
124 #line 1569 "../lib/core/file.nit"
125
126
127 struct dirent *de;
128 de = readdir(self);
129 if (!de) return NULL;
130 return de->d_name;
131 }
132 void core__file___NativeDir_closedir___impl( DIR* self )
133 {
134 #line 1566 "../lib/core/file.nit"
135
136 closedir(self); }
137 FILE* core__file___new_NativeFile_io_open_read___impl( char* path )
138 {
139 #line 1548 "../lib/core/file.nit"
140
141 return fopen(path, "r"); }
142 FILE* core__file___new_NativeFile_io_open_write___impl( char* path )
143 {
144 #line 1550 "../lib/core/file.nit"
145
146 return fopen(path, "w"); }
147 FILE* core__file___new_NativeFile_native_stdin___impl( )
148 {
149 #line 1552 "../lib/core/file.nit"
150
151 return stdin; }
152 FILE* core__file___new_NativeFile_native_stdout___impl( )
153 {
154 #line 1554 "../lib/core/file.nit"
155
156 return stdout; }
157 FILE* core__file___new_NativeFile_native_stderr___impl( )
158 {
159 #line 1556 "../lib/core/file.nit"
160
161 return stderr; }
162 DIR* core__file___new_NativeDir_opendir___impl( char* path )
163 {
164 #line 1563 "../lib/core/file.nit"
165
166 return opendir(path); }
167 char* core__file___Sys_native_getcwd___impl( Sys self )
168 {
169 #line 1714 "../lib/core/file.nit"
170
171 return getcwd(NULL, 0); }