|
|
1.1 root 1: #include <ctype.h>
2: #include <doscalls.h>
3: #include <dos.h>
4: #include <stdlib.h>
5: #include "ssedefs.h"
6:
7:
8:
9:
10: /*** readfile - reads file into filebuffer
11: *
12: * readfile reads a file using DOSREAD into a file buffer
13: * and then parses out lines and places them into segments
14: * via 'addline'.
15: *
16: * readfile (fhandle)
17: *
18: * ENTRY fhandle - handle to file being read
19: *
20: * EXIT rc - return code of readfile and addline
21: *
22: * readfile uses line-feeds('\n') as seperators between lines.
23: * Lines longer than LINESIZE are cut at LINESIZE and continued
24: * on the next line. All non printable characters execpt TABS are
25: * ignored in buildling the line.
26: *
27: * EFFECTS fbuffer - by reading the file into fbuffer
28: * bytesread - by setting it to the number of bytes read
29: * TotalLines - by incrementing it for every line
30: * LineTable - by adding new lines via 'addline'
31: * SegTable - by using free space via 'addline'
32: * and by allocating segments via 'alloseg'
33: * TotalSegs - by incrementing it via 'alloseg'
34: * memory - by allocating segments via 'alloseg'
35: */
36:
37: short readfile(fhandle)
38: unsigned short fhandle;
39: {
40: register unsigned short i, j;
41: unsigned char line[LINESIZE +1]; /* temp buffer for building line */
42: short rc; /* return code of DOS */
43:
44:
45: for (bytesread = 1, j = 0, rc = 0; (bytesread != 0) && (!rc); ) {
46:
47: rc = DOSREAD(fhandle, (char far *)fbuffer,
48: FBUFFSIZE, (unsigned far *)&bytesread);
49:
50: if (rc == 0) {
51: /* scan buffer for complete lines */
52: for(i =0; (i < bytesread) && (!rc); i++) {
53: line[j] = fbuffer[i];
54:
55: if (isprint(line[j])) {
56: if (j < (LINESIZE)) {
57: /* continued building line */
58: j++;
59: }
60: else {
61: /* line length greater than line size */
62: rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES));
63: ++TotalLines;
64: line[0] = line[j];
65: j = 1;
66: }
67: }
68: else if (line[j] == '\t') {
69: /* fill with spaces to next tab */
70: if ((j/TABSIZE +1) > MAXTABS) {
71: /* TAB makes line greater than LINSIZE */
72: for ( ; j < LINESIZE; j++)
73: line[j] = ' ';
74: rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES));
75: ++TotalLines;
76: j = 0;
77: }
78: do {
79: line[j] = ' ';
80: ++j;
81: } while ((j % TABSIZE) != 0);
82: }
83: else if (line[j] == '\n') {
84: /* a complete line */
85: rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES));
86: ++TotalLines;
87: j = 0;
88: }
89: }
90: if ( (bytesread < FBUFFSIZE) && (j >0) && (!rc) ) {
91: /* line left in buffer */
92: rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES));
93: ++TotalLines;
94: j = 0;
95: }
96: }
97: }
98: return(rc);
99: }
100:
101:
102:
103:
104: /*** savefile - saves file to disk
105: *
106: * savefile attempts to write the file to disk.
107: *
108: * savefile (fhandle)
109: *
110: * ENTRY fhandle - handle of file to be written.
111: *
112: * EXIT rc - return code of DOSWRITE and
113: * (byteswritten != bytes in buffer to write)
114: *
115: * savefile fills a buffer with complete lines until the buffer
116: * can not accept anoter complete line, then savefile calls
117: * DOSWRITE with the number of bytes in the buffer. If an
118: * error occurs durning the write or the number bytes in the
119: * buffer does not match the number of bytes written out then
120: * savefile aborts and returns an error code via rc.
121: *
122: * EFFECTS fbuffer - by filling it with 'lines' from the file
123: * the file - by writing out any changes made to it
124: */
125:
126: short savefile(fhandle)
127: unsigned short fhandle;
128: {
129: unsigned short byteswritten; /* number written to file - returned by DOS */
130: unsigned long npl; /* file pointer - not used */
131: unsigned short i, j, k; /* indexes */
132: unsigned long totalbytes; /* total bytes written to file */
133: short rc; /* return code of DOS call */
134:
135: DOSCHGFILEPTR(fhandle, 0l, 0, &npl); /* move file ptr to start of file */
136:
137: totalbytes = rc = 0;
138:
139: /* place lines in fbuffer */
140: for (i = 0, j = 0; (i < TotalLines) && (!rc); i++) {
141: if ((LineTable[i]->linelength) < (FBUFFSIZE - j)) {
142: for (k = 0; k < LineTable[i]->linelength; k++, j++)
143: fbuffer[j] = LineTable[i]->firstchar[k];
144: fbuffer[j] = '\r';
145: j++;
146: fbuffer[j] = '\n';
147: j++;
148: }
149: else {
150: /* fbuffer is full - write to file */
151: rc = DOSWRITE(fhandle, (char far *)fbuffer, j,
152: (unsigned far *)&byteswritten);
153: rc = ( !(j == byteswritten) || rc);
154: totalbytes += (byteswritten);
155: j = 0;
156: }
157: }
158: if ((j > 0) && (!rc)) {
159: /* lines left in fbuffer - write out to file */
160: rc = DOSWRITE(fhandle, (char far *)fbuffer, j,
161: (unsigned far *)&byteswritten);
162: rc = ( !(j == byteswritten) || rc);
163: totalbytes += (byteswritten);
164: }
165:
166: if (!rc) /* no problems writing to the file */
167: /* change file size to current file size */
168: DOSNEWSIZE(fhandle, totalbytes);
169: return(rc);
170: }
171:
172:
173:
174:
175: /*** backupfile - make of the file
176: *
177: * backupfile creates a backup file for the file being edited
178: * by sse.
179: *
180: * backupfile (fname, fhandle)
181: *
182: * ENTRY fhandle - handle to file to backup
183: * fname - path name of file to backup
184: *
185: * EXIT rc - return code of backup
186: *
187: * backupfile creates a backup file by replacing the current
188: * extension with .BAK or adding the extension.BAK if none is
189: * given. If the file being edited has the extension .BAK then
190: * backupfile will not create a backup.
191: *
192: * EFFECTS backup file - by creating it
193: */
194:
195: short backupfile(fname, fhandle)
196: char *fname;
197: unsigned short fhandle;
198: {
199: unsigned short bfhandle; /* handle to backup file */
200: unsigned short bytesread; /* number of bytes read from file fname */
201: unsigned short byteswriten; /* number of bytes written to file bfname */
202: unsigned char i, j; /* indexs */
203: unsigned long npl; /* file pointer - not used */
204: short rc; /* return code of backupfile */
205: char bfname[65]; /* pathname of the backup file */
206:
207: /* build back up file name - bfname, from fname */
208: for (i = 0; ((bfname[i] = fname[i]) != '\0'); i++);
209: for (j = 1;
210: (bfname[i - j] != '\\') && (bfname[i - j] != '.')
211: && (j < 4) && (j < i);
212: j++);
213: if ((bfname[i - j] == '.') && ((i - j) != 0))
214: i -= j;
215: if (bfname[i] != '.')
216: bfname[i] = '.';
217: bfname[++i] = 'B';
218: bfname[++i] = 'A';
219: bfname[++i] = 'K';
220: bfname[++i] = '\0';
221:
222:
223: /* create backup file */
224: rc = openfile(bfname, &bfhandle, WCFLAG);
225: if (!rc) {
226: DOSCHGFILEPTR(fhandle, 0l, 0, &npl); /* file ptr to beginning of fname */
227: for (bytesread = 1, byteswriten = 1;
228: (bytesread != 0) && !(rc = (bytesread != byteswriten)); ) {
229: /* copy fname to bfname */
230: DOSREAD(fhandle, (char far *)fbuffer, FBUFFSIZE,
231: (unsigned far *)&bytesread);
232: DOSWRITE(bfhandle, (char far *)fbuffer, bytesread,
233: (unsigned far *)&byteswriten);
234: }
235: closefile(bfhandle);
236: if (rc)
237: /* delete backup file if there was an error creating it */
238: DOSDELETE( (char far *)bfname, 0l);
239: }
240: return(rc);
241: }
242:
243:
244:
245:
246: /*** openfile - opens file to be read
247: *
248: * openfile uses DOSOPEN to open or create a file and
249: * return a handle to it.
250: *
251: * openfile (fname, fhandle, openflag)
252: *
253: * ENTRY fname - name of the file
254: openflag - is the value of openflag for DOSOPEN
255: *
256: * EXIT fhandle - file handle
257: * rc - return code of openfile
258: *
259: * openfile has the openmode for DOSOPEN set so it may run as
260: * a bound application. The openflag controls the action
261: * of the open, example: if the file exist then open it
262: * or replace it, or if the file does not exist create it
263: * or fail.
264: *
265: * EFFECTS fhandle - by returning a handle to the file
266: * memory - by opening a file
267: */
268:
269: short openfile(fname, fhandle, openflag)
270: char *fname;
271: unsigned short *fhandle;
272: unsigned short openflag;
273: {
274: unsigned short actiontaken = 0; /* value return by DOS */
275: unsigned long fsize = 0l; /* set to not change file size (0) */
276: unsigned short fattrib = 0; /* set to not change file attrib (0) */
277: struct flags {
278: unsigned access :3 ; /* set to read & write access (2) */
279: unsigned reserved1 :1 ; /* must be zero */
280: unsigned sharing :3 ; /* set to deny write access (2) */
281: unsigned inheritance:1 ; /* set to private (1) */
282: unsigned reserved5 :5 ; /* must be zero */
283: unsigned failerrors :1 ; /* needes to be zero for API */
284: unsigned write :1 ; /* needes to be zero for API */
285: unsigned dasdopen :1 ; /* needes to be zero for API */
286: };
287: static union {
288: struct flags modeflags;
289: unsigned short mode;
290: } openmode = { { 2,0,2,1,0,0,0,0 } };
291: short rc; /* return code from DOS call */
292:
293:
294: /* Open the file */
295: rc = DOSOPEN( (char far *)fname, (unsigned far *)fhandle,
296: (unsigned far *)&actiontaken,
297: fsize, fattrib, openflag,
298: openmode.mode, 0l);
299: return(rc);
300: }
301:
302:
303:
304:
305: /*** closefile - closes file
306: *
307: * closefile uses DOSCLOSE to close the file given by
308: * fhandle.
309: *
310: * closefile (fhandle).
311: *
312: * ENTRY fhandle - handle of file to be closed
313: *
314: * closefile does not return an error code if one occurs.
315: *
316: * EFFECTS memory - by closing a file
317: * the file - by closing it
318: */
319:
320: void closefile(fhandle)
321: unsigned short fhandle;
322: {
323: short rc; /* return code of DOS call */
324:
325: /* Close the file */
326: rc = DOSCLOSE(fhandle);
327: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.