~ubuntu-branches/ubuntu/oneiric/yacas/oneiric

« back to all changes in this revision

Viewing changes to src/stdfileio.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gopal Narayanan
  • Date: 2002-04-23 13:50:51 UTC
  • Revision ID: james.westby@ubuntu.com-20020423135051-bbd6ov4orr8eufmw
Tags: upstream-1.0.51
ImportĀ upstreamĀ versionĀ 1.0.51

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
#include "yacasprivate.h"
 
4
#include "stdfileio.h"
 
5
 
 
6
// For lack of better place to put it...
 
7
InputStatus::~InputStatus()
 
8
{
 
9
}
 
10
 
 
11
 
 
12
StdFileInput::StdFileInput(FILE* aFile,InputStatus& aStatus)
 
13
    : LispInput(aStatus)
 
14
{
 
15
    iFile = aFile;
 
16
}
 
17
StdFileInput::StdFileInput(LispLocalFile& aFile,InputStatus& aStatus)
 
18
    : LispInput(aStatus)
 
19
{
 
20
    iFile = aFile.iFile;
 
21
}
 
22
 
 
23
 
 
24
 
 
25
LispChar StdFileInput::Next()
 
26
{
 
27
    LispChar c;
 
28
    c=fgetc(iFile);
 
29
    if (c == '\n')
 
30
        iStatus.NextLine();
 
31
    return c;
 
32
}
 
33
 
 
34
LispChar StdFileInput::Peek() 
 
35
{
 
36
    LispChar c = fgetc(iFile);
 
37
    ungetc(c,iFile);
 
38
    return c;
 
39
}
 
40
 
 
41
void StdFileInput::Rewind()
 
42
{
 
43
    fseek(iFile,0,SEEK_SET);
 
44
}
 
45
 
 
46
LispBoolean StdFileInput::EndOfStream() 
 
47
{
 
48
    return feof(iFile);
 
49
}
 
50
 
 
51
LispCharPtr StdFileInput::StartPtr()
 
52
{
 
53
    LISPASSERT(0);
 
54
    return NULL;
 
55
}
 
56
LispInt StdFileInput::Position()
 
57
{
 
58
    LISPASSERT(0);
 
59
    return 0;
 
60
}
 
61
 
 
62
 
 
63
 
 
64
StdFileOutput::StdFileOutput(FILE* aFile) : iFile(aFile) { }
 
65
StdFileOutput::StdFileOutput(LispLocalFile& aFile) : iFile(aFile.iFile) { }
 
66
 
 
67
 
 
68
void StdFileOutput::PutChar(LispChar aChar)
 
69
{
 
70
    fputc(aChar, iFile);
 
71
}
 
72
 
 
73
 
 
74
 
 
75
 
 
76
 
 
77
 
 
78
 
 
79
CachedStdFileInput::~CachedStdFileInput()
 
80
{
 
81
    PlatFree(iBuffer);
 
82
}
 
83
 
 
84
CachedStdFileInput::CachedStdFileInput(LispLocalFile& aFile,InputStatus& aStatus) : StdFileInput(aFile,aStatus)
 
85
{
 
86
    // Get size of file
 
87
    fseek(iFile,0,SEEK_END);
 
88
    iNrBytes = ftell(iFile);
 
89
    fseek(iFile,0,SEEK_SET);
 
90
    // Read in the full buffer
 
91
    iBuffer = PlatAlloc(iNrBytes+1);
 
92
    Check(iBuffer!=NULL,KLispErrNotEnoughMemory);
 
93
    iCurrentPos = 0;
 
94
    fread(iBuffer,iNrBytes,1,iFile);
 
95
    iBuffer[iNrBytes] = '\0';
 
96
};
 
97
 
 
98
LispChar CachedStdFileInput::Next()
 
99
{
 
100
    LispChar c;
 
101
    LISPASSERT(iCurrentPos < iNrBytes);
 
102
    c = iBuffer[iCurrentPos++];
 
103
 
 
104
    if (c == '\n')
 
105
    {
 
106
        iStatus.NextLine();
 
107
    }
 
108
    return c;
 
109
}
 
110
 
 
111
LispChar CachedStdFileInput::Peek()
 
112
{
 
113
    LISPASSERT(iCurrentPos < iNrBytes);
 
114
    return iBuffer[iCurrentPos];
 
115
}
 
116
 
 
117
 
 
118
void CachedStdFileInput::Rewind()
 
119
{
 
120
        iCurrentPos = 0;
 
121
}
 
122
 
 
123
LispBoolean CachedStdFileInput::EndOfStream()
 
