root/tolp/branches/NameBlockClass/tol/OIS/tol_ois.h @ 520

Revision 520, 21.3 kB (checked in by vdebuen, 5 years ago)

New member BNameBlock::localName_ and related methods

  • Property svn:eol-style set to native
Line 
1/* tol_ois.h: Object's Indexed Serialization.
2                   GNU/TOL Language.
3
4   Copyright (C) 2005 - Bayes Decision, SL (Spain [EU])
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19   USA.
20 */
21
22#ifndef TOL_OIS_H
23#define TOL_OIS_H 1
24
25
26#include <tol/tol_btimer.h>
27#include <tol/tol_bsetgra.h>
28#include <tol/tol_bdatgra.h>
29#include <tol/tol_oisstream.h>
30
31//--------------------------------------------------------------------
32// defines
33//--------------------------------------------------------------------
34
35//Each format change is specified by a version tag XX.YY
36//When the first part doesn't change new software version is able to
37//read old OIS files. This will be called a soft change.
38//When the first part changes new software version is not able to
39//read old OIS files. This will be called a hard change. Then a
40//traslation utility will be available.
41#define OIS_VERSION "02.07.b"
42
43#define OIS_Load_Mode_Header          0
44#define OIS_Load_Mode_Data            1
45#define OIS_Load_Mode_ShowHierarchy   2
46
47//Maximum default size of XML strings
48#define OIS_MaxXmlStrLen 1024*16
49
50//Minimum size of strings to be compressed
51#define OIS_MinStreamSizeToCompress 32
52
53#if defined(_MSC_VER) && (_MSC_VER<1400)
54#  define _LLD64_ "I64d"
55#else
56#  define _LLD64_ "lld"
57#endif
58
59//Enable these macroes to generate log files about OIS image contens
60//It can be usefull to debug some errors
61//#define TRACE_OIS_FILE
62//#define TRACE_OIS_TOKEN
63
64//--------------------------------------------------------------------
65// macros
66//--------------------------------------------------------------------
67#define Ensure(doit) if(!(doit)) { return(false); }
68
69
70//--------------------------------------------------------------------
71  class BOis
72/*
73  OIS: Objects Indexed Serilaization
74  ==================================
75
76  OIS is a tool for save and load TOL objects in a binary native
77  format independently of machine, operative system and compiler.
78
79  Objects: means both TOL objects, functions and structures of
80    any type.
81
82  Indexed: means that objects can be partially searched and
83    loaded by demand. (Delayed loading IS STILL NOT IMPLEMENTED)
84
85  Serialization: means that all information about the object is
86    explictly specified: name, description, expression and contens.
87     -Functions are saved in a binary tree so they will not be parsed.
88     -TimeSet's and Serie's contens both it's virtual tree evaluation
89    method and a vector with data between bounds dates. For non
90    bounded ones default limit dates will be used.
91
92  TOL user can save any set of TOL objects and reload in other TOL
93  session and then use it in the same way that any just declared
94  object.
95
96  You can create TOL libraries and modules of global functions and
97  objects or create data stores of pre-calculated objects.
98
99  An OIS is contained in a plain directory with an XML header that
100  could be used by external documentation managers and a set of
101  binary files containg micro-compressed information about TOL objects
102  and its hierarchy relations.
103
104  Micro-compression means that each information item is compressed
105  independently of the rest allowing partial and random reading
106  instead of full sequential reading of macro-compression. This
107  feature has been implemented with standard multiplattform
108  open-source library BZIP2 (www.bzip.org)
109*/
110//--------------------------------------------------------------------
111{
112public:
113  //------------------------------------------------------------------
114     class BXMLParameters
115  // Contains information about XML header parameters
116  //------------------------------------------------------------------
117  {
118   public:
119    int maxXmlStrLen_;  //Maximum size for XML strings
120    BXMLParameters() : maxXmlStrLen_(0) {}
121    ~BXMLParameters() {}
122  } xmlParam_;
123
124  //------------------------------------------------------------------
125     class BControl
126  // Contains information about OIS building circumstances in order
127  // to control loading process
128  //------------------------------------------------------------------
129  {
130  public:
131  //Information about OIS engine
132    class OisEngine
133    {
134     public: 
135      BText oisVersion_;         //OIS_VERSION at building time
136      BDate release_;            //Building date and time
137      BText curDir_;             //Current directory at building time
138      OisEngine() : oisVersion_(), release_(), curDir_() {}
139     ~OisEngine() {}
140    } oisEngine_;
141
142  //Information about host computer and environment where OIS was built
143    class Machine
144    {
145     public: 
146      BText hostName_;           //IP or name
147      bool  isLittleEndian_;     //Big or little endian flag
148      BText osName_;             //Operative System
149      Machine() : hostName_(), isLittleEndian_(false), osName_() {}
150     ~Machine() {}
151    } machine_;
152
153  //Information about TOL engine
154    class TolEngine
155    {
156     public: 
157      BText cppCompiler_;        //C++ compiler
158      BText tolVersion_;         //TOL version
159      BText tolRelease_;         //TOL release date and time
160      BText tolUserLang_;        //TOL user language
161      TolEngine() 
162      : cppCompiler_(), 
163        tolVersion_(), 
164        tolRelease_(), 
165        tolUserLang_() 
166      {}
167      ~TolEngine() {}
168    } tolEngine_;
169
170  //Information about type sizes in bytes that are used in binary mode
171  //and could depend on compiler options, operative system or machine
172    class TypeSizes
173    {
174     public:     
175      short sizeof_char_;       
176      short sizeof_short_;       
177      short sizeof_int_;
178      short sizeof_BINT64_;
179      short sizeof_double_;     
180      short sizeof_BDat_;
181      short sizeof_BCoefDeg_;
182      short sizeof_BGrammarId_;
183
184      TypeSizes() 
185      : sizeof_char_       (sizeof(char)),
186        sizeof_short_      (sizeof(short)),
187        sizeof_int_        (sizeof(int)),
188        sizeof_BINT64_     (sizeof(BINT64)),
189        sizeof_double_     (sizeof(double)),
190        sizeof_BDat_       (sizeof(BDat)),
191        sizeof_BCoefDeg_   (sizeof(BCoefDeg)),
192        sizeof_BGrammarId_ (sizeof(BGrammarId))
193      {}
194     ~TypeSizes() {}
195    } typeSizes_;
196
197    BControl()
198    : oisEngine_(),
199      machine_(),
200      tolEngine_(),
201      typeSizes_()
202    {}
203    ~BControl() {}
204  } control_;
205
206
207  //------------------------------------------------------------------
208     class BAlias
209  // Alias for replacing a value with {$alias}
210  //------------------------------------------------------------------
211  {
212  private:
213    bool CodeDecode(BText& txt, bool justIfBeginWith, const BText& a, const BText& b)
214    {
215      bool doReplace = true;
216      if(justIfBeginWith)
217      {
218        doReplace = txt.BeginWith(a)==BTRUE;
219        if(doReplace)
220        {
221          txt = b+txt.SubString(a.Length(),txt.Length());
222        }
223      }
224      else
225      {
226        txt.Replace(a,b);
227      }
228      return(doReplace);
229    }
230  public:
231    BText alias_;
232    BText value_;
233
234    BText Coded() const 
235    { 
236      return(BText("{$")+alias_+"}");
237    }
238
239    bool Code(BText& txt, bool justIfBeginWith)
240    {
241      return(CodeDecode(txt,justIfBeginWith,value_,Coded()));
242    }
243
244    bool Decode(BText& txt, bool justIfBeginWith)
245    {
246      return(CodeDecode(txt,justIfBeginWith,Coded(),value_));
247    }
248  };
249
250 
251//Types of micro-compressor for internal serialization
252  enum BSerialEngine
253  {
254    BSE_DEFAULT_  = -1,
255    BSE_NONE_     =  0,
256    BSE_BZIP2_    =  1
257  };
258//Types of macro-compressor for external file manager
259  enum BArchiveEngine
260  {
261    BAE_DEFAULT_  = -1,
262    BAE_NONE_     =  0,
263    BAE_ZIPARC_   =  1
264  };
265
266  //------------------------------------------------------------------
267     class BOptions
268  // Contains optional information about OIS configuration
269  //------------------------------------------------------------------
270  {
271   public:
272  //Information about OIS engine
273    class OisConfig
274    {
275     public:
276      bool  buildHierarchy_;     //If false no hierarchy index will be built
277      OisConfig() : buildHierarchy_(false) {}
278     ~OisConfig() {}
279    } oisConfig_;
280
281  //Information about Compression engine
282    class Compression
283    {
284     public:
285    //Serial data must be compressed at low level in independent 
286    //searchable little blocks to allow indexing
287      class MicroSerialization
288      {
289       public:
290        BSerialEngine engine_;       //Engine for compressing-decompressing
291        int           level_;        //Compression level (1..9) In BZIP is the argument blockSize100k
292        int           minSizeCmprs_; //Serial blocks with length shorter than this will not be compressed
293        MicroSerialization() 
294        : engine_(BSE_NONE_), 
295          level_(0), 
296          minSizeCmprs_(0) {}
297       ~MicroSerialization() {}
298      } serialization_;
299    //Files and directories of an OIS collection and source code files could be packed
300    //in just one ZIP-like file
301      class MacroFileManager
302      {
303       public:
304        BArchiveEngine engine_; //Engine for compressing-decompressing
305        int            level_;  //Compression level (1..9) In BZIP is the argument blockSize100k
306
307        MacroFileManager() : engine_(BAE_NONE_), level_(0) {}
308       ~MacroFileManager() {}
309      } fileManager_;
310
311      Compression() : serialization_(), fileManager_() {}
312     ~Compression() {}
313    } compressor_;
314
315    //You can specify any number of alias for absolute paths of your
316    //TOL source root directories. Then each path_k at the begining of
317    //any TOL source file path by will be replaced by its correspondent
318    //alias codified as {&alias_k}\n"
319    BArray<BAlias> tolSourceSearchPaths_;
320
321    BOptions()
322    : oisConfig_(),
323      compressor_(),
324      tolSourceSearchPaths_()
325    {}
326    ~BOptions() {}
327  } options_;
328
329  //------------------------------------------------------------------
330     class BAddress
331  // Contains information about OIS address into the OIS repository
332  // Address path will be built as
333  //   concept/version/node
334  // You can use "/" in all items to have more complex directory
335  // structure
336  //------------------------------------------------------------------
337  {
338   public:
339    BText concept_;  //Usually this will be a representative general
340                     //identifier of the contained information type
341                     //and we will refer it usually as a OIS package
342                     //for TOL libraries or Sessions for data stores
343                     //of massive estimations or other calculations.
344    BText version_;  //Version control identifer to allow evolution
345                     //of the OIS package/session
346    BText node_;     //The node is the internal path of the module
347                     //into the OIS package or the atomic unity in
348                     //data stores
349
350    BAddress() : concept_(), version_(), node_() {}
351   ~BAddress() {}
352  } address_;
353
354  //------------------------------------------------------------------
355     class BDoc
356  // Contains documental information
357  //------------------------------------------------------------------
358  {
359   public:
360    BText name_;         //The identifier of the OIS
361    BText category_;     //Conceptual category
362    BText subject_;      //Brief description in a line
363    BText keys_;         //Keywords for searching
364    BText abstract_;     //Full description
365    BText authors_;      //e-mail or name of authors
366    BText distribution_; //Publication domain: confidential, client, public
367    BText repository_;   //Repository or root path
368    BDoc() 
369    : name_(),
370      category_(),
371      subject_(),
372      keys_(),
373      abstract_(),
374      authors_(),
375      distribution_(),
376      repository_()
377    {}
378   ~BDoc() {}
379  } doc_;
380
381  //------------------------------------------------------------------
382     class BFileStat
383  // Statistics about OIS binary files
384  //------------------------------------------------------------------
385  { 
386   public:
387    int    entries_;
388    BINT64 bytes_;
389    BFileStat() : entries_(0), bytes_() {}
390   ~BFileStat() {}
391  };
392
393  //------------------------------------------------------------------
394     class BStat
395  // Statistics about OIS directory
396  //------------------------------------------------------------------
397  { 
398   public:
399    double buildingTime_;
400    BArray<BFileStat> fileStat_;
401    BStat() : buildingTime_(0), fileStat_() {}
402   ~BStat() {}
403  } stat_;
404
405
406  //------------------------------------------------------------------
407     enum BFileId
408  // OIS binary file identifiers
409  //------------------------------------------------------------------
410  {
411    BFI_header      =  0,
412    BFI_tolref      =  1,
413    BFI_oisref      =  2,
414    BFI_object      =  3,
415    BFI_set         =  4,
416    BFI_timeset     =  5,
417    BFI_serie       =  6,
418    BFI_matrix      =  7,
419    BFI_polyn       =  8,
420    BFI_ratio       =  9,
421    BFI_code        = 10,
422    BFI_hrchyDetail = 11,
423    BFI_hrchyOffset = 12,
424    BFI_hrchyOrder  = 13,
425    BFI_export      = 14,
426    BFI_maxnum      = 15
427  };
428
429  //------------------------------------------------------------------
430     class BCoefDeg
431  // Monomial structure
432  //------------------------------------------------------------------
433  {
434   public:
435    int  degree_;
436    BDat coefficient_;
437    BCoefDeg() : degree_(0), coefficient_() {}
438   ~BCoefDeg() {}
439  };
440
441  //------------------------------------------------------------------
442     class BDatOld
443  // Old BDat with bool known_
444  //------------------------------------------------------------------
445  {
446   public:
447    BBool  known_;  //!< Indicates if the number is known
448    BReal  value_;  //!< Store a number value
449  };
450
451  //------------------------------------------------------------------
452     class BCoefDegOld
453  // Old BDat with bool known_
454  //------------------------------------------------------------------
455  {
456   public:
457    int  degree_;
458    BDatOld coefficient_;
459    BCoefDegOld() : degree_(0), coefficient_() {}
460   ~BCoefDegOld() {}
461  };
462
463  //------------------------------------------------------------------
464     class BIndexElement
465  // Structure of the index of a element in a set
466  //------------------------------------------------------------------
467  {
468   public:
469    BGrammarId    gid_;
470    unsigned char mode_;
471    BINT64        objectOffset_;
472    int           hrchyEntry_;
473    BIndexElement() 
474    : gid_         (BGI_NotFound), 
475      mode_        (0), 
476      objectOffset_(0),
477      hrchyEntry_  (0)
478    {}
479   ~BIndexElement() {}
480  };
481
482  //------------------------------------------------------------------
483     class BGlossaryElement
484  // Structure of the glossary of a named element in a set
485  //------------------------------------------------------------------
486  {
487   public:
488    BText name_;
489    int   position_;
490    BGlossaryElement() : name_(), position_(0) {}
491   ~BGlossaryElement() {}
492  };
493
494  //------------------------------------------------------------------
495     class BHierarchyDetail
496  // Structure of deatil information about hierachy nodes
497  //------------------------------------------------------------------
498  {
499   public:
500    BGrammarId    gid_;
501    unsigned char mode_;
502    BINT64        objectOffset_;
503    int           hrchyEntry_;
504    BText         name_;
505    BHierarchyDetail() 
506    : gid_         (BGI_NotFound), 
507      mode_        (0),
508      objectOffset_(0),
509      hrchyEntry_  (0),
510      name_()
511    {}
512   ~BHierarchyDetail() {}
513  };
514
515  //------------------------------------------------------------------
516     class BHierarchyIndex
517  // Structure of hrchyOffset_ and hrchyOrder_ files
518  //------------------------------------------------------------------
519  {
520   public:
521    int    hrchyOrder_;
522    int    hrchyEntry_;
523    BINT64 hrchyOffset_;
524    BHierarchyIndex() 
525    : hrchyOrder_(0),
526      hrchyEntry_(0),
527      hrchyOffset_(0)
528    {}
529   ~BHierarchyIndex() {}
530  };
531
532
533protected:
534
535#ifdef TRACE_OIS_FILE
536  static FILE* logWrite_;
537  static FILE* logRead_;
538#endif
539
540#ifdef TRACE_OIS_TOKEN
541  static FILE* tokWrite_;
542  static FILE* tokRead_;
543#endif
544
545//Available modes for Ois.Load function
546  static BArray<BSystemDat*> loadModeOptions_;
547
548//Current OIS_VERSION in a BText
549  static BText oisCurrentVersion_;
550
551//Current OIS default root path
552  static BText oisDefRoot_;
553
554//Default OIS archive method
555  static BText oisDefArchive_;
556
557//Default date format
558  static BDateFormat dateFmt_;
559
560//Current endian flag
561  static bool isLittleEndian_;
562
563  static BStruct*  addressStr_;
564  static BStruct*  docStr_;
565  static BUserSet* defDoc_;
566
567//Enable/Disable serialization engine
568  bool   enable_BSE_;
569
570//True if the image has been closed
571  bool   closed_;
572
573//Files
574  BText  connection_;
575  BStreamHandler* streamHandler_;
576  BStream* header_;
577  BStream* tolref_;
578  BStream* oisref_;
579  BStream* object_;
580  BStream* set_;
581  BStream* serie_;
582  BStream* timeset_;
583  BStream* matrix_;
584  BStream* polyn_;
585  BStream* ratio_;
586  BStream* code_;
587  BStream* hrchyDetail_;
588  BStream* hrchyOffset_;
589  BStream* hrchyOrder_;
590  BStream* export_;
591
592  BArray<BStream*>        source_;
593  BArray<BStream*>        allFiles_;
594  BArray<BHierarchyIndex> hierarchyIndex_;
595  BArray<int>             hierarchyOrder_;
596
597//Time counter
598  BTimer elapsed_;
599 
600//Functions
601  void  InitBuild ();
602  void  SetAllFiles();
603  void  SetRoot   (const BText&  root);
604  void  SetAddress(const BSet* address);
605  bool  Error  (const BText& msg) const;
606  bool  Warning(const BText& msg) const;
607  BSyntaxObject* NullError(const BText& msg) const;
608
609public:
610  static int                  def_CLv_;
611  static BSerialEngine def_BSE_;
612  static BArchiveEngine       def_BAE_;
613
614//Parameters
615 
616  BOis();
617  virtual ~BOis();
618
619  static BText SerialEngine(BSerialEngine e=BSE_DEFAULT_);
620  static BText ArchiveEngine(BArchiveEngine a=BAE_DEFAULT_);
621  static void PutDefaultSerialEngine(BOis::BSerialEngine e);
622  static void PutDefaultArchiveEngine(BOis::BArchiveEngine a);
623  static BText ArchiveExtension(BOis::BArchiveEngine a=BAE_DEFAULT_);
624  static BSerialEngine SerialEngine(const BText& txt);
625  static BArchiveEngine       ArchiveEngine      (const BText& txt);
626  static const BText& CurrentVersion() { return(oisCurrentVersion_); }
627  static BText AutoPath(const BText& path);
628  static BText PlainPath    (const BText& path);
629  static BText GetModulePath(const BText& tolFile);
630  static bool  RemoveModule (const BText& tolFile);
631  static BDate GetModuleTime(const BText& tolFile);
632
633  static bool Initialize    ();
634  static void SwapEndian    (void* Addr, const int Nb);
635  static bool IsLittleEndian(void) { return(isLittleEndian_); }
636
637  void  SetConnection(const BText& connection);
638  void SetPath(const BText& connection, 
639               const BSet*  address);
640
641  void SetModulePath(const BText& tolFile);
642
643  BText SubPath () const;
644  BText Path    () const;
645  bool  Exists  () const { return(streamHandler_!=NULL); }
646  void GetTolSourceSearchPaths(const BText& fun, const BSet& options, int opt);
647  bool RemoveImage ();
648  virtual bool Close();
649};
650
651//--------------------------------------------------------------------
652// inline functions
653//--------------------------------------------------------------------
654
655//--------------------------------------------------------------------
656  inline int CompareAddress(const void* v1, const void* v2)
657
658/*! Compair two atom receiving the double pointers.
659 * \param v1 first atom to caompare
660 * \param v2 second atom to compare
661 * \return returns an integer less than, equal to, or greather then zero,
662 *         if the name of \a v1is found, respectively, to be less then, to
663 *         match, or be greater then name of \a v2
664 */
665//--------------------------------------------------------------------
666{
667  BSyntaxObject* obj1 = *((BSyntaxObject**)v1);
668  BSyntaxObject* obj2 = *((BSyntaxObject**)v2);
669  size_t s1 = (size_t)obj1;
670  size_t s2 = (size_t)obj2;
671  return((s1<s2)?-1:(s1>s2)?1:0);
672}
673
674//--------------------------------------------------------------------
675   inline int CompareGlossaryElement(const void* v1, const void* v2)
676// Compares the name of two received pointers to BGlossaryElement
677//--------------------------------------------------------------------
678{
679  BOis::BGlossaryElement* obj1 = (BOis::BGlossaryElement*)v1;
680  BOis::BGlossaryElement* obj2 = (BOis::BGlossaryElement*)v2;
681        return( BText::Compare(&obj1->name_, &obj2->name_));
682};
683
684
685//--------------------------------------------------------------------
686   inline int CompareHrchyEntry(const void* v1, const void* v2)
687// Compares the entry number of two received pointers to
688// BHierarchyIndex
689//--------------------------------------------------------------------
690{
691  BOis::BHierarchyIndex* obj1 = (BOis::BHierarchyIndex*)v1;
692  BOis::BHierarchyIndex* obj2 = (BOis::BHierarchyIndex*)v2;
693  size_t s1 = (size_t)obj1->hrchyEntry_;
694  size_t s2 = (size_t)obj2->hrchyEntry_;
695  return((s1<s2)?-1:(s1>s2)?1:0);
696}
697
698//--------------------------------------------------------------------
699   inline int CompareAliasValue(const void* v1, const void* v2)
700// Compares the entry number of two received pointers to
701// BAlias
702//--------------------------------------------------------------------
703{
704  BOis::BAlias* obj1 = (BOis::BAlias*)v1;
705  BOis::BAlias* obj2 = (BOis::BAlias*)v2;
706        return( BText::Compare(&obj1->value_, &obj2->value_));
707}
708
709#endif // TOL_OIS_H
710
711
Note: See TracBrowser for help on using the browser.