#include #include #include /* NOTE: FIO.H was developed in MS-DOS 6.22, and may or may not function smoothly under Windows emulated DOS */ /* ==================== STREAMLINE I/O TOOLS start here ================= */ /* ---------------- SETSIZE.C --------------- */ /* _setsize.c creates a new file of size bytes, ready for modification syntax: setsize(handle, file size bytes) */ /* #include main() { long size=1024; FILE *f=fopen("hram.9", "w"); setsize(f, size); } */ setsize(FILE *f, long size) { long i; for(i=1; i<=size; i++){ fprintf(f, "`"); } fclose(f); } /* ---------------- WRITEn.C --------------- */ /* overwites starting from nth byte of the file ` f for file handle ` n for the nth byte from which writing will begin ie. the first byte of what we want to write will replace whatever occupied the nth byte previously, and so on. ` overwrite_with_what is what we want to write in */ /* #include main() { long n=10; char *overwrite_with_what; FILE *f=fopen("hram.0", "r+"); *overwrite_with_what='T'; *(overwrite_with_what+1)='V'; *(overwrite_with_what+2)='\0'; writeN(f, n, overwrite_with_what); fclose(f); } */ writeN(FILE *f, long n, char *overwrite_with_what) { fseek(f, n-1, 0); fprintf(f, "%s", overwrite_with_what); } /* ----- COUNT ----- Counts how many time the single character c appears in file f */ /* main() { FILE *f=fopen("hram.0", "r+"); long total=-1; char c='['; total=count(f, c); printf(" \n %ld %c were found.", total, c); fclose(f); } */ count(FILE *f, char c){ char pp=''; long i=0; fseek(f, 0, 0); while( pp != EOF ) { pp=getc(f); if( pp == c ) i++; } return(i); } /* -----------(1) READnm ---------------*/ /*1. read from nth byte to mth byte -- readnm */ /* #include main() { long n=5; long m=10; char *answer; FILE *f=fopen("hram.1", "r+"); readnm(f, n, m, answer); printf("\n ANSWER = %c", *(answer+1)); } */ readnm(FILE *f, long n, long m, char *answer){ long j; fseek(f, n-1, 0); /* from 0 start of file, 1 current, 2 eof */ for(j=0; j<=m-n+1; j++){ *(answer+j)=getc(f); } *(answer+j+1)='\0'; } /* -----------(2) READnk ------------- */ /*2. read k bytes starting from nth byte -- readnk*/ /* #include main() { long n=5; long k=10; char *answer; FILE *f=fopen("hram.1", "r+"); readnk(f, n, k, answer); printf("\n ANSWER = %c", *(answer+9)); } */ readnk(FILE *f, long n, long k, char *answer){ long j; fseek(f, n-1, 0); /* from 0 start of file, 1 current, 2 eof */ for(j=0; j<=k-1; j++){ *(answer+j)=getc(f); } *(answer+k)='\0'; } /* -----(3) READnc ---------*/ /*3. read from nth byte until a specific character -- readnc*/ /* #include main() { long n=5; char *answer; char c=')'; FILE *f=fopen("hram.1", "r+"); readnc(f, n, c, answer); printf("\n ANSWER = %c", *(answer+4)); } */ readnc(FILE *f, long n, char c, char *answer){ long j=0; char pp=''; fseek(f, n-1, 0); /* from 0 start of file, 1 current, 2 eof */ while(pp!=c){ pp=getc(f); *(answer+j)=pp; j++; } *(answer+j-1)='\0'; } /* --(4) READnabm ------ */ /*4. read the content between character A and character B in the range of*/ /* nth byte to mth byte -- readnabm*/ /* #include main() { long n=2; long m=15; char a='('; char b=')'; char *answer; FILE *f=fopen("hram.1", "r+"); readnabm(f, n, a, b, m, answer); printf("\n ANSWER = %c", *(answer+4)); } */ readnabm(FILE *f, long n, char a, char b, long m, char *answer){ long j=0; char pp=''; long l=0; fseek(f, n-1, 0); /* from 0 start of file, 1 current, 2 eof */ while(pp!=a){ pp=getc(f); printf("\n%c",pp); j++;} while(pp!=b && j<=m-n){ pp=getc(f); *(answer+l)=pp; l++; printf("\n%c",pp); j++; } *(answer+l-1)='\0'; } /* -------(5) READnextK ------- */ /*5. read k bytes from current pointer position onwards -- readnextk*/ /* #include main() { long k=2; char *answer; FILE *f=fopen("hram.1", "r+"); readnextk(f, k, answer); printf("\n ANSWER = %c", *(answer)); } */ readnextk(FILE *f, long k, char *answer){ long j; for(j=0; j<=k-1; j++){ *(answer+j)=getc(f); } *(answer+k)='\0'; } /* ----(6) READNEXTab -------- */ /*6. read the content between character A and character B in the range of from*/ /* current pointer position to kth byte hereonwards -- readnextkab*/ /*#include main() { char a='('; char b=')'; char *answer; FILE *f=fopen("hram.1", "r+"); readnextab(f, a, b, answer); printf("\n ANSWER = %c", *(answer+3)); }*/ readnextab(FILE *f, char a, char b, char *answer){ long j=0; char pp=''; long l=0; while(pp!=a){ pp=getc(f); j++;} while(pp!=b){ pp=getc(f); *(answer+l)=pp; l++; } *(answer+l-1)='\0'; } /* ---(7) READNEXTkAB ------- */ /*7. read the content between next characters A and B -- readnextab */ /*#include main() { long k=20; char a='('; char b=')'; char *answer; FILE *f=fopen("hram.1", "r+"); readnextkab(f, k, a, b, answer); printf("\n ANSWER = %c", *(answer+7)); } */ readnextkab(FILE *f, long k, char a, char b, char *answer){ long j=0; char pp=''; long l=0; while(pp!=a && j<=k){ pp=getc(f); j++;} while(pp!=b && j+l<=k){ pp=getc(f); *(answer+l)=pp; l++; } *(answer+l-1)='\0'; } /* -------------- (8) READcK -------------- */ /* Description: Dump everything from current file pointer position until c character, into an array (excluding the c character), and then move the file pointer k bytes further forward. Read on from current pointer position until character c is reached. Everything from starting pointer position up to the character c (excluding the c character) will be ported into an array. Before this function quits, it will move the pointer k bytes forward (to skip over carriage return for example, in preparation for next call). Pre-requisite: Everytime we call this function, our pointer must be on where we want our output array to begin. This function only makes sure to stop at character c and skip over next k bytes; it does not care where we start recording from. Application: One exmaple of when to use this function is when we are dealing with dir/b output in MS-DOS. For example, we obtained a list of files using dir/b > FILENAME, but we want each of these filename passed back to us in an array or by a pointer without the extension filename and the full stop. This function becomes handy. We set the terminal mark trap to the full stop so that our function knows to break and wrap up everytime it comes to a full stop; we also, set k to 4 in order to skip the three bytes of extension filename plus one byte of carriage return. There is one thing we need to look out for in this particular application, which is that all three bytes of the extension filenames must be present in every file. Assume that one of these files had an extension filename of say, "me" (for example in the case of a file with file name "read.me") - in this situation there are only two bytes of extension filename instead of three, and ofcourse readck() will not work properly from this point onwards. On the other hand we can equally well trap a carriage return instead of the full stop, to capture entire filenames and without the byte problem just mentioned. In this case we would set k to 0, since now there wouldn't be anything we need to skip after every carriage return. Bug: "Null pointer assignment" sometimes crops up at the end. I think it has to do with *extron but not too sure what exactly is mucking up. It's got to be due to some fundamental concept I'm misunderstanding. */ /* #include main() { FILE *f = fopen("FILENAME", "r+"); */ /* char intron='.'; */ /* ending mark, separator */ /* char *extron='t'; */ /* reply from readck */ /* long k=4; */ /* to skip the next k bytes after the ending mark */ /* long lengthVerify=0; */ /* set initial value 1 to get the game started */ /* long a=0;*/ /* use for loop instead of while to decide how many files to access */ /* for(a=0; a<20; a++){*/ /* while(lengthVerify != -1){ lengthVerify = readck(f, intron, k, extron); printf("%s [Length = %ld]\n", extron, lengthVerify); } */ /* end of for */ /* fclose(f); } */ /* end of main*/ readck(FILE *f, char intron, long k, char *extron){ long i=0; /*Counter for skip k*/ long j=0; /*Position locater for ex. Also keeps track of array length*/ char ppp=''; /* to fill up ex until character in is reached */ while( (ppp = getc(f)) != intron && ppp != EOF) { *(extron+j) = ppp; j++; /* printf("%c", ppp);*/ } *(extron+j)='\0'; /* wrap it up with an array termination code */ for(i=0; i<=k; i++) getc(f); /* skip k bytes as requested */ if(ppp == EOF) j=-1; /* if end of file, set return length to -1 in order to notify caller */ return(j); /* returns array length excluding the termination code */ } /* end of readck */ /* ==================== STREAMLINE I/O TOOLS end here =================== */ /* ===================== RECORDS I/O TOOLS start here ================= */ /* --------- SetRec -----------*/ /* SetRec creates a table of no_of_records records, each record has a gross length of raw_single_record_length. Each record ends in the character(s) end_char. There will be therefore in effect, these many nett bytes available for the user to use: raw_single_record_length - bytes used up by end_char. The no_of_records ofcourse can be extended later using writeRec if required. If we want to use a carriage return as the end_char, we need to declare this: char end_char[2]="\x0A\0"; Each carriage return takes up 2 bytes instead of just one, so a 30 bytes long record will now have only 28 effective bytes left. I don't understand why \0D keeps coming in and stuffs up the total file length, but anyway the problem has been worked around using v--; I suspect it's DOS that added it for me. */ /* #include main() { char end_char[2]="\x0A\0"; long raw_single_record_length=30; long no_of_records=100; long effective_length; FILE *f=fopen("hram.0", "w+"); effective_length=setRec(f, raw_single_record_length, no_of_records, end_char); printf("\n Effective length = %ld bytes (out of %ld total bytes per record)", effective_length, raw_single_record_length); fclose(f); } */ setRec(FILE *f, long raw_single_record_length, long no_of_records, char *end_char) { long i, u, v=0; while( *(end_char+v)!='\0' ){ v++; } if(*end_char=='\x0A') v++; for(i=1; i<=no_of_records; i++){ for(u=1; u<=raw_single_record_length-v; u++){ fprintf(f, "`"); } fprintf(f, "%s", end_char); } return(raw_single_record_length-v); } /* --------- WriteRecNk --------- ` f for file handle ` raw_single_record_length is the total length of a single record including end character length ` effective_length is raw take away endchar length ` record_no, eg. if we want to change the content of nth record, record_no will be equal to n ` what_to_write is ofcourse what we want to write into that record. It cannot be longer than the effective_length. In the case when it is shorter than the effective_length, the left over space will be filled by ' Return values: -1 if record we want to write is longer than effective_length. Successful operation will return the number of bytes of what was written in. Note: WriteRecNk is faster than WriteRecNc because the latter needs to calculate the length of end_character(s) before it is able to obtain effective_length. In the case of WriteRecNk, the value of effective_length is fed and readily usable, hence requires less processing time. */ /* #include main() { long written=0; long raw_single_record_length=30; long effective_length=28; long record_no=9; char what_to_write[21]="[k](1111.22,3333.44)\0"; FILE *f=fopen("hram.0", "r+"); written=writeRecNk(f, raw_single_record_length, effective_length, record_no, what_to_write); printf("\n %ld bytes written into %ldth record", written, record_no); fclose(f); } */ writeRecNk(FILE *f, long raw_single_record_length, long effective_length, long record_no, char *what_to_write){ long i, u, w=0; long length_of_what_to_write=0; /* Calculate the length of what we want to write */ while( *(what_to_write+w)!='\0' ){ w++; } length_of_what_to_write=w; /* What we want to write in is longer than effective_length can allow, abort operation*/ if(length_of_what_to_write > effective_length) { return(-1); } /* Normal condition. Proceed with the writing process*/ else { fseek(f, (record_no-1)*raw_single_record_length, 0); fprintf(f, "%s", what_to_write); for(i=1; i<=effective_length-length_of_what_to_write; i++){fprintf(f,"`");} return(length_of_what_to_write); } /*end of else*/ } /*end of writeRecNk*/ /* -----WriteRecNc ----- ` f for file handle ` raw_single_record_length is the total length of a single record including end character length ` end_char marks the end of each record ` record_no, eg. if we want to change the content of nth record, record_no will be equal to n ` what_to_write is ofcourse what we want to write into that record. It cannot be longer than the effective_length. In the case when it is shorter than the effective_length, the left over space will be filled by ` */ /* #include main() { long raw_single_record_length=30; char end_char[2]="\x0A\0"; long record_no=5; char what_to_write[26]="[k](1111.22,3333.44)"; FILE *f=fopen("hram.0", "r+"); writeRecNc(f, raw_single_record_length, end_char, record_no, what_to_write); fclose(f); } */ writeRecNc(FILE *f, long raw_single_record_length, char *end_char, long record_no, char *what_to_write){ long i, u, v=0, w=0; long length_of_what_to_write=0, effective_length=0; /* making sure to proceed only if what we want to write in is not longer than the length of our record */ while( *(end_char+v)!='\0' ){ v++; } if(*end_char=='\x0A') {v++;} effective_length=raw_single_record_length-v; while( *(what_to_write+w)!='\0' ){ w++; } length_of_what_to_write=w; /* What we want to write in is too long, abort operation*/ if(length_of_what_to_write > effective_length) { return(-1); } /* Normal condition. Proceed with the writing process*/ else { fseek(f, (record_no-1)*raw_single_record_length, 0); fprintf(f, "%s", what_to_write); for(i=1; i<=effective_length-length_of_what_to_write; i++){fprintf(f,"`");} return(length_of_what_to_write); } /*end of else*/ } /*end of writeRecNc*/ /* ----- ReadRecNk ---- ReadRecNk stands for Reading k bytes starting from the first byte of the Nth record ` f for file handle ` record_no for the nth record we want to read ` raw_single_record_length is the total length of each record including endcharacters ` effective_length is the actual usable length of a record. ie. raw_single_record_length take away the bytes occupied by endcharacter(s). Ofcourse if we wish to read only say 3 bytes, we may well assign a value of 3 to this variable, instead of assigning the real effective length. ` pocket will contain the output, ie. content of nth record */ /* #include main() { long b; char *pocket; long effective_length=28; long record_no=2; long raw_single_record_length=30; FILE *f=fopen("hram.0", "r+"); for(b=0;b<=raw_single_record_length;b++){ *(pocket+b)='~'; } *(pocket+b+1)='\0'; readRecNk(f, record_no, raw_single_record_length, effective_length, pocket); printf("\n %ldth record = %s", record_no, pocket); fclose(f); } */ readRecNk(FILE *f, long record_no, long raw_single_record_length, long effective_length, char *pocket) { long r; fseek(f, (record_no-1)*raw_single_record_length, 0); for(r=0; r<=effective_length; r++){ *(pocket+r) = getc(f); } *(pocket+r-1)='\0'; return(r); } /*end of readrecNk*/ /* ----- ReadRecNc ----- */ /* ReadRecNc stands for Reading from the first byte of Nth record until the specified endcharacter(s) is/are met ` f for file handle ` record_no for the nth record we want to read ` raw_single_record_length is the total length of each record including endcharacters ` end_char signals the end of each record. Eg. for endcharacter of 123, we declare: char end_char[4]='123\0'; For endcharacter of a carriage return, we declare: char end_char[2]='\x0A\0'; ` pocket will contain the output, ie. content of nth record */ /* #include main() { long b; char *pocket; char end_char[2]="\x0A\0"; long record_no=2; long raw_single_record_length=30; FILE *f=fopen("hram.0", "r+"); for(b=0;b<=raw_single_record_length;b++){ *(pocket+b)='~'; } *(pocket+b+1)='\0'; readRecNc(f, record_no, raw_single_record_length, end_char, pocket); printf("\n %ldth record = %s", record_no, pocket); fclose(f); } */ readRecNc(FILE *f, long record_no, long raw_single_record_length, char *end_char, char *pocket) { long i=0, end_char_length=0; long r=0; int result=0; /* count how many bytes in the end_char */ i=0; while( *(end_char+i) != '\0' ) { i++; } end_char_length=i; fseek(f, (record_no-1)*raw_single_record_length, 0); while( result!=1 ) { *(pocket+r) = getc(f); if(*(pocket+r) == *end_char){result=testEnd(f, end_char, end_char_length);} r++; } *(pocket+r-1)='\0'; } /*end of readrecNc*/ /* The mechanism goes: every time the first character of end_char is met in readRecN function, testEnd is called upon. testEnd will look ahead however bytes the endcharacter(s) is/are and see if these character(s) will be coming next or not. If yes, obviously the end of record has been reached; If not, it just happened that some of the stuff picked up resembled but do not match entirely to the endcharacter, in this case, the end of the record has not yet been reached.*/ testEnd(FILE *f, char *end_char, long end_char_length){ long j=0, k=0; int result=0; long current_position=ftell(f); for(j=1; j<=end_char_length; j++){ if( *(end_char+j) == getc(f)){k++;} } if(k==end_char_length-1) { result = 1; } else { fseek(f, current_position, 0); } return(result); } /*end of testEnd*/ /* ----- ReadRecNsk ---- ReadRecNsk stands for Reading k bytes starting from the sth byte of the Nth record (sth byte being the first byte read) ` f for file handle ` record_no for the nth record we want to read ` raw_single_record_length is the total length of each record including endcharacters ` pocket will contain the output, ie. content of nth record Note: suspicious of bug(s) in this one */ /* #include main() { long b; char *pocket; long record_no=2, s=3, k=4; long raw_single_record_length=30; FILE *f=fopen("hram.0", "r+"); for(b=0;b<=raw_single_record_length;b++){ *(pocket+b)='~'; } *(pocket+b+1)='\0'; readRecNsk(f, raw_single_record_length, record_no, s, k, pocket); printf("\n %ldth record = %s", record_no, pocket); fclose(f); } */ readRecNsk(FILE *f, long raw_single_record_length, long record_no, long s, long k, char *pocket) { long r; fseek(f, (record_no-1)*raw_single_record_length+s-1, 0); for(r=0; r<=k; r++){ *(pocket+r) = getc(f); } *(pocket+r-1)='\0'; return(r); } /*end of readrecNsk*/ /* ----- Strip ----- Receives a pointer to a string, strip() will return the pointer for the string between the two specified characters. ` a for the starting string ` [ and ] for the two specified characters ` b for the content between these two specified characters in the starting string a. */ /* main() { char a[20]="[1](12345,67890)\0"; char *b="```````````````````\0"; strip(a, '[', ']', b); printf("\n Stripped result = %s", b); } /* strip(char *a, char l, char r, char *b) { long i=0, j=0; while( *(a+i) != l ) { i++; } i++; while( *(a+i) != r ) { *(b+j)=*(a+i); i++; j++; } *(b+j)='\0'; } /* ----- F2C ----- */ /* F2C converts double float into a string. ` value is the double float number ` no_of_decimals is the number of decimal points desired ` c is the character pointer for the result string */ /* #include main() { double value=99999.8888; int no_of_decimals=4; long string_length=-1; char *c="````````````````````````````````````````\0"; string_length=f2c(value, no_of_decimals, c); printf("\nDouble float %f -> String %s (%ld bytes; %d decimal points)", value,c,string_length, no_of_decimals); } */ f2c(double value, int ndig, char *c) { int i,j; int *dec=0; int *sign=0; int ddd=0; char *raw_result="`````````````````````````````````````````````\0"; raw_result=fcvt(value, ndig, dec, sign); ddd=*dec; for(i=0; i<=ddd-1; i++){ *(c+i)= *(raw_result+i);} j=i; *(c+i)='.'; i++; while( *(raw_result+j) != '\0' ) { *(c+i)=*(raw_result+j); i++; j++;} *(c+i)='\0'; return(i); } /* ----- FUSE ----- fuseCC(string pointer 1, string pointer 2, receiver pointer); fuseCF(string pointer, double float, number of decimals, reciever pointer); fuseFC(double float, number of decimals, string pointer, receiver pointer); Return value is the number of bytes of received content */ /* #include main() { char *e1="TWO\0"; char *e2="DOGS\0"; double value=999.8888; int no_of_decimals=4; long response=-1; char *fusedCC="```````````````````````\0"; char *fusedCF="```````````````````````\0"; char *fusedFC="```````````````````````\0"; response=fuseCC(e1, e2, fusedCC); response=fuseCF(e1, value, no_of_decimals, fusedCF); response=fuseFC(value, no_of_decimals, e1, fusedFC); printf("\n FusedCF = %s (%ld bytes)", fusedCF, response); } */ fuseCC(char *e1, char *e2, char *fused){ long i=0, j=0; while( *(e1+i) != '\0' ) { *(fused+i) = *(e1+i); i++;} while( *(e2+j) != '\0' ) { *(fused+i) = *(e2+j); i++; j++;} *(fused+i)='\0'; return(i); } fuseCF(char *e1, double value, int no_of_decimals, char *fused){ long string_length=-1; long pre_response=-1; char *pre_fused="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\0"; string_length=f2c(value, no_of_decimals, pre_fused); pre_response=fuseCC(e1, pre_fused, fused); return(pre_response); } fuseFC(double value, int no_of_decimals, char *e1, char *fused){ long string_length=-1; long pre_response=-1; char *pre_fused="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\0"; string_length=f2c(value, no_of_decimals, pre_fused); pre_response=fuseCC(pre_fused, e1, fused); return(pre_response); } /* ----- Pick2 ----- Picks up two records at a time. p1 and p2 will be made to point to r1th and r2th records respectively. Return value is the total number of bytes picked up, ie. length of record r1 plus length of record r2. User may wish to employ a pair of independent for-loops in Main, to achieve the effect of nP2, or use two dependent loops for nC2. */ /* main() { long raw_single_record_length=30; long r1=2; long r2=5; char *p1="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\0"; char *p2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\0"; FILE *f=fopen("hram.0", "r+"); Pick2(f, raw_single_record_length, r1, r2, p1, p2); printf(" \n record 1 = %s", p1); printf(" \n record 2 = %s", p2); fclose(f); } */ Pick2(FILE *f, long raw_single_record_length, long r1, long r2, char *p1, char *p2) { long i, j=0; char pp=''; fseek(f, (r1-1)*raw_single_record_length, 0); for(i=0; i<= raw_single_record_length-1; i++) { *(p1+i)=getc(f); } *(p1+i)='\0'; fseek(f, (r2-1)*raw_single_record_length, 0); for(j=0; j<= raw_single_record_length-1; j++) { *(p2+j)=getc(f); } *(p2+j)='\0'; return(i+j); }