configfile.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000
4  * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 /********************************************************************
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  *
38  ********************************************************************/
39 
40 /*
41  * $Id: configfile.h 9120 2013-01-07 00:18:52Z jpgr87 $
42  */
43 #ifndef CONFFILE_H
44 #define CONFFILE_H
45 
46 #if defined (WIN32)
47  #if defined (PLAYER_STATIC)
48  #define PLAYERCORE_EXPORT
49  #elif defined (playercore_EXPORTS)
50  #define PLAYERCORE_EXPORT __declspec (dllexport)
51  #else
52  #define PLAYERCORE_EXPORT __declspec (dllimport)
53  #endif
54 #else
55  #define PLAYERCORE_EXPORT
56 #endif
57 
58 #include <stdio.h>
59 
60 #include <libplayerinterface/player.h>
61 
196 class PLAYERCORE_EXPORT ConfigFile
197 {
199  public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
200 
202  public: ConfigFile(const char* _default_host, uint32_t _default_robot);
203 
205  public: ConfigFile();
206 
208  public: ~ConfigFile();
209 
211  private: void InitFields();
212 
216  public: bool Load(const char *filename);
217 
226  public: void InsertFieldValue(int index,
227  const char* name,
228  const char* value);
229 
230  // Save config back into file
231  // Set filename to NULL to save back into the original file
232  private: bool Save(const char *filename);
233 
236  public: bool WarnUnused();
237 
243  public: bool ReadBool(int section, const char *name, bool value);
244 
245  // Write a bool as "yes" or "no"
246  private: void WriteBool(int section, const char* name, bool value);
247 
248  // Write a bool as "1" or "0" (for backward compatability)
249  private: void WriteBool_Compat(int section, const char* name, bool value);
250 
251 
257  public: const char *ReadString(int section,
258  const char *name,
259  const char *value);
260 
261  // Write a string
262  private: void WriteString(int section,
263  const char *name,
264  const char *value);
265 
271  public: int ReadInt(int section,
272  const char *name,
273  int value);
274 
275  // Write an integer
276  private: void WriteInt(int section,
277  const char *name,
278  int value);
279 
285  public: double ReadFloat(int section,
286  const char *name,
287  double value);
288 
289  // Write a float
290  private: void WriteFloat(int section,
291  const char *name,
292  double value);
293 
299  public: double ReadLength(int section,
300  const char *name,
301  double value);
302 
303  // Write a length (includes units conversion)
304  private: void WriteLength(int section,
305  const char *name,
306  double value);
307 
317  public: double ReadAngle(int section, const char *name, double value);
318 
329  public: uint32_t ReadColor(int section,
330  const char *name,
331  uint32_t value);
332 
342  public: const char *ReadFilename(int section,
343  const char *name,
344  const char *value);
345 
349  public: int GetTupleCount(int section, const char *name);
350 
357  public: const char *ReadTupleString(int section,
358  const char *name,
359  int index,
360  const char *value);
361 
362  // Write a string to a tuple
363  private: void WriteTupleString(int section,
364  const char *name,
365  int index,
366  const char *value);
367 
374  public: int ReadTupleInt(int section,
375  const char *name,
376  int index,
377  int value);
378 
379  // Write a int to a tuple
380  private: void WriteTupleInt(int section,
381  const char *name,
382  int index,
383  int value);
384 
385 
392  public: double ReadTupleFloat(int section,
393  const char *name,
394  int index,
395  double value);
396 
397  // Write a float to a tuple
398  private: void WriteTupleFloat(int section,
399  const char *name,
400  int index,
401  double value);
402 
409  public: double ReadTupleLength(int section,
410  const char *name,
411  int index,
412  double value);
413 
414  // Write a to a tuple length (includes units conversion)
415  private: void WriteTupleLength(int section,
416  const char *name,
417  int index,
418  double value);
419 
430  public: double ReadTupleAngle(int section,
431  const char *name,
432  int index,
433  double value);
434 
435  // Write an angle to a tuple (includes units conversion)
436  private: void WriteTupleAngle(int section,
437  const char *name,
438  int index,
439  double value);
440 
452  public: uint32_t ReadTupleColor(int section,
453  const char *name,
454  int index,
455  uint32_t value);
456 
461  //
469  public: int ReadDeviceAddr(player_devaddr_t* addr, int section,
470  const char *name, int code, int index,
471  const char *key);
472 
473  // Parse a driver block, and update the deviceTable accordingly
474  public: bool ParseDriver(int section);
475 
476  // Parse an interface block, and update the interface systems accordingly
477  public: bool ParseInterface(int section);
478 
479  // Parse all driver blocks
480  public: bool ParseAllDrivers();
481 
482  // Parse all interface blocks
483  public: bool ParseAllInterfaces();
484 
486  public: int GetSectionCount();
487 
489  public: const char *GetSectionType(int section);
490 
493  public: int LookupSection(const char *type);
494 
497  public: int GetSectionParent(int section);
498 
499 
501  // Private methods used to load stuff from the config file
502 
503  // Load tokens from a file.
504  private: bool LoadTokens(FILE *file, int include);
505 
506  // Read in a comment token
507  private: bool LoadTokenComment(FILE *file, int *line, int include);
508 
509  // Read in a word token
510  private: bool LoadTokenWord(FILE *file, int *line, int include);
511 
512  // Load an include token; this will load the include file.
513  private: bool LoadTokenInclude(FILE *file, int *line, int include);
514 
515  // Read in a number token
516  private: bool LoadTokenNum(FILE *file, int *line, int include);
517 
518  // Read in a string token
519  private: bool LoadTokenString(FILE *file, int *line, int include);
520 
521  // Read in a whitespace token
522  private: bool LoadTokenSpace(FILE *file, int *line, int include);
523 
524  // Save tokens to a file.
525  private: bool SaveTokens(FILE *file);
526 
527  // Clear the token list
528  private: void ClearTokens();
529 
530  // Add a token to the token list
531  private: bool AddToken(int type, const char *value, int include);
532 
533  // Set a token in the token list
534  private: bool SetTokenValue(int index, const char *value);
535 
536  // Get the value of a token
537  private: const char *GetTokenValue(int index);
538 
540  public: void DumpTokens();
541 
542  // Parse a line
543  private: bool ParseTokens();
544 
545  // Parse an include statement
546  private: bool ParseTokenInclude(int *index, int *line);
547 
548  // Parse a macro definition
549  private: bool ParseTokenDefine(int *index, int *line);
550 
551  // Parse a macro definition
552  private: bool ParseTokenPlugin(int *index, int *line);
553 
554  // Parse an word (could be a section or an field) from the token list.
555  private: bool ParseTokenWord(int section, int *index, int *line);
556 
557  // Parse a section from the token list.
558  private: bool ParseTokenSection(int section, int *index, int *line);
559 
560  // Parse an field from the token list.
561  private: bool ParseTokenField(int section, int *index, int *line);
562 
563  // Parse a tuple.
564  private: bool ParseTokenTuple(int section, int field,
565  int *index, int *line);
566 
567  // Clear the macro list
568  private: void ClearMacros();
569 
570  // Add a macro
571  private: int AddMacro(const char *macroname, const char *sectionname,
572  int line, int starttoken, int endtoken);
573 
574  // Lookup a macro by name
575  // Returns -1 if there is no macro with this name.
576  private: int LookupMacro(const char *macroname);
577 
578  // Dump the macro list for debugging
579  private: void DumpMacros();
580 
581  // Clear the section list
582  private: void ClearSections();
583 
584  // Add a section
585  private: int AddSection(int parent, const char *type);
586 
588  public: void DumpSections();
589 
590  // Clear the field list
591  private: void ClearFields();
592 
593  // Add a field
594  private: int AddField(int section, const char *name, int line);
595 
596  // Add a field value.
597  private: void AddFieldValue(int field, int index, int value_token);
598 
599  // Get a field
600  private: int GetField(int section, const char *name);
601 
602  // Get the number of elements for this field
603  private: int GetFieldValueCount(int field);
604 
605  // Get the value of an field element
606  // Set flag_used to true mark the field element as read.
607  private: const char *GetFieldValue(int field, int index, bool flag_used = true);
608 
609  // Set the value of an field.
610  private: void SetFieldValue(int field, int index, const char *value);
611 
613  public: void DumpFields();
614 
615  // Look up the color in a data based (transform color name -> color value).
616  private: uint32_t LookupColor(const char *name);
617 
619  public: char *filename;
620 
621  // Token types.
622  private: enum
623  {
624  TokenComment,
625  TokenWord, TokenNum, TokenString, TokenBool,
626  TokenOpenSection, TokenCloseSection,
627  TokenOpenTuple, TokenCloseTuple,
628  TokenSpace, TokenEOL
629  };
630 
631  // Token structure.
632  private: struct Token
633  {
634  // Non-zero if token is from an include file.
635  int include;
636 
637  // Token type (enumerated value).
638  int type;
639 
640  // Token value
641  char *value;
642  };
643 
644  // A list of tokens loaded from the file.
645  // Modified values are written back into the token list.
646  private: int token_size, token_count;
647  private: Token *tokens;
648 
649  // Private macro class
650  private: struct CMacro
651  {
652  // Name of macro
653  const char *macroname;
654 
655  // Name of section
656  const char *sectionname;
657 
658  // Line the macro definition starts on.
659  int line;
660 
661  // Range of tokens in the body of the macro definition.
662  int starttoken, endtoken;
663  };
664 
665  // Macro list
666  private: int macro_size;
667  private: int macro_count;
668  private: CMacro *macros;
669 
670  // Private section class
671  private: struct Section
672  {
673  // Parent section
674  int parent;
675 
676  // Type of section (i.e. position, laser, etc).
677  const char *type;
678  };
679 
680  // Section list
681  private: int section_size;
682  private: int section_count;
683  private: Section *sections;
684 
685  // Private field class
686  private: struct Field
687  {
688  // Index of section this field belongs to
689  int section;
690 
691  // Name of field
692  const char *name;
693 
694  // A list of token indexes
695  int value_count;
696  int *values;
697 
698  // Flag set if field value has been used
699  bool *useds;
700 
701  // Line this field came from
702  int line;
703  };
704 
705  // Field list
706  private: int field_size;
707  private: int field_count;
708  private: Field *fields;
709  private: uint32_t default_host;
710  private: uint32_t default_robot;
711 
712  // Conversion units
713  private: double unit_length;
714  private: double unit_angle;
715 };
716 
717 #endif
Class for loading configuration file information.
Definition: configfile.h:196
Definition: configfile.h:650
A device address.
Definition: player.h:145
Definition: configfile.h:671
Definition: configfile.h:686
Definition: configfile.h:632
char * filename
Name of the file we loaded.
Definition: configfile.h:619