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