SimpleFile.h

Go to the documentation of this file.
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/io/SGIO.h>
00015 #include <shogun/base/SGObject.h>
00016 
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <sys/mman.h>
00020 
00021 namespace shogun
00022 {
00027 template <class T> class CSimpleFile : public CSGObject
00028 {
00029     public:
00031         CSimpleFile() :CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
00032         {
00033             SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n");
00034 
00035             file=NULL;
00036             filename=strdup("");
00037             status = false;
00038         }
00039 
00046         CSimpleFile(char* fname, FILE* f)
00047         : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
00048         {
00049             file=f;
00050             filename=strdup(fname);
00051             status = (file!=NULL && filename!=NULL);
00052         }
00053 
00054         virtual ~CSimpleFile()
00055         {
00056             SG_FREE(filename);
00057             free_line_buffer();
00058         }
00059 
00066         T* load(T* target, int64_t& num)
00067         {
00068             if (status)
00069             {
00070                 status=false;
00071 
00072                 if (num==0)
00073                 {
00074                     bool seek_status=true;
00075                     int64_t cur_pos=ftell(file);
00076 
00077                     if (cur_pos!=-1)
00078                     {
00079                         if (!fseek(file, 0, SEEK_END))
00080                         {
00081                             if ((num=(int64_t) ftell(file)) != -1)
00082                             {
00083                                 SG_INFO( "file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T));
00084                                 num/=sizeof(T);
00085                             }
00086                             else
00087                                 seek_status=false;
00088                         }
00089                         else
00090                             seek_status=false;
00091                     }
00092 
00093                     if ((fseek(file,cur_pos, SEEK_SET)) == -1)
00094                         seek_status=false;
00095 
00096                     if (!seek_status)
00097                     {
00098                         SG_ERROR( "filesize autodetection failed\n");
00099                         num=0;
00100                         return NULL;
00101                     }
00102                 }
00103 
00104                 if (num>0)
00105                 {
00106                     if (!target)
00107                         target=SG_MALLOC(T, num);
00108 
00109                     if (target)
00110                     {
00111                         size_t num_read=fread((void*) target, sizeof(T), num, file);
00112                         status=((int64_t) num_read == num);
00113 
00114                         if (!status)
00115                             SG_ERROR( "only %ld of %ld entries read. io error\n", (int64_t) num_read, num);
00116                     }
00117                     else
00118                         SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename);
00119                 }
00120                 return target;
00121             }
00122             else 
00123             {
00124                 num=-1;
00125                 return NULL;
00126             }
00127         }
00128 
00135         bool save(T* target, int64_t num)
00136         {
00137             if (status)
00138             {
00139                 status=false;
00140                 if (num>0)
00141                 {
00142                     if (!target)
00143                         target=SG_MALLOC(T, num);
00144 
00145                     if (target)
00146                     {
00147                         status=(fwrite((void*) target, sizeof(T), num, file)==
00148                             (size_t) num);
00149                     }
00150                 }
00151             }
00152             return status;
00153         }
00154 
00160         void get_buffered_line(char* line, uint64_t len)
00161         {
00162 
00163             /*
00164             if (!line_buffer)
00165             {
00166                 line_buffer=SG_MALLOC(char, line_buffer_size);
00167                 size_t num_read=fread((void*) target, sizeof(T), num, file);
00168 
00169                     if (target)
00170                     {
00171                         size_t num_read=fread((void*) target, sizeof(T), num, file);
00172                         status=((int64_t) num_read == num);
00173 
00174                         if (!status)
00175                             SG_ERROR( "only %ld of %ld entries read. io error\n", (int64_t) num_read, num);
00176                     }
00177                     else
00178                         SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename);
00179 
00180                         */
00181         }
00182 
00184         void free_line_buffer()
00185         {
00186             SG_FREE(line_buffer);
00187             line_buffer=NULL;
00188         }
00189 
00194         inline void set_line_buffer_size(int32_t bufsize)
00195         {
00196             if (bufsize<=0)
00197                 bufsize=1024*1024;
00198 
00199             free_line_buffer();
00200             line_buffer_size=bufsize;
00201         }
00202 
00207         inline bool is_ok() { return status; }
00208 
00210         inline virtual const char* get_name() const { return "SimpleFile"; }
00211 
00212     protected:
00214         FILE* file;
00216         bool status;
00218         char task;
00220         char* filename;
00221 
00223         int32_t line_buffer_size;
00225         char* line_buffer;
00226 };
00227 }
00228 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation