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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
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 
00021 template <class T> class CSimpleFile : public CSGObject
00022 {
00023     public:
00030         CSimpleFile(CHAR* fname, FILE* f)
00031         : CSGObject()
00032         {
00033             file=f;
00034             filename=strdup(fname);
00035             status = (file!=NULL && filename!=NULL);
00036         }
00037 
00038         ~CSimpleFile() { free(filename); }
00039 
00046         T* load(T* target, LONG& num=0)
00047         {
00048             if (status)
00049             {
00050                 status=false;
00051 
00052                 if (num==0)
00053                 {
00054                     bool seek_status=true;
00055                     LONG cur_pos=ftell(file);
00056 
00057                     if (cur_pos!=-1)
00058                     {
00059                         if (!fseek(file, 0, SEEK_END))
00060                         {
00061                             if ((num=(int)ftell(file)) != -1)
00062                             {
00063                                 SG_INFO( "file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T));
00064                                 num/=sizeof(T);
00065                             }
00066                             else
00067                                 seek_status=false;
00068                         }
00069                         else
00070                             seek_status=false;
00071                     }
00072 
00073                     if ((fseek(file,cur_pos, SEEK_SET)) == -1)
00074                         seek_status=false;
00075 
00076                     if (!seek_status)
00077                     {
00078                         SG_ERROR( "filesize autodetection failed\n");
00079                         num=0;
00080                         return NULL;
00081                     }
00082                 }
00083 
00084                 if (num>0)
00085                 {
00086                     if (!target)
00087                         target=new T[num];
00088 
00089                     if (target)
00090                     {
00091                         size_t num_read=fread((void*) target, sizeof(T), num, file);
00092                         status=((LONG) num_read == num);
00093 
00094                         if (!status)
00095                             SG_ERROR( "only %ld of %ld entries read. io error\n", (LONG) num_read, num);
00096                     }
00097                     else
00098                         SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (LONG) num, filename);
00099                 }
00100                 return target;
00101             }
00102             else 
00103             {
00104                 num=-1;
00105                 return NULL;
00106             }
00107         }
00108 
00115         bool save(T* target, LONG num)
00116         {
00117             if (status)
00118             {
00119                 status=false;
00120                 if (num>0)
00121                 {
00122                     if (!target)
00123                         target=new T[num];
00124 
00125                     if (target)
00126                     {
00127                         status=(fwrite((void*) target, sizeof(T), num, file) == (unsigned long) num);
00128                     }
00129                 }
00130             }
00131             return status;
00132         }
00133 
00138         inline bool is_ok() { return status; }
00139 
00140     protected:
00142         FILE* file;
00144         bool status;
00146         CHAR task;
00148         CHAR* filename;
00149 };
00150 #endif

SHOGUN Machine Learning Toolbox - Documentation