configfile.h
/*
* Player - One Hell of a Robot Server
* Copyright (C) 2000
* Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/********************************************************************
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
********************************************************************/
/*
* $Id: configfile.h 8004 2009-07-13 14:03:44Z thjc $
*/
#ifndef CONFFILE_H
#define CONFFILE_H
#if defined (WIN32)
#if defined (PLAYER_STATIC)
#define PLAYERCORE_EXPORT
#elif defined (playercore_EXPORTS)
#define PLAYERCORE_EXPORT __declspec (dllexport)
#else
#define PLAYERCORE_EXPORT __declspec (dllimport)
#endif
#else
#define PLAYERCORE_EXPORT
#endif
#include <stdio.h>
#include <libplayerinterface/player.h>
class PLAYERCORE_EXPORT ConfigFile
{
public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
public: ConfigFile(const char* _default_host, uint32_t _default_robot);
public: ConfigFile();
public: ~ConfigFile();
private: void InitFields();
public: bool Load(const char *filename);
public: void InsertFieldValue(int index,
const char* name,
const char* value);
// Save config back into file
// Set filename to NULL to save back into the original file
private: bool Save(const char *filename);
public: bool WarnUnused();
public: bool ReadBool(int section, const char *name, bool value);
// Write a bool as "yes" or "no"
private: void WriteBool(int section, const char* name, bool value);
// Write a bool as "1" or "0" (for backward compatability)
private: void WriteBool_Compat(int section, const char* name, bool value);
public: const char *ReadString(int section,
const char *name,
const char *value);
// Write a string
private: void WriteString(int section,
const char *name,
const char *value);
public: int ReadInt(int section,
const char *name,
int value);
// Write an integer
private: void WriteInt(int section,
const char *name,
int value);
public: double ReadFloat(int section,
const char *name,
double value);
// Write a float
private: void WriteFloat(int section,
const char *name,
double value);
public: double ReadLength(int section,
const char *name,
double value);
// Write a length (includes units conversion)
private: void WriteLength(int section,
const char *name,
double value);
public: double ReadAngle(int section, const char *name, double value);
public: uint32_t ReadColor(int section,
const char *name,
uint32_t value);
public: const char *ReadFilename(int section,
const char *name,
const char *value);
public: int GetTupleCount(int section, const char *name);
public: const char *ReadTupleString(int section,
const char *name,
int index,
const char *value);
// Write a string to a tuple
private: void WriteTupleString(int section,
const char *name,
int index,
const char *value);
public: int ReadTupleInt(int section,
const char *name,
int index,
int value);
// Write a int to a tuple
private: void WriteTupleInt(int section,
const char *name,
int index,
int value);
public: double ReadTupleFloat(int section,
const char *name,
int index,
double value);
// Write a float to a tuple
private: void WriteTupleFloat(int section,
const char *name,
int index,
double value);
public: double ReadTupleLength(int section,
const char *name,
int index,
double value);
// Write a to a tuple length (includes units conversion)
private: void WriteTupleLength(int section,
const char *name,
int index,
double value);
public: double ReadTupleAngle(int section,
const char *name,
int index,
double value);
// Write an angle to a tuple (includes units conversion)
private: void WriteTupleAngle(int section,
const char *name,
int index,
double value);
public: uint32_t ReadTupleColor(int section,
const char *name,
int index,
uint32_t value);
//
public: int ReadDeviceAddr(player_devaddr_t* addr, int section,
const char *name, int code, int index,
const char *key);
// Parse a driver block, and update the deviceTable accordingly
public: bool ParseDriver(int section);
// Parse an interface block, and update the interface systems accordingly
public: bool ParseInterface(int section);
// Parse all driver blocks
public: bool ParseAllDrivers();
// Parse all interface blocks
public: bool ParseAllInterfaces();
public: int GetSectionCount();
public: const char *GetSectionType(int section);
public: int LookupSection(const char *type);
public: int GetSectionParent(int section);
// Private methods used to load stuff from the config file
// Load tokens from a file.
private: bool LoadTokens(FILE *file, int include);
// Read in a comment token
private: bool LoadTokenComment(FILE *file, int *line, int include);
// Read in a word token
private: bool LoadTokenWord(FILE *file, int *line, int include);
// Load an include token; this will load the include file.
private: bool LoadTokenInclude(FILE *file, int *line, int include);
// Read in a number token
private: bool LoadTokenNum(FILE *file, int *line, int include);
// Read in a string token
private: bool LoadTokenString(FILE *file, int *line, int include);
// Read in a whitespace token
private: bool LoadTokenSpace(FILE *file, int *line, int include);
// Save tokens to a file.
private: bool SaveTokens(FILE *file);
// Clear the token list
private: void ClearTokens();
// Add a token to the token list
private: bool AddToken(int type, const char *value, int include);
// Set a token in the token list
private: bool SetTokenValue(int index, const char *value);
// Get the value of a token
private: const char *GetTokenValue(int index);
public: void DumpTokens();
// Parse a line
private: bool ParseTokens();
// Parse an include statement
private: bool ParseTokenInclude(int *index, int *line);
// Parse a macro definition
private: bool ParseTokenDefine(int *index, int *line);
// Parse a macro definition
private: bool ParseTokenPlugin(int *index, int *line);
// Parse an word (could be a section or an field) from the token list.
private: bool ParseTokenWord(int section, int *index, int *line);
// Parse a section from the token list.
private: bool ParseTokenSection(int section, int *index, int *line);
// Parse an field from the token list.
private: bool ParseTokenField(int section, int *index, int *line);
// Parse a tuple.
private: bool ParseTokenTuple(int section, int field,
int *index, int *line);
// Clear the macro list
private: void ClearMacros();
// Add a macro
private: int AddMacro(const char *macroname, const char *sectionname,
int line, int starttoken, int endtoken);
// Lookup a macro by name
// Returns -1 if there is no macro with this name.
private: int LookupMacro(const char *macroname);
// Dump the macro list for debugging
private: void DumpMacros();
// Clear the section list
private: void ClearSections();
// Add a section
private: int AddSection(int parent, const char *type);
public: void DumpSections();
// Clear the field list
private: void ClearFields();
// Add a field
private: int AddField(int section, const char *name, int line);
// Add a field value.
private: void AddFieldValue(int field, int index, int value_token);
// Get a field
private: int GetField(int section, const char *name);
// Get the number of elements for this field
private: int GetFieldValueCount(int field);
// Get the value of an field element
// Set flag_used to true mark the field element as read.
private: const char *GetFieldValue(int field, int index, bool flag_used = true);
// Set the value of an field.
private: void SetFieldValue(int field, int index, const char *value);
public: void DumpFields();
// Look up the color in a data based (transform color name -> color value).
private: uint32_t LookupColor(const char *name);
public: char *filename;
// Token types.
private: enum
{
TokenComment,
TokenWord, TokenNum, TokenString, TokenBool,
TokenOpenSection, TokenCloseSection,
TokenOpenTuple, TokenCloseTuple,
TokenSpace, TokenEOL
};
// Token structure.
private: struct Token
{
// Non-zero if token is from an include file.
int include;
// Token type (enumerated value).
int type;
// Token value
char *value;
};
// A list of tokens loaded from the file.
// Modified values are written back into the token list.
private: int token_size, token_count;
private: Token *tokens;
// Private macro class
private: struct CMacro
{
// Name of macro
const char *macroname;
// Name of section
const char *sectionname;
// Line the macro definition starts on.
int line;
// Range of tokens in the body of the macro definition.
int starttoken, endtoken;
};
// Macro list
private: int macro_size;
private: int macro_count;
private: CMacro *macros;
// Private section class
private: struct Section
{
// Parent section
int parent;
// Type of section (i.e. position, laser, etc).
const char *type;
};
// Section list
private: int section_size;
private: int section_count;
private: Section *sections;
// Private field class
private: struct Field
{
// Index of section this field belongs to
int section;
// Name of field
const char *name;
// A list of token indexes
int value_count;
int *values;
// Flag set if field value has been used
bool *useds;
// Line this field came from
int line;
};
// Field list
private: int field_size;
private: int field_count;
private: Field *fields;
private: uint32_t default_host;
private: uint32_t default_robot;
// Conversion units
private: double unit_length;
private: double unit_angle;
};
#endif
ConfigFile
Class for loading configuration file information.
Definition: configfile.h:196
ConfigFile::CMacro
Definition: configfile.h:650
player_devaddr
A device address.
Definition: player.h:141
ConfigFile::Section
Definition: configfile.h:671
ConfigFile::Field
Definition: configfile.h:686
ConfigFile::Token
Definition: configfile.h:632
ConfigFile::filename
char * filename
Name of the file we loaded.
Definition: configfile.h:619

Last updated 12 September 2005 21:38:45