00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00068
00069
00070 #ifndef _NML_H_
00071 #define _NML_H_
00072
00073
00074 #ifdef WIN32
00075 #pragma warning(push,3)
00076 #pragma warning(disable:4018)
00077 #pragma warning(disable:4100)
00078 #pragma warning(disable:4514)
00079 #pragma warning(disable:4786)
00080 #endif
00081
00082 #include <string>
00083 #include <vector>
00084 #include <ios>
00085
00086 #ifdef WIN32
00087 #pragma warning(pop)
00088 #pragma warning(disable:4786)
00089 #pragma warning(disable:4514)
00090 #endif
00091
00092
00093 std::string HexDump(const unsigned char *p,
00094 unsigned int len,
00095 unsigned int bytes_per_line = 16);
00096 std::string HexDump(const char *p,
00097 unsigned int len,
00098 unsigned int bytes_per_line = 16);
00099
00100
00101
00103 class NMLEscapeCodeHandler
00104 {
00105 public:
00106 virtual bool Convert(std::string & dest,
00107 const std::string & src) const = 0;
00108 };
00109
00110
00112 class RemoveNMLEscapeSequences : public NMLEscapeCodeHandler
00113 {
00114 public:
00115 RemoveNMLEscapeSequences();
00116 virtual ~RemoveNMLEscapeSequences();
00117
00118 bool Convert(std::string & dest,
00119 const std::string & src) const;
00120 };
00121
00122
00124 class NMLEscapeSequences2HTML : public NMLEscapeCodeHandler
00125 {
00126 public:
00127 NMLEscapeSequences2HTML();
00128 virtual ~NMLEscapeSequences2HTML();
00129
00130 bool Convert(std::string & dest,
00131 const std::string & src) const;
00132 };
00133
00134
00135
00137 class NML
00138 {
00139 friend class NMLFactory;
00140
00141 public:
00142
00143 static const unsigned int NML_MAX_LEN;
00144 static const unsigned int NML_MAX_NR_OF_LEVELS;
00145 static const unsigned int NML_MAX_NR_OF_MENU_ITEMS;
00146 static const unsigned int MAX_NR_OF_WATCHES;
00147 static const unsigned int NML_MIN_NR_OF_ITEM_BYTES;
00148 static const unsigned int NML_NR_OF_HEADER_BYTES;
00149 static const unsigned short ROOT_OBJECT_ID;
00150 static const char *ObjectTypeString[];
00151
00152
00153
00155 typedef enum {INVALID, MENU, PLAIN, TITLE, LIST} object_type_t;
00156
00158 typedef unsigned short NewsObjectId_t;
00159
00161 typedef struct
00162 {
00163 unsigned short nml_len;
00164 unsigned short extended_header_len;
00165 unsigned char nml[2048];
00166 } RawNewsObject_t;
00167
00169 typedef struct
00170 {
00171 std::string text;
00172 unsigned short link_id;
00173 bool link_id_available;
00174 } Item_t;
00175
00177 typedef struct
00178 {
00179 NewsObjectId_t object_id;
00180 object_type_t object_type;
00181 bool static_flag;
00182 unsigned char revision_index;
00183 std::string extended_header;
00184 std::string title;
00185 std::vector<Item_t> item;
00186 } News_t;
00187
00188
00189 NML();
00190 NML(const NML & prototype);
00191 ~NML();
00192
00193
00194 const NML & operator=(const NML & prototype);
00195 bool operator==(const NML & prototype) const;
00196 std::ostream & operator<<(std::ostream & os) const;
00197
00198
00199 std::string Dump(void) const;
00200
00203 inline bool isValid(void) const
00204 {
00205 return _valid;
00206 }
00207
00210 inline bool isRootObject(void) const
00211 {
00212 return (GetObjectId()==ROOT_OBJECT_ID);
00213 }
00214
00217 inline bool isMenu(void) const
00218 {
00219 return (GetObjectType()==MENU);
00220 }
00221
00224 inline bool isStatic(void) const
00225 {
00226 return _news.static_flag;
00227 }
00228
00231 inline object_type_t GetObjectType(void) const
00232 {
00233 return _news.object_type;
00234 }
00235
00238 inline const char *GetObjectTypeString(void) const
00239 {
00240 if (_news.object_type<=LIST)
00241 {
00242 return NML::ObjectTypeString[_news.object_type];
00243 }
00244 else
00245 {
00246 return "illegal";
00247 }
00248 }
00249
00252 inline NewsObjectId_t GetObjectId(void) const
00253 {
00254 return _news.object_id;
00255 }
00256
00259 inline unsigned char GetRevisionIndex(void) const
00260 {
00261 return _news.revision_index;
00262 }
00263
00266 inline std::string GetExtendedHeader(void) const
00267 {
00268 return _news.extended_header;
00269 }
00270
00273 inline std::string GetTitle(void) const
00274 {
00275 return _news.title;
00276 }
00277
00280 inline unsigned int GetNrOfItems(void) const
00281 {
00282 return _news.item.size();
00283 }
00284
00287 inline const std::vector<Item_t> & GetItems(void) const
00288 {
00289 return _news.item;
00290 }
00291
00294 inline std::string GetItemText(unsigned int i) const
00295 {
00296 return (i<GetNrOfItems())?_news.item[i].text:"";
00297 }
00298
00304 inline NewsObjectId_t GetLinkId(unsigned int i) const
00305 {
00306 if (i>=GetNrOfItems()) return 0x0815;
00307 return _news.item[i].link_id;
00308 }
00309
00313 inline bool isLinkIdAvailable(unsigned int i) const
00314 {
00315 return (i<GetNrOfItems()) ? _news.item[i].link_id_available : false;
00316 }
00317
00318
00319
00322 inline void SetObjectId(NewsObjectId_t oid)
00323 {
00324 _news.object_id = oid;
00325 }
00326
00330 inline void SetLinkAvailability(unsigned int i, bool f)
00331 {
00332 if (isMenu() && (i<GetNrOfItems()))
00333 _news.item[i].link_id_available = f;
00334 }
00335
00336 void SetError(NewsObjectId_t oid, const char *title);
00337 void SetErrorDump(NewsObjectId_t oid,
00338 const RawNewsObject_t & rno,
00339 const char *error_msg);
00340
00341 private:
00342 bool _valid;
00343 News_t _news;
00344 NMLEscapeCodeHandler *_EscapeCodeHandler;
00345 };
00346
00347 bool operator==(const NML::Item_t &, const NML::Item_t &);
00348 std::string DumpRaw(const NML::RawNewsObject_t & rno);
00349
00350
00351
00352
00353 class NMLFactory
00354 {
00355 public:
00356 static NMLFactory *Instance(void);
00357 static void ExitInstance(void);
00358
00359 NML *CreateNML(const NML::RawNewsObject_t & rno,
00360 const NMLEscapeCodeHandler *EscapeCodeHandler);
00361 NML *CreateError(NML::NewsObjectId_t oid,
00362 const char *title);
00363 NML *CreateErrorDump(NML::NewsObjectId_t oid,
00364 const NML::RawNewsObject_t & rno,
00365 const char *error_msg);
00366
00367 private:
00368 static NMLFactory *_instance;
00369 NMLFactory();
00370 ~NMLFactory();
00371 NMLFactory & operator=(const NMLFactory &);
00372 NMLFactory(const NMLFactory &);
00373 };
00374
00375
00376 #endif