Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

stringfile.C

Go to the documentation of this file.
00001 #include "stringfile.h"
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 StringFile::StringFile(long length)
00007 {
00008         pointer = 0;
00009         if(length == 0)
00010         {
00011                 this->length = 100000;
00012         }
00013         else
00014         {
00015                 this->length = length;
00016         }
00017         string = new char[this->length + 1];
00018         available = this->length;
00019 }
00020 
00021 StringFile::StringFile(char *filename)
00022 {
00023         FILE *in;
00024         if(in = fopen(filename, "rb"))
00025         {
00026                 fseek(in, 0, SEEK_END);
00027                 length = ftell(in);
00028                 available = length;
00029                 fseek(in, 0, SEEK_SET);
00030                 string = new char[length + 5];
00031 
00032                 fread(string, length, 1, in);
00033                 for(int i = 0; i < 5; i++) string[length + i] = 0;
00034                 fclose(in);
00035         }
00036         else
00037         {
00038                 //printf("File not found: %s\n", filename);
00039                 length = 0;
00040                 available = 1;
00041                 string = new char[1];
00042                 string[0] = 0;
00043         }
00044         
00045         pointer = 0;
00046 }
00047 
00048 StringFile::~StringFile()
00049 {
00050         delete [] string;
00051 }
00052 
00053 int StringFile::write_to_file(char *filename)
00054 {
00055         FILE *out;
00056         if(out = fopen(filename, "wb"))
00057         {
00058                 fwrite(string, pointer, 1, out);
00059         }
00060         else
00061         {
00062 //              printf("Couldn't open %s for writing.\n", filename);
00063                 return 1;
00064         }
00065         fclose(out);
00066         return 0;
00067 }
00068 
00069 int StringFile::read_from_string(char *string)
00070 {
00071         int i;
00072         
00073         delete [] this->string;
00074         length = strlen(string);
00075         available = length;
00076         this->string = new char[length + 5];
00077         strcpy(this->string, string);
00078         for(i = 0; i < 5; i++) this->string[length + i] = 0;
00079         return 0;
00080 }
00081 
00082 long StringFile::get_length()
00083 {
00084         return strlen(string);
00085 }
00086 
00087 long StringFile::get_pointer()
00088 {
00089         return pointer;
00090 }
00091 
00092 int StringFile::readline(char *arg2)
00093 {       
00094         readline(string1, arg2);
00095         return 0;
00096 }
00097 
00098 int StringFile::readline()
00099 {       
00100         readline(string1, string1);
00101         return 0;
00102 }
00103 
00104 int StringFile::readline(float *arg2)
00105 {
00106         readline(string1, arg2);
00107         return 0;
00108 }
00109 
00110 int StringFile::readline(int *arg2)
00111 {
00112         readline(string1, arg2);
00113         return 0;
00114 }
00115 
00116 int StringFile::readline(long *arg2)
00117 {
00118         readline(string1, arg2);
00119         return 0;
00120 }
00121 
00122 int StringFile::readline(Freq *arg2)
00123 {
00124         readline(string1, &(arg2->freq));
00125         return 0;
00126 }
00127 
00128 int StringFile::readline(char *arg1, char *arg2)
00129 {
00130         int i, len, max;
00131         len = 0; max = 1024;
00132         
00133         while(string[pointer] == ' ') pointer++; // skip indent
00134         arg1[0] = 0;    arg2[0] = 0;
00135         
00136         for(i = 0; string[pointer] != ' ' && string[pointer] != '\n' && len < max; i++, pointer++)
00137         {     // get title
00138                 arg1[i] = string[pointer];
00139                 len++;
00140         }
00141         arg1[i] = 0;
00142 
00143         if(string[pointer] != '\n')
00144         {       // get value
00145                 pointer++;      // skip space
00146                 for(i = 0; string[pointer] != '\n' && len < max; i++, pointer++)
00147                 {
00148                         arg2[i] = string[pointer];
00149                         len++;
00150                 }
00151                 arg2[i] = 0;
00152         }
00153         pointer++;      // skip eoln
00154         return 0;
00155 }
00156 
00157 int StringFile::backupline()
00158 {
00159         while(string[pointer] != 10 && pointer > 0)
00160         {
00161                 pointer--;     // first eoln
00162         }
00163         if(string[pointer] == 10) pointer--;        // skip eoln
00164         
00165         while(string[pointer] != 10 && pointer > 0)
00166         {
00167                 pointer--;     // second eoln
00168         }
00169         
00170         if(string[pointer] == 10) pointer++;      // skip eoln
00171         return 0;
00172 }
00173 
00174 int StringFile::readline(char *arg1, long *arg2)
00175 {
00176         readline(arg1, string1);
00177         *arg2 = atol(string1);
00178         return 0;
00179 }
00180 
00181 int StringFile::readline(char *arg1, int *arg2)
00182 {
00183         long arg;
00184         readline(arg1, &arg);
00185         *arg2 = (int)arg;
00186         return 0;
00187 }
00188 
00189 int StringFile::readline(char *arg1, float *arg2)
00190 {
00191         readline(arg1, string1);
00192         *arg2 = atof(string1);
00193         return 0;
00194 }
00195 
00196 int StringFile::writeline(char *arg1, int indent)
00197 {
00198         int i;
00199         
00200 // reallocate the string
00201         if(pointer + strlen(arg1) > available)
00202         {
00203                 char *newstring = new char[available * 2];
00204                 strcpy(newstring, string);
00205                 delete string;
00206                 available *= 2;
00207                 length *= 2;
00208                 string = newstring;
00209         }
00210         
00211         for(i = 0; i < indent; i++, pointer++) string[pointer] = ' ';
00212         sprintf(&string[pointer], arg1);
00213         pointer += strlen(arg1);
00214         return 0;
00215 }
00216 
00217 int StringFile::writeline(char *arg1, char *arg2, int indent)
00218 {
00219         int i;
00220         
00221         sprintf(string1, "%s %s\n", arg1, arg2);
00222         writeline(string1, indent);
00223         return 0;
00224 }
00225 
00226 int StringFile::writeline(char *arg1, long arg2, int indent)
00227 {
00228         sprintf(string1, "%s %ld\n", arg1, arg2);
00229         writeline(string1, indent);
00230         return 0;
00231 }
00232 
00233 int StringFile::writeline(char *arg1, int arg2, int indent)
00234 {
00235         sprintf(string1, "%s %d\n", arg1, arg2);
00236         writeline(string1, indent);
00237         return 0;
00238 }
00239 
00240 int StringFile::writeline(char *arg1, float arg2, int indent)
00241 {
00242         sprintf(string1, "%s %f\n", arg1, arg2);
00243         writeline(string1, indent);
00244         return 0;
00245 }
00246 
00247 int StringFile::writeline(char *arg1, Freq arg2, int indent)
00248 {       
00249         sprintf(string1, "%s %d\n", arg1, arg2.freq);
00250         writeline(string1, indent);
00251         return 0;
00252 }

Generated on Sun Jan 8 13:26:35 2006 for Guicast-svn by  doxygen 1.4.4