File.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 __FILE_H__
00012 #define __FILE_H__
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <stdarg.h>
00017 
00018 #include "base/SGObject.h"
00019 #include "lib/DynamicArray.h"
00020 #include "lib/SimpleFile.h"
00021 #include "features/Features.h"
00022 
00023 namespace shogun
00024 {
00025     class DynamicArray;
00026     class SimpleFile;
00027     class Features;
00028     enum EFeatureType;
00029 
00030 template <class ST> struct T_STRING;
00031 template <class ST> struct TSparse;
00032 
00040 class CFile : public CSGObject
00041 {
00042 public:
00047     CFile(FILE* f);
00048 
00060     CFile(char* fname, char rw, EFeatureType type, char fourcc[4]=NULL);
00061 
00062     virtual ~CFile();
00063 
00069     int32_t parse_first_header(EFeatureType &type);
00070 
00076     int32_t parse_next_header(EFeatureType &type);
00077 
00078     // set target to NULL to get it automagically allocated
00079     // set num to 0 if whole file is to be read
00086     int32_t*   load_int_data(int32_t* target, int64_t& num);
00087 
00094     float64_t*  load_real_data(float64_t* target, int64_t& num);
00095 
00102     float32_t*  load_shortreal_data(float32_t* target, int64_t& num);
00103 
00110     char*  load_char_data(char* target, int64_t& num);
00111 
00118     uint8_t*  load_byte_data(uint8_t* target, int64_t& num);
00119 
00126     uint16_t*  load_word_data(uint16_t* target, int64_t& num);
00127 
00134     int16_t* load_short_data(int16_t* target, int64_t& num);
00135 
00142     template <class DT> DT* load_data(DT* target, int64_t& num)
00143     {
00144         CSimpleFile<DT> f(filename, file);
00145         target=f.load(target, num);
00146         status=(target!=NULL);
00147         return target;
00148     }
00149 
00156     template <class DT> bool save_data(DT* src, int64_t num)
00157     {
00158         CSimpleFile<DT> f(filename, file);
00159         status=f.save(src, num);
00160         return status;
00161     }
00162 
00169     bool save_int_data(int32_t* src, int64_t num);
00170 
00177     bool save_real_data(float64_t* src, int64_t num);
00178 
00185     bool save_shortreal_data(float32_t* src, int64_t num);
00186 
00193     bool save_char_data(char* src, int64_t num);
00194 
00201     bool save_byte_data(uint8_t* src, int64_t num);
00202 
00209     bool save_word_data(uint16_t* src, int64_t num);
00210 
00217     bool save_short_data(int16_t* src, int64_t num);
00218 
00223     inline bool is_ok()
00224     {
00225         return status;
00226     }
00227 
00228 
00241     bool read_real_valued_sparse(
00242         TSparse<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec);
00243 
00251     bool write_real_valued_sparse(
00252         const TSparse<float64_t>* matrix, int32_t num_feat, int32_t num_vec);
00253 
00265     bool read_real_valued_dense(
00266         float64_t*& matrix, int32_t& num_feat, int32_t& num_vec);
00267 
00275     bool write_real_valued_dense(
00276         const float64_t* matrix, int32_t num_feat, int32_t num_vec);
00277 
00289     bool read_char_valued_strings(T_STRING<char>*& strings, int32_t& num_str, int32_t& max_string_len);
00290 
00297     bool write_char_valued_strings(const T_STRING<char>* strings, int32_t num_str);
00298 
00300     inline virtual const char* get_name() const { return "File"; }
00301 
00302 protected:
00307     bool read_header();
00312     bool write_header();
00313 
00314 private:
00316     template <class T> void append_item(CDynamicArray<T>* items, char* ptr_data, char* ptr_item);
00317 
00318 protected:
00320     FILE* file;
00322     bool status;
00324     char task;
00326     char* filename;
00328     EFeatureType expected_type;
00330     int32_t num_header;
00332     char fourcc[4];
00333 };
00334 }
00335 #endif

SHOGUN Machine Learning Toolbox - Documentation