N
Nelu
I would appreciate some comments on the code below for reading a line of
text from a stream.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/**
* Reads a line of text from a stream and returns the string on
* success and NULL on failure.
* fstream - the stream to read from
* maxRead - a variable that specifies the maximum number of
* characters to read. If maxRead is NULL then SIZE_MAX-1 is
* considered as the maximum value. If maxRead is different from NULL
* then the function will set the value to the length of non-NULL
* string returned by the function.
* fullLine - a variable that specifies whether or not the end of line
* has been reached (a complete line was read). It also returns true if
EOF
* has been reached. It can be set to NULL.
*/
char *readStringWithLimit(FILE *fstream, size_t *maxRead,
int *fullLine) {
size_t capacityIncrement=256;
size_t capacity=capacityIncrement;
size_t readLimit;
size_t stringLength=0;
char *myStr, *tmpStr;
int localFullLine;
unsigned char c;
int ic;
if(maxRead==NULL) {
readLimit=SIZE_MAX-1;
} else {
if((*maxRead)>SIZE_MAX-1) {
readLimit=SIZE_MAX-1;
} else {
readLimit=*maxRead-1;
}
}
myStr=malloc(capacity);
if(!myStr) {
return NULL;
}
while(1) {
ic=fgetc(fstream);
if(ic==EOF) {
if(ferror(fstream)) {
free(myStr);
return NULL;
} else {
localFullLine=1;
break;
}
} else {
c=(unsigned char)ic;
if(c=='\n') {
localFullLine=1;
break;
} else {
if(readLimit>=stringLength) {
if(stringLength==capacity) {
//calculate the necessary size
if(SIZE_MAX-capacityIncrement>capacity) {
capacity+=capacityIncrement;
} else {
capacity=SIZE_MAX;
}
tmpStr=realloc(myStr,capacity);
if(tmpStr) {
myStr=tmpStr;
} else {
free(myStr);
return NULL;
}
}
myStr[stringLength++]=c;
} else {
ungetc(ic,fstream);
localFullLine=0;
break;
}
}
}
}
if(stringLength==capacity) {
tmpStr=realloc(myStr,capacity+1);
if(tmpStr) {
myStr=tmpStr;
} else {
free(myStr);
return NULL;
}
}
myStr[stringLength]='\0';
if(maxRead) {
*maxRead=stringLength;
}
if(fullLine) {
*fullLine=localFullLine;
}
return myStr;
}
Thank you.
text from a stream.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/**
* Reads a line of text from a stream and returns the string on
* success and NULL on failure.
* fstream - the stream to read from
* maxRead - a variable that specifies the maximum number of
* characters to read. If maxRead is NULL then SIZE_MAX-1 is
* considered as the maximum value. If maxRead is different from NULL
* then the function will set the value to the length of non-NULL
* string returned by the function.
* fullLine - a variable that specifies whether or not the end of line
* has been reached (a complete line was read). It also returns true if
EOF
* has been reached. It can be set to NULL.
*/
char *readStringWithLimit(FILE *fstream, size_t *maxRead,
int *fullLine) {
size_t capacityIncrement=256;
size_t capacity=capacityIncrement;
size_t readLimit;
size_t stringLength=0;
char *myStr, *tmpStr;
int localFullLine;
unsigned char c;
int ic;
if(maxRead==NULL) {
readLimit=SIZE_MAX-1;
} else {
if((*maxRead)>SIZE_MAX-1) {
readLimit=SIZE_MAX-1;
} else {
readLimit=*maxRead-1;
}
}
myStr=malloc(capacity);
if(!myStr) {
return NULL;
}
while(1) {
ic=fgetc(fstream);
if(ic==EOF) {
if(ferror(fstream)) {
free(myStr);
return NULL;
} else {
localFullLine=1;
break;
}
} else {
c=(unsigned char)ic;
if(c=='\n') {
localFullLine=1;
break;
} else {
if(readLimit>=stringLength) {
if(stringLength==capacity) {
//calculate the necessary size
if(SIZE_MAX-capacityIncrement>capacity) {
capacity+=capacityIncrement;
} else {
capacity=SIZE_MAX;
}
tmpStr=realloc(myStr,capacity);
if(tmpStr) {
myStr=tmpStr;
} else {
free(myStr);
return NULL;
}
}
myStr[stringLength++]=c;
} else {
ungetc(ic,fstream);
localFullLine=0;
break;
}
}
}
}
if(stringLength==capacity) {
tmpStr=realloc(myStr,capacity+1);
if(tmpStr) {
myStr=tmpStr;
} else {
free(myStr);
return NULL;
}
}
myStr[stringLength]='\0';
if(maxRead) {
*maxRead=stringLength;
}
if(fullLine) {
*fullLine=localFullLine;
}
return myStr;
}
Thank you.