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 #include <stdio.h>
00047
00048 #include <libplayercore/player.h>
00049
00184 class ConfigFile
00185 {
00187 public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00188
00190 public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00191
00193 public: ConfigFile();
00194
00196 public: ~ConfigFile();
00197
00199 private: void InitFields();
00200
00204 public: bool Load(const char *filename);
00205
00214 public: void InsertFieldValue(int index,
00215 const char* name,
00216 const char* value);
00217
00218
00219
00220 private: bool Save(const char *filename);
00221
00224 public: bool WarnUnused();
00225
00231 public: bool ReadBool(int section, const char *name, bool value);
00232
00233
00234 private: void WriteBool(int section, const char* name, bool value);
00235
00236
00237 private: void WriteBool_Compat(int section, const char* name, bool value);
00238
00239
00245 public: const char *ReadString(int section,
00246 const char *name,
00247 const char *value);
00248
00249
00250 private: void WriteString(int section,
00251 const char *name,
00252 const char *value);
00253
00259 public: int ReadInt(int section,
00260 const char *name,
00261 int value);
00262
00263
00264 private: void WriteInt(int section,
00265 const char *name,
00266 int value);
00267
00273 public: double ReadFloat(int section,
00274 const char *name,
00275 double value);
00276
00277
00278 private: void WriteFloat(int section,
00279 const char *name,
00280 double value);
00281
00287 public: double ReadLength(int section,
00288 const char *name,
00289 double value);
00290
00291
00292 private: void WriteLength(int section,
00293 const char *name,
00294 double value);
00295
00305 public: double ReadAngle(int section, const char *name, double value);
00306
00317 public: uint32_t ReadColor(int section,
00318 const char *name,
00319 uint32_t value);
00320
00330 public: const char *ReadFilename(int section,
00331 const char *name,
00332 const char *value);
00333
00337 public: int GetTupleCount(int section, const char *name);
00338
00345 public: const char *ReadTupleString(int section,
00346 const char *name,
00347 int index,
00348 const char *value);
00349
00350
00351 private: void WriteTupleString(int section,
00352 const char *name,
00353 int index,
00354 const char *value);
00355
00362 public: int ReadTupleInt(int section,
00363 const char *name,
00364 int index,
00365 int value);
00366
00367
00368 private: void WriteTupleInt(int section,
00369 const char *name,
00370 int index,
00371 int value);
00372
00373
00380 public: double ReadTupleFloat(int section,
00381 const char *name,
00382 int index,
00383 double value);
00384
00385
00386 private: void WriteTupleFloat(int section,
00387 const char *name,
00388 int index,
00389 double value);
00390
00397 public: double ReadTupleLength(int section,
00398 const char *name,
00399 int index,
00400 double value);
00401
00402
00403 private: void WriteTupleLength(int section,
00404 const char *name,
00405 int index,
00406 double value);
00407
00418 public: double ReadTupleAngle(int section,
00419 const char *name,
00420 int index,
00421 double value);
00422
00423
00424 private: void WriteTupleAngle(int section,
00425 const char *name,
00426 int index,
00427 double value);
00428
00440 public: uint32_t ReadTupleColor(int section,
00441 const char *name,
00442 int index,
00443 uint32_t value);
00444
00449
00457 public: int ReadDeviceAddr(player_devaddr_t* addr, int section,
00458 const char *name, int code, int index,
00459 const char *key);
00460
00461
00462 public: bool ParseDriver(int section);
00463
00464
00465 public: bool ParseInterface(int section);
00466
00467
00468 public: bool ParseAllDrivers();
00469
00470
00471 public: bool ParseAllInterfaces();
00472
00474 public: int GetSectionCount();
00475
00477 public: const char *GetSectionType(int section);
00478
00481 public: int LookupSection(const char *type);
00482
00485 public: int GetSectionParent(int section);
00486
00487
00489
00490
00491
00492 private: bool LoadTokens(FILE *file, int include);
00493
00494
00495 private: bool LoadTokenComment(FILE *file, int *line, int include);
00496
00497
00498 private: bool LoadTokenWord(FILE *file, int *line, int include);
00499
00500
00501 private: bool LoadTokenInclude(FILE *file, int *line, int include);
00502
00503
00504 private: bool LoadTokenNum(FILE *file, int *line, int include);
00505
00506
00507 private: bool LoadTokenString(FILE *file, int *line, int include);
00508
00509
00510 private: bool LoadTokenSpace(FILE *file, int *line, int include);
00511
00512
00513 private: bool SaveTokens(FILE *file);
00514
00515
00516 private: void ClearTokens();
00517
00518
00519 private: bool AddToken(int type, const char *value, int include);
00520
00521
00522 private: bool SetTokenValue(int index, const char *value);
00523
00524
00525 private: const char *GetTokenValue(int index);
00526
00528 public: void DumpTokens();
00529
00530
00531 private: bool ParseTokens();
00532
00533
00534 private: bool ParseTokenInclude(int *index, int *line);
00535
00536
00537 private: bool ParseTokenDefine(int *index, int *line);
00538
00539
00540 private: bool ParseTokenPlugin(int *index, int *line);
00541
00542
00543 private: bool ParseTokenWord(int section, int *index, int *line);
00544
00545
00546 private: bool ParseTokenSection(int section, int *index, int *line);
00547
00548
00549 private: bool ParseTokenField(int section, int *index, int *line);
00550
00551
00552 private: bool ParseTokenTuple(int section, int field,
00553 int *index, int *line);
00554
00555
00556 private: void ClearMacros();
00557
00558
00559 private: int AddMacro(const char *macroname, const char *sectionname,
00560 int line, int starttoken, int endtoken);
00561
00562
00563
00564 private: int LookupMacro(const char *macroname);
00565
00566
00567 private: void DumpMacros();
00568
00569
00570 private: void ClearSections();
00571
00572
00573 private: int AddSection(int parent, const char *type);
00574
00576 public: void DumpSections();
00577
00578
00579 private: void ClearFields();
00580
00581
00582 private: int AddField(int section, const char *name, int line);
00583
00584
00585 private: void AddFieldValue(int field, int index, int value_token);
00586
00587
00588 private: int GetField(int section, const char *name);
00589
00590
00591 private: int GetFieldValueCount(int field);
00592
00593
00594
00595 private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00596
00597
00598 private: void SetFieldValue(int field, int index, const char *value);
00599
00601 public: void DumpFields();
00602
00603
00604 private: uint32_t LookupColor(const char *name);
00605
00607 public: char *filename;
00608
00609
00610 private: enum
00611 {
00612 TokenComment,
00613 TokenWord, TokenNum, TokenString, TokenBool,
00614 TokenOpenSection, TokenCloseSection,
00615 TokenOpenTuple, TokenCloseTuple,
00616 TokenSpace, TokenEOL
00617 };
00618
00619
00620 private: struct Token
00621 {
00622
00623 int include;
00624
00625
00626 int type;
00627
00628
00629 char *value;
00630 };
00631
00632
00633
00634 private: int token_size, token_count;
00635 private: Token *tokens;
00636
00637
00638 private: struct CMacro
00639 {
00640
00641 const char *macroname;
00642
00643
00644 const char *sectionname;
00645
00646
00647 int line;
00648
00649
00650 int starttoken, endtoken;
00651 };
00652
00653
00654 private: int macro_size;
00655 private: int macro_count;
00656 private: CMacro *macros;
00657
00658
00659 private: struct Section
00660 {
00661
00662 int parent;
00663
00664
00665 const char *type;
00666 };
00667
00668
00669 private: int section_size;
00670 private: int section_count;
00671 private: Section *sections;
00672
00673
00674 private: struct Field
00675 {
00676
00677 int section;
00678
00679
00680 const char *name;
00681
00682
00683 int value_count;
00684 int *values;
00685
00686
00687 bool *useds;
00688
00689
00690 int line;
00691 };
00692
00693
00694 private: int field_size;
00695 private: int field_count;
00696 private: Field *fields;
00697 private: uint32_t default_host;
00698 private: uint32_t default_robot;
00699
00700
00701 private: double unit_length;
00702 private: double unit_angle;
00703 };
00704
00705 #endif