SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef __SIMPLEFILE_H__ 00012 #define __SIMPLEFILE_H__ 00013 00014 #include <shogun/lib/memory.h> 00015 #include <shogun/io/SGIO.h> 00016 #include <shogun/base/SGObject.h> 00017 00018 #include <stdio.h> 00019 #include <string.h> 00020 #include <sys/mman.h> 00021 00022 namespace shogun 00023 { 00028 template <class T> class CSimpleFile : public CSGObject 00029 { 00030 public: 00032 CSimpleFile() :CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL) 00033 { 00034 SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n") 00035 00036 file=NULL; 00037 filename=get_strdup(""); 00038 status = false; 00039 00040 set_generic<T>(); 00041 } 00042 00049 CSimpleFile(char* fname, FILE* f) 00050 : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL) 00051 { 00052 file=f; 00053 filename=get_strdup(fname); 00054 status = (file!=NULL && filename!=NULL); 00055 } 00056 00057 virtual ~CSimpleFile() 00058 { 00059 SG_FREE(filename); 00060 free_line_buffer(); 00061 } 00062 00069 T* load(T* target, int64_t& num) 00070 { 00071 if (status) 00072 { 00073 status=false; 00074 00075 if (num==0) 00076 { 00077 bool seek_status=true; 00078 int64_t cur_pos=ftell(file); 00079 00080 if (cur_pos!=-1) 00081 { 00082 if (!fseek(file, 0, SEEK_END)) 00083 { 00084 if ((num=(int64_t) ftell(file)) != -1) 00085 { 00086 SG_INFO("file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T)) 00087 num/=sizeof(T); 00088 } 00089 else 00090 seek_status=false; 00091 } 00092 else 00093 seek_status=false; 00094 } 00095 00096 if ((fseek(file,cur_pos, SEEK_SET)) == -1) 00097 seek_status=false; 00098 00099 if (!seek_status) 00100 { 00101 SG_ERROR("filesize autodetection failed\n") 00102 num=0; 00103 return NULL; 00104 } 00105 } 00106 00107 if (num>0) 00108 { 00109 if (!target) 00110 target=SG_MALLOC(T, num); 00111 00112 if (target) 00113 { 00114 size_t num_read=fread((void*) target, sizeof(T), num, file); 00115 status=((int64_t) num_read == num); 00116 00117 if (!status) 00118 SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num) 00119 } 00120 else 00121 SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename) 00122 } 00123 return target; 00124 } 00125 else 00126 { 00127 num=-1; 00128 return NULL; 00129 } 00130 } 00131 00138 bool save(T* target, int64_t num) 00139 { 00140 if (status) 00141 { 00142 status=false; 00143 if (num>0) 00144 { 00145 if (!target) 00146 target=SG_MALLOC(T, num); 00147 00148 if (target) 00149 { 00150 status=(fwrite((void*) target, sizeof(T), num, file)== 00151 (size_t) num); 00152 } 00153 } 00154 } 00155 return status; 00156 } 00157 00163 void get_buffered_line(char* line, uint64_t len) 00164 { 00165 00166 /* 00167 if (!line_buffer) 00168 { 00169 line_buffer=SG_MALLOC(char, line_buffer_size); 00170 size_t num_read=fread((void*) target, sizeof(T), num, file); 00171 00172 if (target) 00173 { 00174 size_t num_read=fread((void*) target, sizeof(T), num, file); 00175 status=((int64_t) num_read == num); 00176 00177 if (!status) 00178 SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num) 00179 } 00180 else 00181 SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename) 00182 00183 */ 00184 } 00185 00187 void free_line_buffer() 00188 { 00189 SG_FREE(line_buffer); 00190 line_buffer=NULL; 00191 } 00192 00197 inline void set_line_buffer_size(int32_t bufsize) 00198 { 00199 if (bufsize<=0) 00200 bufsize=1024*1024; 00201 00202 free_line_buffer(); 00203 line_buffer_size=bufsize; 00204 } 00205 00210 inline bool is_ok() { return status; } 00211 00213 virtual const char* get_name() const { return "SimpleFile"; } 00214 00215 protected: 00217 FILE* file; 00219 bool status; 00221 char task; 00223 char* filename; 00224 00226 int32_t line_buffer_size; 00228 char* line_buffer; 00229 }; 00230 } 00231 #endif