Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

dbf.h

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (C) 2000
00004 // Ralf Westram
00005 // (Coded@ReallySoft.de)
00006 //
00007 // Permission to use, copy, modify, distribute and sell this software
00008 // and its documentation for any purpose is hereby granted without fee,
00009 // provided that the above copyright notice appear in all copies and
00010 // that both that copyright notice and this permission notice appear
00011 // in supporting documentation.  Ralf Westram makes no
00012 // representations about the suitability of this software for any
00013 // purpose.  It is provided "as is" without express or implied warranty.
00014 //
00015 // This code is part of my library.
00016 // You may find a more recent version at http://www.reallysoft.de/
00017 //
00019 
00020 #ifndef DBF_H
00021 #define DBF_H
00022 
00023 #ifndef __CTIME__
00024 #include <ctime>
00025 #endif
00026 #ifndef __FSTREAM__
00027 #include <fstream>
00028 #endif
00029 #ifndef __MAP__
00030 #include <map>
00031 #endif
00032 
00033 
00034 #ifndef ERR_H
00035 #include "Err.h"
00036 #endif
00037 #ifndef SMARTPTR_H
00038 #include "smartptr.h"
00039 #endif
00040 
00041 
00042 namespace rs {
00043 
00045     namespace dbf {
00046 
00047         // --------------------------------------------------------------------------------
00048         //     class DBF_EntryDeclaration
00049         // --------------------------------------------------------------------------------
00050 
00052         class DBF_EntryDeclaration {
00053         private:
00054             std::string name;
00055             char type;
00056             char xtra[4];
00057             unsigned char fieldlen;
00058             char right;
00059             unsigned offset; // in record
00060             unsigned index; // index of field in record
00061 
00062         public:
00063             DBF_EntryDeclaration(unsigned char firstChar, std::istream& in, unsigned& offset_in_record, unsigned& index_);
00064             virtual ~DBF_EntryDeclaration() {}
00065 
00066             const std::string& Name() const { return name; }
00067             unsigned Fieldlen() const { return fieldlen; }
00068             unsigned Offset() const { return offset; }
00069             unsigned Index() const { return index; }
00070 
00071             bool is_numeric() const { return type=='N'; }
00072             bool is_string() const { return type=='C'; }
00073 
00074             void dump(std::ostream& out) const;
00075             DECLARE_STREAM_OP(DBF_EntryDeclaration);
00076 
00077             bool operator<(const DBF_EntryDeclaration& other) const { return name<other.name; }
00078         };
00079 
00080         typedef SmartPtr<DBF_EntryDeclaration> DBF_EntryDeclarationPtr;
00081         typedef std::map<std::string, DBF_EntryDeclarationPtr> DBF_EntryDeclarationList;
00082 
00083         // --------------------------------------------------
00084         //      class DBF_Reader : rs_boost::noncopyable
00085         // --------------------------------------------------
00087         class DBF_Reader : rs_boost::noncopyable {
00088         private:
00089             std::string filename;
00090             std::ifstream in;
00091 
00092             struct tm created;
00093             unsigned no_of_records;
00094             unsigned length_of_header;
00095             unsigned record_size;
00096 
00097             DBF_EntryDeclarationList entryTypes;
00098 
00099             unsigned current_record;
00100             char *current_record_data;
00101 
00102             void read_record(unsigned record_number) {
00103                 if (record_number!=current_record) {
00104                     in.seekg(length_of_header+record_number*record_size+1);
00105                     in.read(current_record_data, record_size);
00106                     current_record = record_number;
00107                 }
00108             }
00109 
00110         public:
00121             DBF_Reader(const std::string& filename_);
00122             virtual ~DBF_Reader() { delete current_record_data; }
00123 
00126             void dumpEntryDefinition(std::ostream& out) const;
00127 
00131             unsigned Fields() const { return entryTypes.size(); }
00132 
00138             const std::string& FieldName(unsigned num) const;
00139 
00142             unsigned Records() const { return no_of_records; }
00143 
00146             bool nextRecord() {
00147                 if (current_record==no_of_records-1) return false;
00148                 read_record(current_record+1);
00149                 return true;
00150             }
00151 
00155             bool gotoRecord(unsigned index) {
00156                 if (index>=no_of_records) return false;
00157                 read_record(index);
00158                 return true;
00159             }
00160 
00168             std::string FieldContent(const std::string& fieldName, bool crop=true) const;
00169         };
00170     };
00171 };
00172 
00173 #else
00174 #error dbf.h included twice
00175 #endif // DBF_H

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen