compiler/libec: (#341, #351, #644, #771) Improved enum type matching and type handlin...
[sdk] / ecere / src / sys / TempFile.ec
1 namespace sys;
2
3 import "System"
4
5 public class TempFile : File
6 {
7    byte * buffer;
8    uint size;
9    uint position;
10    bool eof;
11    FileOpenMode openMode;
12    uint allocated;
13
14    openMode = writeRead;
15
16    ~TempFile()
17    {
18       delete buffer;
19    }
20
21    int Read(byte * buffer, uint size, uint count)
22    {
23       int readSize = size * count;
24       int read = Min(readSize, this.size - position);
25
26       if(position >= this.size) eof = true;
27       if(buffer) memcpy(buffer, this.buffer + position, read);
28
29       position += read;
30
31       return read / size;
32    }
33
34    int Write(byte * buffer, uint size, uint count)
35    {
36       int writeSize = size * count;
37       int written = writeSize;
38
39       if(this.size - position < writeSize)
40       {
41          this.size += writeSize - (this.size - position);
42          if(this.allocated < this.size)
43          {
44             this.allocated *= 2;
45             if(this.allocated < this.size)
46                this.allocated = this.size * 2;
47             this.buffer = renew this.buffer byte[this.allocated];
48          }
49       }
50       memcpy(this.buffer + position, buffer, writeSize);
51
52       position += written;
53
54       return written / size;
55    }
56
57    bool Getc(char * ch)
58    {
59       int read = Read(ch, 1, 1);
60       return !eof && read != 0;
61    }
62
63    bool Putc(char ch)
64    {
65       int written = Write(&ch, 1, 1);
66       return written != 0;
67    }
68
69    bool Puts(char * string)
70    {
71       int len = string ? strlen(string) : 0;
72       int written = Write(string, 1, len);
73       return written == len;
74    }
75
76    bool Seek(int pos, FileSeekMode mode)
77    {
78       bool result = true;
79       uint increase = 0;
80       switch(mode)
81       {
82          case start:
83          {
84             if(pos >= size)
85             {
86                if(openMode == readWrite)
87                {
88                   position = pos;
89                   increase = pos - size;
90                }
91                else
92                {
93                   position = size;
94                   result = false;
95                }
96             }
97             else if(pos < 0)
98             {
99                position = 0;
100                result = false;
101             }
102             else
103                position = pos;
104             break;
105          }
106          case current:
107          {
108             if(position + pos >= size)
109             {
110                if(openMode == readWrite)
111                {
112                   position += pos;
113                   increase = position - size;
114                }
115                else
116                {
117                   position = size;
118                   result = false;
119                }
120             }
121             else if(position + pos < 0)
122             {
123                position = 0;
124                result = false;
125             }
126             else
127                position += pos;
128             break;
129          }
130          case end:
131          {
132             if((int)size + pos >= (int)size)
133             {
134                if(openMode == readWrite)
135                {
136                   position = size + pos;
137                   increase = position - size;
138                }
139                else
140                {
141                   position = size;
142                   result = false;
143                }
144             }
145             else if((int)size + pos < 0)
146             {
147                position = 0;
148                result = false;
149             }
150             else
151                position = size + pos;
152             break;
153          }
154       }
155       if(result)
156          eof = false;
157
158       if(increase)
159       {
160          this.size += increase;
161          this.buffer = renew this.buffer byte[this.size];
162       }
163       return result;
164    }
165
166    uint Tell()
167    {
168       return position;
169    }
170
171    bool Eof()
172    {
173       return eof;
174    }
175
176    uint GetSize()
177    {
178       return size;
179    }
180
181    bool Truncate(FileSize size)
182    {
183       buffer = renew buffer byte[size];
184       this.size = size;
185       this.allocated = size;
186       if(position > size) position = size;      // What to do here?
187       return true;
188    }
189
190 public:
191    property FileOpenMode openMode
192    {
193       set { openMode = value; }
194       get { return openMode; }
195    }
196    property byte * buffer { get { return buffer; } };
197 };