/* /////////////////////////////////////////////////////////////// // // // lines.c - makes a line of chars // // ver 1.1 // // // // Language: Borland C, ver 3.0 // // Platform: Gateway 386/25, win95.400 // // Application: CSE784 demonstration // // Author: Jim Fawcett, 123 Link Hall, Syracuse Univ. // // (315) 443-2654, jfawcett@cat.syr.edu // // // /////////////////////////////////////////////////////////////// */ #include /* FILE */ #include /* malloc() */ #include /* strlen() */ /*----< global parameter >------------ private --------------*/ static int BLOCKSIZE = 256; /*----< make a copy of string >-------------------------------- Processing: - allocates memory for a line and copies input string into it - user is expected to free line when done with it Inputs: - pointer to string, e.g., a null terminated array of chars Outputs: - returns a pointer to copy of string on heap */ char *makeLine(char *s) { char *ptr, *head; ptr = head = (char*)malloc(strlen(s)+1); if(!ptr) return ptr; while(s) *ptr++ = *s++; return head; } /* */ /*----< make a block of chars on heap >------------------------ Processing: - attempts to allocate size bytes of memory from heap - fills block with null terminators - user is expected to free line when done with it Inputs: - size is the number of bytes requested Outputs: - returns a pointer to allocated memory if successful, else returns 0 */ char *getBlock(int size) { char *block = (char*)malloc(size); int i; if(block) for(i=0; i--------- private --------- Processing: - allocates size bytes of memory from heap - copies s into new memory - does not free old allocation pointed to by s - if size is smaller than strlen(s)+1 s will be truncated and properly null terminated Inputs: - s is a null terminated string of arbitrary size - size is the amount of heap memory requested Outputs: - returns a pointer to newly allocated memory with copy of s */ static char *resize(char *s, int size) { char *temp; int i; #ifdef TEST_MODULE2 fprintf(stdout, "\nreallocating memory to size %d\n", size); #endif temp = getBlock(size*sizeof(char)); if(!temp) return temp; for(i=0; i--------------------------------- Processing: - allocates memory for line of chars - copies first available line from file, terminated by either a newline or EOF - user is expected to free line when done with it Inputs: - pointer to an open file stream, can be stdin Outputs: - returns pointer to null terminated line of chars if not at end of file, otherwise returns 0 - does not include the line terminating newline or EOF */ char *getLine(FILE *fptr) { int ch, j, i = 0; char *temp; char *line = (char*)malloc(BLOCKSIZE*sizeof(char)); if(!line) { fprintf(stdout, "memory alloc failure in getLine()\n"); return line; } while((ch = fgetc(fptr)) != '\n') { if(i == BLOCKSIZE-10) { temp = resize(line, BLOCKSIZE *= 2); free(line); line = temp; } if(ch == EOF) break; else if(ch == '\f') ch = '\n'; /* strip formfeeds */ else if(ch == '\t') { int count = 8 - (i%8); /* tabsize = 8 */ for(j=0; j------------------------------------------*/ #ifdef TEST_LINES void main(int argc, char *argv[]) { FILE *fptr; char *line; /* test resizing of memory by using very small BLOCKSIZE */ BLOCKSIZE = 4; /* user's don't need to (and can't) set BLOCKSIZE */ fprintf(stdout,"\n\n Testing Module2\n ===============\n"); line = makeLine(argv[argc-1]); fprintf(stdout, "\n%s %s\n", "last arg on command line is:", line); free(line); fprintf(stdout, "\nplease enter an arbitrary line of text: "); line = getLine(stdin); fprintf(stdout, "\n%s %s\n", "input from program user is: ", line); free(line); fprintf(stdout, "\nplease enter name of a file of text: "); line = getLine(stdin); fptr = fopen(line,"r"); free(line); if(!fptr) fprintf(stdout, "can't open file %s\n",line); line = getLine(fptr); fprintf(stdout, "\n%s %s\n", "input from file is: ", line); free(line); } #endif