adds 'nativeavailable()' and 'read(buf,offset,len)' methods into FileInputStream...
[IRC.git] / Robust / src / Runtime / file.c
1 #ifndef MULTICORE
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <unistd.h>
5 #endif
6 #include <sys/types.h>
7 #include "structdefs.h"
8 #include "mem.h"
9 #include "runtime.h"
10 #include "methodheaders.h"
11
12 #ifdef D___FileOutputStream______nativeWrite____I__AR_B_I_I
13 void CALL34(___FileOutputStream______nativeWrite____I__AR_B_I_I, int fd, int off, int len, int fd, struct ArrayObject * ___array___, int off, int len) {
14 #ifdef MULTICORE
15 #else
16   char * string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
17   int status=write(fd, &string[off], len);
18 #endif
19 }
20 #endif
21
22 #ifdef D___FileOutputStream______nativeClose____I
23 void CALL11(___FileOutputStream______nativeClose____I, int fd, int fd) {
24 #ifdef MULTICORE
25 #else
26   close(fd);
27 #endif
28 }
29 #endif
30
31 #ifdef D___FileOutputStream______nativeFlush____I
32 void CALL11(___FileOutputStream______nativeFlush____I, int fd, int fd) {
33   // not supported in RAW version
34 #ifdef MULTICORE
35 #else
36   fsync(fd);
37 #endif
38 }
39 #endif
40
41 #ifdef D___FileOutputStream______nativeOpen_____AR_B
42 int CALL01(___FileOutputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
43 #ifdef MULTICORE
44   return 0;
45 #else
46   int length=VAR(___filename___)->___length___;
47   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
48   int fd=open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
49   return fd;
50 #endif
51 }
52 #endif
53
54 #ifdef D___FileOutputStream______nativeAppend_____AR_B
55 int CALL01(___FileOutputStream______nativeAppend_____AR_B, struct ArrayObject * ___filename___) {
56 #ifdef MULTICORE
57   return 0;
58 #else
59   int length=VAR(___filename___)->___length___;
60   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
61   int fd=open(filename, O_WRONLY|O_CREAT|O_APPEND, S_IRWXU);
62   return fd;
63 #endif
64 }
65 #endif
66
67 #ifdef D___FileInputStream______nativeOpen_____AR_B
68 int CALL01(___FileInputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
69 #ifdef MULTICORE
70   return 0;
71 #else
72   int length=VAR(___filename___)->___length___;
73   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
74   int fd;
75   if ((fd=open(filename, O_RDONLY, 0)) < 0) {
76     printf(">>>\n");
77     perror("open failed");
78     printf("filename is %s\n", filename);
79     system("pwd");
80     printf("<<<\n");
81   }
82   return fd;
83 #endif
84 }
85 #endif
86
87 #ifdef D___FileInputStream______nativeClose____I
88 void CALL11(___FileInputStream______nativeClose____I, int fd, int fd) {
89 #ifdef MULTICORE
90 #else
91   close(fd);
92 #endif
93 }
94 #endif
95
96 #ifdef D___FileInputStream______nativeRead____I__AR_B_I
97 int CALL23(___FileInputStream______nativeRead____I__AR_B_I, int fd, int numBytes, int fd, struct ArrayObject * ___array___, int numBytes) {
98 #ifdef MULTICORE
99   return -1;
100 #else
101   int toread=VAR(___array___)->___length___;
102   char* string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
103   int status;
104
105   if (numBytes<toread)
106     toread=numBytes;
107
108   if ((status=read(fd, string, toread)) < 0) {
109     perror("");
110   }
111   return status;
112 #endif
113 }
114 #endif
115
116 #ifdef D___FileInputStream______nativePeek____I
117 int CALL11(___FileInputStream______nativePeek____I, int fd, int fd) {
118 #ifdef MULTICORE
119   return 0;
120 #else
121   int status;
122   char string[1];
123   status=read(fd, string, 1);
124
125   if( status <= 0 ) {
126     return status;
127   }
128   lseek(fd, -1, SEEK_CUR);
129   return string[0];
130 #endif
131 }
132 #endif
133
134 #ifdef D___File______nativeLength_____AR_B
135 long long CALL01(___File______nativeLength_____AR_B, struct ArrayObject * ___pathname___) {
136 #ifdef MULTICORE
137   return 0;
138 #else
139   int length=VAR(___pathname___)->___length___;
140   char* filename= (((char *)&VAR(___pathname___)->___length___)+sizeof(int));
141   struct stat st;
142   stat(filename, &st);
143   return st.st_size;
144 #endif
145 }
146 #endif
147
148 #ifdef D___FileInputStream______nativeAvailable____I
149 int CALL11(___FileInputStream______nativeAvailable____I, int fd, int fd) {
150 #ifdef MULTICORE
151   return 0;
152 #else
153   int avail;
154   int cur=lseek(fd, 0, SEEK_CUR);
155   int fsize = lseek(fd, 0, SEEK_END);
156   avail=fsize-cur;
157   return avail;
158 #endif
159 }
160 #endif