SimpleFile.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __SIMPLEFILE_H__
00012 #define __SIMPLEFILE_H__
00013
00014 #include "lib/io.h"
00015 #include "base/SGObject.h"
00016
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <sys/mman.h>
00020
00025 template <class T> class CSimpleFile : public CSGObject
00026 {
00027 public:
00034 CSimpleFile(char* fname, FILE* f)
00035 : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
00036 {
00037 file=f;
00038 filename=strdup(fname);
00039 status = (file!=NULL && filename!=NULL);
00040 }
00041
00042 virtual ~CSimpleFile()
00043 {
00044 free(filename);
00045 free_line_buffer();
00046 }
00047
00054 T* load(T* target, int64_t& num=0)
00055 {
00056 if (status)
00057 {
00058 status=false;
00059
00060 if (num==0)
00061 {
00062 bool seek_status=true;
00063 int64_t cur_pos=ftell(file);
00064
00065 if (cur_pos!=-1)
00066 {
00067 if (!fseek(file, 0, SEEK_END))
00068 {
00069 if ((num=(int)ftell(file)) != -1)
00070 {
00071 SG_INFO( "file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T));
00072 num/=sizeof(T);
00073 }
00074 else
00075 seek_status=false;
00076 }
00077 else
00078 seek_status=false;
00079 }
00080
00081 if ((fseek(file,cur_pos, SEEK_SET)) == -1)
00082 seek_status=false;
00083
00084 if (!seek_status)
00085 {
00086 SG_ERROR( "filesize autodetection failed\n");
00087 num=0;
00088 return NULL;
00089 }
00090 }
00091
00092 if (num>0)
00093 {
00094 if (!target)
00095 target=new T[num];
00096
00097 if (target)
00098 {
00099 size_t num_read=fread((void*) target, sizeof(T), num, file);
00100 status=((int64_t) num_read == num);
00101
00102 if (!status)
00103 SG_ERROR( "only %ld of %ld entries read. io error\n", (int64_t) num_read, num);
00104 }
00105 else
00106 SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename);
00107 }
00108 return target;
00109 }
00110 else
00111 {
00112 num=-1;
00113 return NULL;
00114 }
00115 }
00116
00123 bool save(T* target, int64_t num)
00124 {
00125 if (status)
00126 {
00127 status=false;
00128 if (num>0)
00129 {
00130 if (!target)
00131 target=new T[num];
00132
00133 if (target)
00134 {
00135 status=(fwrite((void*) target, sizeof(T), num, file)==
00136 (size_t) num);
00137 }
00138 }
00139 }
00140 return status;
00141 }
00142
00143 void get_buffered_line(char* line, uint64_t len)
00144 {
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 }
00165
00166 void free_line_buffer()
00167 {
00168 delete[] line_buffer;
00169 line_buffer=NULL;
00170 }
00171
00172 inline void set_line_buffer_size(int32_t bufsize)
00173 {
00174 if (bufsize<=0)
00175 bufsize=1024*1024;
00176
00177 free_line_buffer();
00178 line_buffer_size=bufsize;
00179 }
00180
00185 inline bool is_ok() { return status; }
00186
00188 inline virtual const char* get_name() const { return "SimpleFile"; }
00189
00190 protected:
00192 FILE* file;
00194 bool status;
00196 char task;
00198 char* filename;
00199
00200
00201 int32_t line_buffer_size;
00203 char* line_buffer;
00204 };
00205 #endif