diff --git a/src/output.cpp b/src/output.cpp --- a/src/output.cpp +++ b/src/output.cpp @@ -41,11 +41,11 @@ using namespace MyWarning; #define FNAMESIZE 100 int OpenFileAndCheckExistance(FILE **fp,const char *fname,char *ftype) { - + *fp=fopen(fname,ftype); if (*fp==NULL) return FALSE; - + if (!strncmp(ftype,"a",1)) { if (ftell(*fp)>0L) return TRUE; else return FALSE; @@ -53,49 +53,46 @@ int OpenFileAndCheckExistance(FILE **fp, } int FileExistsP(const char *fname) { - + FILE *fp; fp=fopen(fname,"r"); if (fp==NULL) return FALSE; - + fclose(fp); return TRUE; +} -} - int YesNoP(const char *message) { - + char answer[100]; fprintf(stderr,"%s (y/n) ",message); fflush(stderr); - + scanf("%s",answer); while (strcmp(answer,"y") && strcmp(answer,"n")) { fprintf(stderr,"\n\bPlease answer 'y' or 'n'. "); fflush(stderr); scanf("%s",answer); } - + if (!strcmp(answer,"y")) return TRUE; - + return FALSE; - } FILE *OpenWriteFile(const char *filename) { - char fname[FNAMESIZE]; FILE *fp; fprintf(stderr,"Opening %s for writing\n",filename); - + if(FileExistsP(filename)==TRUE) { - + if (false/*par.interactive*/) { char *message=(char *)malloc(2000*sizeof(char)); if (!YesNoP("File exists, overwrite?")) { @@ -108,12 +105,11 @@ FILE *OpenWriteFile(const char *filename /* Rename old file */ sprintf(fname, "%s~",filename); rename(filename, fname); - } } - + strncpy(fname, filename, FNAMESIZE-1); - + if ((fp=fopen(fname,"w"))==NULL) { char *message=(char *)malloc(2000*sizeof(char)); sprintf(message," Could not open file %s for writing: " @@ -121,7 +117,6 @@ FILE *OpenWriteFile(const char *filename perror(""); throw(message); } - return fp; } @@ -131,7 +126,7 @@ FILE *OpenReadFile(const char *filename) FILE *fp; fprintf(stderr,"Opening %s for reading\n",filename); - + if((OpenFileAndCheckExistance(&fp,filename,"r"))==FALSE) { char *message=(char *)malloc(2000*sizeof(char)); sprintf(message," File %s not found or empty, exiting... \n" ,filename); @@ -144,15 +139,15 @@ FILE *OpenReadFile(const char *filename) char *ReadLine(FILE *fp) { /* does almost the same as fgetln(), but DEC Unix doesn't understand - fgetln(). Also I want my function to return a real C string, - terminated by a \0. */ + fgetln(). Also I want my function to return a real C string, + terminated by a \0. */ /* The function reads a line from file *fp, and returns a pointer to the - line read, which can be freed with a normal free(). The length of the - string is written in *len */ - + line read, which can be freed with a normal free(). The length of the + string is written in *len */ + #define INITIAL_BUFSIZE 100 - + char *tmpstring; int character; long bufsize; @@ -160,62 +155,54 @@ char *ReadLine(FILE *fp) int pos; CheckFile(fp); - + /* first allocate a string with a standard length */ bufsize=INITIAL_BUFSIZE; MEMORYCHECK(tmpstring=(char *)malloc(bufsize*sizeof(char))); pos=0; - + while ((character=getc(fp))!=EOF && /* read a character and check */ - character!='\n') { - - - tmpstring[pos]=(char)character; - (pos)++; + character!='\n') { - if (pos >= bufsize) { - /* line is longer than initial_bufsize, reallocate space */ - bufsize+=INITIAL_BUFSIZE; - MEMORYCHECK(tmpstring=(char *)realloc(tmpstring,bufsize*sizeof(char))); - - } - + tmpstring[pos]=(char)character; + (pos)++; + + if (pos >= bufsize) { + /* line is longer than initial_bufsize, reallocate space */ + bufsize+=INITIAL_BUFSIZE; + MEMORYCHECK(tmpstring=(char *)realloc(tmpstring,bufsize*sizeof(char))); + } } - if (character==EOF) { - - if (pos==0) { - /* EOF was reached, while no characters were read */ - free(tmpstring); - return NULL; - } - if (ferror(fp)) { - error("I/O error in ReadLine(%ld): %s\n",fp, strerror(errno)); - } - + if (pos==0) { + /* EOF was reached, while no characters were read */ + free(tmpstring); + return NULL; + } + if (ferror(fp)) { + error("I/O error in ReadLine(%ld): %s\n",fp, strerror(errno)); + } + } - } - /* Allocate enough memory for the line */ MEMORYCHECK(line=(char *)malloc((++(pos))*sizeof(char))); strncpy(line,tmpstring,(pos)-1); free(tmpstring); - + line[pos-1]='\0'; return line; - } void CheckFile(FILE *fp) { if (ftell(fp)<0) { - /* file is probably not open, or another error occured */ - error("File error (fp=%ld): %d %s\n",fp,errno,strerror(errno)); + /* file is probably not open, or another error occured */ + error("File error (fp=%ld): %d %s\n",fp,errno,strerror(errno)); } /* File pointer is ok */ } @@ -223,12 +210,12 @@ void CheckFile(FILE *fp) char *Chext(char *filename) { /* Chop the extension from a filename */ - + /* Search backwards until a dot */ - + /* Remember to free the memory allocated by this function */ /* ( free(result) ) */ - + /* not yet tested */ int i; @@ -237,32 +224,29 @@ char *Chext(char *filename) { for (i=strlen(filename)-1;i>=0;i--) { if (filename[i]=='.') break; - } - + /* No . found */ if (i==0) { - + result=strdup(filename); } else { - + /* . found */ result=(char *)malloc((i+1)*sizeof(char)); strncpy(result, filename, i); } return result; - - } void MakeDir(const char *dirname) { #ifdef QTGRAPHICS QFileInfo file_info(dirname); - + //check for existance if (file_info.exists()) { - + if (file_info.isDir()) { // OK cerr << "Using existing directory " << dirname << " for data storage." << endl; @@ -273,7 +257,6 @@ void MakeDir(const char *dirname) { cerr << message << endl; throw(message); } - } // make directory @@ -286,23 +269,23 @@ void MakeDir(const char *dirname) { //exit(1); return; } - + // check - + #else - + int status; char message[MESS_BUF_SIZE]; - + status=mkdir(dirname, S_IRWXU); // S_IRWXU: Read, Write, Execute by Owner - + if (status<0) { // error occurred - + //check for existance - + if (errno==EEXIST) { - + // Check whether it is a directory struct stat buf; stat(dirname, &buf); @@ -325,17 +308,15 @@ void MakeDir(const char *dirname) { } } else { // a different error occurred. Print error and quit - + snprintf(message,MESS_BUF_SIZE,"Error in making directory %s",dirname); perror(message); exit(1); - } - - - } #endif } + +/* finis */