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 #ifndef CONFFILE_H
00027 #define CONFFILE_H
00028
00029 #include <stdio.h>
00030
00031 #include <libplayercore/player.h>
00032
00167 class ConfigFile
00168 {
00170 public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00171
00173 public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00174
00176 public: ~ConfigFile();
00177
00179 private: void InitFields();
00180
00184 public: bool Load(const char *filename);
00185
00186
00187
00188 private: bool Save(const char *filename);
00189
00192 public: bool WarnUnused();
00193
00199 public: bool ReadBool(int section, const char *name, bool value);
00200
00201
00202 private: void WriteBool(int section, const char* name, bool value);
00203
00204
00205 private: void WriteBool_Compat(int section, const char* name, bool value);
00206
00207
00213 public: const char *ReadString(int section,
00214 const char *name,
00215 const char *value);
00216
00217
00218 private: void WriteString(int section,
00219 const char *name,
00220 const char *value);
00221
00227 public: int ReadInt(int section,
00228 const char *name,
00229 int value);
00230
00231
00232 private: void WriteInt(int section,
00233 const char *name,
00234 int value);
00235
00241 public: double ReadFloat(int section,
00242 const char *name,
00243 double value);
00244
00245
00246 private: void WriteFloat(int section,
00247 const char *name,
00248 double value);
00249
00255 public: double ReadLength(int section,
00256 const char *name,
00257 double value);
00258
00259
00260 private: void WriteLength(int section,
00261 const char *name,
00262 double value);
00263
00273 public: double ReadAngle(int section, const char *name, double value);
00274
00285 public: uint32_t ReadColor(int section,
00286 const char *name,
00287 uint32_t value);
00288
00298 public: const char *ReadFilename(int section,
00299 const char *name,
00300 const char *value);
00301
00305 public: int GetTupleCount(int section, const char *name);
00306
00313 public: const char *ReadTupleString(int section,
00314 const char *name,
00315 int index,
00316 const char *value);
00317
00318
00319 private: void WriteTupleString(int section,
00320 const char *name,
00321 int index,
00322 const char *value);
00323
00330 public: int ReadTupleInt(int section,
00331 const char *name,
00332 int index,
00333 int value);
00334
00335
00336 private: void WriteTupleInt(int section,
00337 const char *name,
00338 int index,
00339 int value);
00340
00341
00348 public: double ReadTupleFloat(int section,
00349 const char *name,
00350 int index,
00351 double value);
00352
00353
00354 private: void WriteTupleFloat(int section,
00355 const char *name,
00356 int index,
00357 double value);
00358
00365 public: double ReadTupleLength(int section,
00366 const char *name,
00367 int index,
00368 double value);
00369
00370
00371 private: void WriteTupleLength(int section,
00372 const char *name,
00373 int index,
00374 double value);
00375
00386 public: double ReadTupleAngle(int section,
00387 const char *name,
00388 int index,
00389 double value);
00390
00391
00392 private: void WriteTupleAngle(int section,
00393 const char *name,
00394 int index,
00395 double value);
00396
00408 public: uint32_t ReadTupleColor(int section,
00409 const char *name,
00410 int index,
00411 uint32_t value);
00412
00417
00425 public: int ReadDeviceAddr(player_devaddr_t* addr, int section,
00426 const char *name, int code, int index,
00427 const char *key);
00428
00429
00430 public: bool ParseDriver(int section);
00431
00432
00433 public: bool ParseAllDrivers();
00434
00436 public: int GetSectionCount();
00437
00439 public: const char *GetSectionType(int section);
00440
00443 public: int LookupSection(const char *type);
00444
00447 public: int GetSectionParent(int section);
00448
00449
00451
00452
00453
00454 private: bool LoadTokens(FILE *file, int include);
00455
00456
00457 private: bool LoadTokenComment(FILE *file, int *line, int include);
00458
00459
00460 private: bool LoadTokenWord(FILE *file, int *line, int include);
00461
00462
00463 private: bool LoadTokenInclude(FILE *file, int *line, int include);
00464
00465
00466 private: bool LoadTokenNum(FILE *file, int *line, int include);
00467
00468
00469 private: bool LoadTokenString(FILE *file, int *line, int include);
00470
00471
00472 private: bool LoadTokenSpace(FILE *file, int *line, int include);
00473
00474
00475 private: bool SaveTokens(FILE *file);
00476
00477
00478 private: void ClearTokens();
00479
00480
00481 private: bool AddToken(int type, const char *value, int include);
00482
00483
00484 private: bool SetTokenValue(int index, const char *value);
00485
00486
00487 private: const char *GetTokenValue(int index);
00488
00490 public: void DumpTokens();
00491
00492
00493 private: bool ParseTokens();
00494
00495
00496 private: bool ParseTokenInclude(int *index, int *line);
00497
00498
00499 private: bool ParseTokenDefine(int *index, int *line);
00500
00501
00502 private: bool ParseTokenWord(int section, int *index, int *line);
00503
00504
00505 private: bool ParseTokenSection(int section, int *index, int *line);
00506
00507
00508 private: bool ParseTokenField(int section, int *index, int *line);
00509
00510
00511 private: bool ParseTokenTuple(int section, int field,
00512 int *index, int *line);
00513
00514
00515 private: void ClearMacros();
00516
00517
00518 private: int AddMacro(const char *macroname, const char *sectionname,
00519 int line, int starttoken, int endtoken);
00520
00521
00522
00523 private: int LookupMacro(const char *macroname);
00524
00525
00526 private: void DumpMacros();
00527
00528
00529 private: void ClearSections();
00530
00531
00532 private: int AddSection(int parent, const char *type);
00533
00535 public: void DumpSections();
00536
00537
00538 private: void ClearFields();
00539
00540
00541 private: int AddField(int section, const char *name, int line);
00542
00543
00544 private: void AddFieldValue(int field, int index, int value_token);
00545
00546
00547 private: int GetField(int section, const char *name);
00548
00549
00550 private: int GetFieldValueCount(int field);
00551
00552
00553
00554 private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00555
00556
00557 private: void SetFieldValue(int field, int index, const char *value);
00558
00560 public: void DumpFields();
00561
00562
00563 private: uint32_t LookupColor(const char *name);
00564
00566 public: char *filename;
00567
00568
00569 private: enum
00570 {
00571 TokenComment,
00572 TokenWord, TokenNum, TokenString, TokenBool,
00573 TokenOpenSection, TokenCloseSection,
00574 TokenOpenTuple, TokenCloseTuple,
00575 TokenSpace, TokenEOL
00576 };
00577
00578
00579 private: struct Token
00580 {
00581
00582 int include;
00583
00584
00585 int type;
00586
00587
00588 char *value;
00589 };
00590
00591
00592
00593 private: int token_size, token_count;
00594 private: Token *tokens;
00595
00596
00597 private: struct CMacro
00598 {
00599
00600 const char *macroname;
00601
00602
00603 const char *sectionname;
00604
00605
00606 int line;
00607
00608
00609 int starttoken, endtoken;
00610 };
00611
00612
00613 private: int macro_size;
00614 private: int macro_count;
00615 private: CMacro *macros;
00616
00617
00618 private: struct Section
00619 {
00620
00621 int parent;
00622
00623
00624 const char *type;
00625 };
00626
00627
00628 private: int section_size;
00629 private: int section_count;
00630 private: Section *sections;
00631
00632
00633 private: struct Field
00634 {
00635
00636 int section;
00637
00638
00639 const char *name;
00640
00641
00642 int value_count;
00643 int *values;
00644
00645
00646 bool *useds;
00647
00648
00649 int line;
00650 };
00651
00652
00653 private: int field_size;
00654 private: int field_count;
00655 private: Field *fields;
00656 private: uint32_t default_host;
00657 private: uint32_t default_robot;
00658
00659
00660 private: double unit_length;
00661 private: double unit_angle;
00662 };
00663
00664 #endif