entry.h

Go to the documentation of this file.
00001 // Copyright (C) 2004 Xavier Décoret <Xavier.Decoret@imag.fr>
00002 
00003 // This program is free software; you can redistribute it and/or 
00004 // modify it under the terms of the GNU General Public License 
00005 // as published by the Free Software Foundation; either 
00006 // version 2 of the License, or (at your option) any later 
00007 // version.
00008 
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 #ifndef XDKBIB_ENTRY_H
00019 #define XDKBIB_ENTRY_H
00020 
00021 #if HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <map>
00028 
00029 namespace xdkbib
00030 {
00031   class ValuePart;
00032   class Field;
00033   class FieldHandle;
00034   class Entry;
00035 
00036   typedef enum { Braced,Quoted,Number,String } ValuePartType;
00037   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00038   // Interface of ValuePart
00039   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00040   class ValuePart
00041   {
00042   public:
00043     ValuePart();
00044     ValuePart(ValuePartType t,const std::string& v);
00045     ValuePartType type() const;
00046     const std::string& content() const;
00047   private:
00048     ValuePartType type_;
00049     std::string   content_;
00050   };    
00051   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00052   // Interface of Field
00053   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00054   class Field
00055   {
00056   public:
00057     Field();
00058     const std::string& name() const;
00059     const std::vector<ValuePart>& valueParts() const;
00060     int line() const;
00061   private:
00062     friend class FieldHandle;
00063     friend class Entry;
00064     Field(const std::string& name,int line_=-1);
00065     std::string            name_;
00066     std::vector<ValuePart> valueParts_;
00067     int                    line_;
00068   };
00069   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00070   // Interface of FieldHandle
00071   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00072   class FieldHandle
00073   {
00074   public:
00075     FieldHandle();
00076     const Field* operator->() const;
00077     const std::string& name() const;
00078     std::string value() const;    
00079     bool isMissing() const;
00080 
00081     FieldHandle& add(const ValuePart& p,int line=-1);
00082     FieldHandle& operator<<(const ValuePart& p);
00083     FieldHandle& operator+=(const ValuePart& p);
00084     template <class O>
00085     FieldHandle& append(O first,const O& last,int line=-1);
00086     void clear();
00087     
00088     const Entry* entry() const;
00089     operator bool() const;
00090 
00091     FieldHandle& operator++();
00092     void next();
00093   protected:
00094     friend class Entry;
00095     FieldHandle(Entry* e,const std::string& name);
00096     FieldHandle(Entry* e,const std::map<std::string,Field>::iterator& f);  
00097   private:
00098     std::string                           xname_;
00099     bool                                  missing_;
00100     std::map<std::string,Field>::iterator field_;
00101     Entry*                                entry_;
00102   };
00103   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00104   // Interface of Entry
00105   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00106   class Entry
00107   {
00108   public:
00109     Entry();
00110     virtual ~Entry();
00111     Entry(const std::string& type,const std::string& key,int line=-1);
00112     const std::string& type() const;
00113     const std::string& key() const;
00114     int line() const;
00115     const std::string& comment() const;
00116     void setComment(const std::string&);
00117 
00118     FieldHandle addField(const std::string& name,int line=-1);
00119 
00120     bool hasField(const std::string& fieldName) const;
00121     FieldHandle field(const std::string& fieldName);
00122     FieldHandle firstField();
00123 
00124     const std::string& sortKey() const;
00125     void setSortKey(const std::string& s);
00126 
00127     const std::map<std::string,Field>& fields() const;
00128 
00129     const std::string& longestFieldName() const;
00130   protected:
00131     friend class FieldHandle;
00132     virtual std::string valueOf(const std::string& s) const;
00133   private:
00134     std::string                 type_;
00135     std::string                 key_;
00136     std::map<std::string,Field> fieldsMap_;
00137     std::string                 sortKey_;
00138     int                         line_;
00139     std::string                 comment_;
00140   };
00141   //************************************************************
00142   // Implementation of FieldHandle
00143   //************************************************************
00144   template <class O>
00145   FieldHandle&
00146   FieldHandle::append(O first,const O& last,int line)
00147   {
00148     if (isMissing())
00149     {
00150       field_ = entry_->addField(xname_).field_;
00151       field_->second.line_ = line;
00152     }
00153     while (first != last)
00154     {
00155       field_->second.valueParts_.push_back(*first++);
00156     }
00157   }
00158 }
00159 
00160 #endif // XDKBIB_ENTRY_H

Generated on Wed Jan 24 01:14:42 2007 for XDKBIBTEX by  doxygen 1.5.1