124
{
 
125
    return (iCurrentPos >= iNrBytes);
 
126
}
 
127
 
 
128
LispCharPtr CachedStdFileInput::StartPtr()
 
129
{
 
130
    return iBuffer;
 
131
}
 
132
LispInt CachedStdFileInput::Position()
 
133
{
 
134
    return iCurrentPos;
 
135
}
 
136
 
 
137
 
 
138
void InternalFindFile(LispCharPtr aFileName, InputDirectories& aInputDirectories,
 
139
                     LispCharPtr aFoundFile)
 
140
{
 
141
    strcpy(aFoundFile,aFileName);
 
142
    FILE* file = fopen(aFileName,"rb");
 
143
    LispInt i=0;
 
144
    while (file == NULL && i<aInputDirectories.NrItems())
 
145
    {
 
146
        strcpy(aFoundFile,aInputDirectories[i]->String());
 
147
        strcat(aFoundFile,aFileName);
 
148
        file = fopen(aFoundFile,"rb");
 
149
        i++;
 
150
    }
 
151
    if (file != NULL)
 
152
    {
 
153
        fclose(file);
 
154
    }
 
155
    else
 
156
    {
 
157
        aFoundFile[0] = '\0';
 
158
    }
 
159
}
 
160
 
 
161
LispLocalFile::LispLocalFile(LispEnvironment& aEnvironment,
 
162
                             LispCharPtr aFileName, LispBoolean aRead,
 
163
                             InputDirectories& aInputDirectories)
 
164
: iEnvironment(aEnvironment)
 
165
{
 
166
    if (aRead)
 
167
    {
 
168
        LispChar othername[1024];//TODO
 
169
        strcpy(othername,aFileName);
 
170
        iFile = fopen(aFileName,"rb");
 
171
        LispInt i=0;
 
172
        while (iFile == NULL && i<aInputDirectories.NrItems())
 
173
        {
 
174
            strcpy(othername,aInputDirectories[i]->String());
 
175
            strcat(othername,aFileName);
 
176
            iFile = fopen(othername,"rb");
 
177
            i++;
 
178
        }
 
179
    }
 
180
    else
 
181
        iFile = fopen(aFileName,"w");
 
182
 
 
183
    if (iFile == NULL)
 
184
        iOpened=0;
 
185
    else
 
186
        iOpened=1;
 
187
 
 
188
    SAFEPUSH(iEnvironment,*this);
 
189
}
 
190
 
 
191
//aRead is for opening in read mode (otherwise opened in write mode)
 
192
LispLocalFile::~LispLocalFile()
 
193
{
 
194
    SAFEPOP(iEnvironment);
 
195
    Delete();
 
196
}
 
197
 
 
198
void LispLocalFile::Delete()
 
199
{
 
200
    if (iFile)
 
201
        fclose(iFile);
 
202
    iFile = NULL;
 
203
}
 
204
 
 
205
 
 
206
 
 
207
 
 
208
CachedStdUserInput::CachedStdUserInput(InputStatus& aStatus) :
 
209
StdUserInput(aStatus)
 
210
{
 
211
//printf("CachedStdUserInput:construct\n");
 
212
    Rewind();
 
213
};
 
214
LispChar CachedStdUserInput::Next()
 
215
{
 
216
//printf("CachedStdUserInput:Next\n");
 
217
    LispChar c = Peek();
 
218
    iCurrentPos++;
 
219
    printf("%c",c);
 
220
    return c;
 
221
}
 
222
 
 
223
LispChar CachedStdUserInput::Peek()
 
224
{
 
225
    if (iCurrentPos == iBuffer.NrItems())
 
226
    {
 
227
        iBuffer.Append(fgetc(iFile));
 
228
    }
 
229
    return iBuffer[iCurrentPos];
 
230
}
 
231
 
 
232
LispBoolean CachedStdUserInput::EndOfStream()
 
233
{
 
234
    return LispFalse;
 
235
}
 
236
 
 
237
void CachedStdUserInput::Rewind()
 
238
{
 
239
    // Make sure there is a buffer to point to.
 
240
    iBuffer.GrowTo(10);
 
241
    iBuffer.SetNrItems(0);
 
242
    iCurrentPos=0;
 
243
}
 
244
 
 
245
LispCharPtr CachedStdUserInput::StartPtr()
 
246
{
 
247
    if (iBuffer.NrItems() == 0)
 
248
        Peek();
 
249
    return &iBuffer[0];
 
250
}
 
251
 
 
252
LispInt CachedStdUserInput::Position()
 
253
{
 
254
    return iCurrentPos;
 
255
}
 
256
 
 
257