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 #ifndef CONFFILE_H
00044 #define CONFFILE_H
00045
00046 #if defined (WIN32)
00047 #if defined (PLAYER_STATIC)
00048 #define PLAYERCORE_EXPORT
00049 #elif defined (playercore_EXPORTS)
00050 #define PLAYERCORE_EXPORT __declspec (dllexport)
00051 #else
00052 #define PLAYERCORE_EXPORT __declspec (dllimport)
00053 #endif
00054 #else
00055 #define PLAYERCORE_EXPORT
00056 #endif
00057
00058 #include <stdio.h>
00059
00060 #include <libplayerinterface/player.h>
00061
00196 class PLAYERCORE_EXPORT ConfigFile
00197 {
00199 public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00200
00202 public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00203
00205 public: ConfigFile();
00206
00208 public: ~ConfigFile();
00209
00211 private: void InitFields();
00212
00216 public: bool Load(const char *filename);
00217
00226 public: void InsertFieldValue(int index,
00227 const char* name,
00228 const char* value);
00229
00230
00231
00232 private: bool Save(const char *filename);
00233
00236 public: bool WarnUnused();
00237
00243 public: bool ReadBool(int section, const char *name, bool value);
00244
00245
00246 private: void WriteBool(int section, const char* name, bool value);
00247
00248
00249 private: void WriteBool_Compat(int section, const char* name, bool value);
00250
00251
00257 public: const char *ReadString(int section,
00258 const char *name,
00259 const char *value);
00260
00261
00262 private: void WriteString(int section,
00263 const char *name,
00264 const char *value);
00265
00271 public: int ReadInt(int section,
00272 const char *name,
00273 int value);
00274
00275
00276 private: void WriteInt(int section,
00277 const char *name,
00278 int value);
00279
00285 public: double ReadFloat(int section,
00286 const char *name,
00287 double value);
00288
00289
00290 private: void WriteFloat(int section,
00291 const char *name,
00292 double value);
00293
00299 public: double ReadLength(int section,
00300 const char *name,
00301 double value);
00302
00303
00304 private: void WriteLength(int section,
00305 const char *name,
00306 double value);
00307
00317 public: double ReadAngle(int section, const char *name, double value);
00318
00329 public: uint32_t ReadColor(int section,
00330 const char *name,
00331 uint32_t value);
00332
00342 public: const char *ReadFilename(int section,
00343 const char *name,
00344 const char *value);
00345
00349 public: int GetTupleCount(int section, const char *name);
00350
00357 public: const char *ReadTupleString(int section,
00358 const char *name,
00359 int index,
00360 const char *value);
00361
00362
00363 private: void WriteTupleString(int section,
00364 const char *name,
00365 int index,
00366 const char *value);
00367
00374 public: int ReadTupleInt(int section,
00375 const char *name,
00376 int index,
00377 int value);
00378
00379
00380 private: void WriteTupleInt(int section,
00381 const char *name,
00382 int index,
00383 int value);
00384
00385
00392 public: double ReadTupleFloat(int section,
00393 const char *name,
00394 int index,
00395 double value);
00396
00397
00398 private: void WriteTupleFloat(int section,
00399 const char *name,
00400 int index,
00401 double value);
00402
00409 public: double ReadTupleLength(int section,
00410 const char *name,
00411 int index,
00412 double value);
00413
00414
00415 private: void WriteTupleLength(int section,
00416 const char *name,
00417 int index,
00418 double value);
00419
00430 public: double ReadTupleAngle(int section,
00431 const char *name,
00432 int index,
00433 double value);
00434
00435
00436 private: void WriteTupleAngle(int section,
00437 const char *name,
00438 int index,
00439 double value);
00440
00452 public: uint32_t ReadTupleColor(int section,
00453 const char *name,
00454 int index,
00455 uint32_t value);
00456
00461
00469 public: int ReadDeviceAddr(player_devaddr_t* addr, int section,
00470 const char *name, int code, int index,
00471 const char *key);
00472
00473
00474 public: bool ParseDriver(int section);
00475
00476
00477 public: bool ParseInterface(int section);
00478
00479
00480 public: bool ParseAllDrivers();
00481
00482
00483 public: bool ParseAllInterfaces();
00484
00486 public: int GetSectionCount();
00487
00489 public: const char *GetSectionType(int section);
00490
00493 public: int LookupSection(const char *type);
00494
00497 public: int GetSectionParent(int section);
00498
00499
00501
00502
00503
00504 private: bool LoadTokens(FILE *file, int include);
00505
00506
00507 private: bool LoadTokenComment(FILE *file, int *line, int include);
00508
00509
00510 private: bool LoadTokenWord(FILE *file, int *line, int include);
00511
00512
00513 private: bool LoadTokenInclude(FILE *file, int *line, int include);
00514
00515
00516 private: bool LoadTokenNum(FILE *file, int *line, int include);
00517
00518
00519 private: bool LoadTokenString(FILE *file, int *line, int include);
00520
00521
00522 private: bool LoadTokenSpace(FILE *file, int *line, int include);
00523
00524
00525 private: bool SaveTokens(FILE *file);
00526
00527
00528 private: void ClearTokens();
00529
00530
00531 private: bool AddToken(int type, const char *value, int include);
00532
00533
00534 private: bool SetTokenValue(int index, const char *value);
00535
00536
00537 private: const char *GetTokenValue(int index);
00538
00540 public: void DumpTokens();
00541
00542
00543 private: bool ParseTokens();
00544
00545
00546 private: bool ParseTokenInclude(int *index, int *line);
00547
00548
00549 private: bool ParseTokenDefine(int *index, int *line);
00550
00551
00552 private: bool ParseTokenPlugin(int *index, int *line);
00553
00554
00555 private: bool ParseTokenWord(int section, int *index, int *line);
00556
00557
00558 private: bool ParseTokenSection(int section, int *index, int *line);
00559
00560
00561 private: bool ParseTokenField(int section, int *index, int *line);
00562
00563
00564 private: bool ParseTokenTuple(int section, int field,
00565 int *index, int *line);
00566
00567
00568 private: void ClearMacros();
00569
00570
00571 private: int AddMacro(const char *macroname, const char *sectionname,
00572 int line, int starttoken, int endtoken);
00573
00574
00575
00576 private: int LookupMacro(const char *macroname);
00577
00578
00579 private: void DumpMacros();
00580
00581
00582 private: void ClearSections();
00583
00584
00585 private: int AddSection(int parent, const char *type);
00586
00588 public: void DumpSections();
00589
00590
00591 private: void ClearFields();
00592
00593
00594 private: int AddField(int section, const char *name, int line);
00595
00596
00597 private: void AddFieldValue(int field, int index, int value_token);
00598
00599
00600 private: int GetField(int section, const char *name);
00601
00602
00603 private: int GetFieldValueCount(int field);
00604
00605
00606
00607 private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00608
00609
00610 private: void SetFieldValue(int field, int index, const char *value);
00611
00613 public: void DumpFields();
00614
00615
00616 private: uint32_t LookupColor(const char *name);
00617
00619 public: char *filename;
00620
00621
00622 private: enum
00623 {
00624 TokenComment,
00625 TokenWord, TokenNum, TokenString, TokenBool,
00626 TokenOpenSection, TokenCloseSection,
00627 TokenOpenTuple, TokenCloseTuple,
00628 TokenSpace, TokenEOL
00629 };
00630
00631
00632 private: struct Token
00633 {
00634
00635 int include;
00636
00637
00638 int type;
00639
00640
00641 char *value;
00642 };
00643
00644
00645
00646 private: int token_size, token_count;
00647 private: Token *tokens;
00648
00649
00650 private: struct CMacro
00651 {
00652
00653 const char *macroname;
00654
00655
00656 const char *sectionname;
00657
00658
00659 int line;
00660
00661
00662 int starttoken, endtoken;
00663 };
00664
00665
00666 private: int macro_size;
00667 private: int macro_count;
00668 private: CMacro *macros;
00669
00670
00671 private: struct Section
00672 {
00673
00674 int parent;
00675
00676
00677 const char *type;
00678 };
00679
00680
00681 private: int section_size;
00682 private: int section_count;
00683 private: Section *sections;
00684
00685
00686 private: struct Field
00687 {
00688
00689 int section;
00690
00691
00692 const char *name;
00693
00694
00695 int value_count;
00696 int *values;
00697
00698
00699 bool *useds;
00700
00701
00702 int line;
00703 };
00704
00705
00706 private: int field_size;
00707 private: int field_count;
00708 private: Field *fields;
00709 private: uint32_t default_host;
00710 private: uint32_t default_robot;
00711
00712
00713 private: double unit_length;
00714 private: double unit_angle;
00715 };
00716
00717 #endif