NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
config_reader.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 // Code taken from www.cs.cmu.edu/~coral/projects/localization/source.html
20 // Brian Coltin is in the team and said this code could be used for the project
21 
22 #ifndef CONFIG_READER_CONFIG_READER_H_
23 #define CONFIG_READER_CONFIG_READER_H_
24 
25 extern "C" {
26 #include <lua.h>
27 #include <lualib.h>
28 #include <lauxlib.h>
29 }
30 
31 #include <glog/logging.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 
38 #include <cmath>
39 #include <functional>
40 #include <string>
41 #include <vector>
42 
43 #include "ff_common/init.h"
45 
46 namespace config_reader {
47 
48 class ConfigReader {
49  public:
50  enum FileFlags {
51  Optional = 1<<0, // file need not be present for success
52  };
53 
54  class Table {
55  friend class ConfigReader;
56  public:
57  Table();
58  Table(ConfigReader *c, const char *exp);
59  Table(Table *t, const char *exp);
60  Table(Table *t, int index);
61  bool IsInit();
62  bool CheckValExists(const char *exp);
63 
64  int GetSize(); // Only works for tables that are arrays
65  bool GetTable(const char *exp, Table *table);
66  bool GetTable(int index, Table *table);
67  bool GetStr(const char *exp, std::string *str);
68  bool GetStr(int index, std::string *str);
69  bool GetBool(const char *exp, bool *val);
70  bool GetBool(int index, bool *val);
71  bool GetInt(const char *exp, int *val);
72  bool GetInt(int index, int *val);
73  bool GetLongLong(const char *exp, int64_t *val);
74  bool GetLongLong(int index, int64_t *val);
75  bool GetUInt(const char *exp, unsigned int *val);
76  bool GetUInt(int index, unsigned int *val);
77  bool GetReal(const char *exp, float *val);
78  bool GetReal(int index, float *val);
79  bool GetReal(const char *exp, double *val);
80  bool GetReal(int index, double *val);
81  bool GetPosReal(const char *exp, float *val);
82  bool GetPosReal(int index, float *val);
83  bool GetPosReal(const char *exp, double *val);
84  bool GetPosReal(int index, double *val);
85  bool GetInt(const char *exp, int *val, int min, int max);
86  bool GetInt(int index, int *val, int min, int max);
87  bool GetReal(const char *exp, float *val, float min, float max);
88  bool GetReal(int index, float *val, float min, float max);
89  bool GetReal(const char *exp, double *val, double min, double max);
90  bool GetReal(int index, double *val, double min, double max);
91  bool IsNumber(const char *exp);
92  ConfigReader *Config() { return config_; }
93 
94  private:
95  void Init(ConfigReader *c, int ref, const char *exp, int size);
96 
97  ConfigReader *config_;
98  int ref_;
99  std::string full_exp_;
100  int size_;
101  };
102 
103  ConfigReader();
104  explicit ConfigReader(const char* path);
105  ~ConfigReader();
106 
107  void Close();
108  void Reset();
109  bool IsOpen();
110  void SetPath(const char* path);
111  void AddFile(const char *filename, unsigned flags = 0);
112  bool ReadFiles();
113  bool IsFileModified();
114  void CheckFilesUpdated(std::function<void(void)>);
115  bool CheckValExists(const char *exp);
116 
117  bool GetTable(const char *exp, Table *table);
118  bool GetStr(const char *exp, std::string *str);
119  bool GetBool(const char *exp, bool *val);
120  bool GetInt(const char *exp, int *val);
121  bool GetLongLong(const char *exp, int64_t *val);
122  bool GetUInt(const char *exp, unsigned int *val);
123  bool GetReal(const char *exp, float *val);
124  bool GetReal(const char *exp, double *val);
125  bool GetPosReal(const char *exp, float *val);
126  bool GetPosReal(const char *exp, double *val);
127  bool GetInt(const char *exp, int *val, int min, int max);
128  bool GetReal(const char *exp, float *val, float min, float max);
129  bool GetReal(const char *exp, double *val, double min, double max);
130 
131  protected:
132  class FileHeader{
133  public:
134  std::string filename_; // name of file
135  unsigned flags_; // flags from FileFlags
136  WatchFiles::Watch watch_; // file modification watch
138  FileHeader(const FileHeader &fh);
139  };
140 
141 
142  bool InitLua();
143  void CloseLua();
144  void ClearWatches();
145  void ShowError(int err_code, const char *filename);
146  bool ReadFile(const char *filename, unsigned flags);
147  void AddImports();
148  void AddStandard();
149  bool PutObjectOnLuaStack(const char *exp);
150  bool PutObjectOnLuaStack(const char *exp, int ref);
151  bool PutObjectOnLuaStack(int index, int ref);
152  bool IsTopValid();
153  void OutputGetValueError(const char *exp, const char *type);
154 
155  bool ReadTable(const char *exp, Table *table);
156  bool ReadStr(const char *exp, std::string *str);
157  bool ReadBool(const char *exp, bool *val);
158  bool ReadInt(const char *exp, int *val);
159  bool ReadLongLong(const char *exp, int64_t *val);
160  bool ReadUInt(const char *exp, unsigned int *val);
161  bool ReadReal(const char *exp, float *val);
162  bool ReadReal(const char *exp, double *val);
163  bool ReadPosReal(const char *exp, float *val);
164  bool ReadPosReal(const char *exp, double *val);
165  bool ReadInt(const char *exp, int *val, int min, int max);
166  bool ReadReal(const char *exp, float *val, float min, float max);
167  bool ReadReal(const char *exp, double *val, double min, double max);
168  bool IsNumber();
169 
170  std::vector<FileHeader> files_; // list of config files to load
171  lua_State * l_; // lua interpreter (valid between readFiles() and clear())
172  WatchFiles watch_files_; // class used to monitor files for changes
173  bool modified_; // true if config files need to be re-read
174  std::string path_;
175 };
176 
177 bool FileExists(const char *filename);
178 
179 } // namespace config_reader
180 
181 #endif // CONFIG_READER_CONFIG_READER_H_
config_reader::ConfigReader::Table::IsInit
bool IsInit()
Definition: config_reader.cc:96
config_reader::ConfigReader::Table::GetUInt
bool GetUInt(const char *exp, unsigned int *val)
Definition: config_reader.cc:248
config_reader::ConfigReader::GetUInt
bool GetUInt(const char *exp, unsigned int *val)
Definition: config_reader.cc:626
watch_files.h
config_reader::ConfigReader::FileHeader::watch_
WatchFiles::Watch watch_
Definition: config_reader.h:136
config_reader::ConfigReader::~ConfigReader
~ConfigReader()
Definition: config_reader.cc:490
config_reader
Definition: config_reader.h:46
config_reader::ConfigReader::ClearWatches
void ClearWatches()
Definition: config_reader.cc:717
config_reader::ConfigReader::ReadFiles
bool ReadFiles()
Definition: config_reader.cc:530
config_reader::ConfigReader::ReadPosReal
bool ReadPosReal(const char *exp, float *val)
Definition: config_reader.cc:1019
config_reader::ConfigReader::GetTable
bool GetTable(const char *exp, Table *table)
Definition: config_reader.cc:586
config_reader::ConfigReader::files_
std::vector< FileHeader > files_
Definition: config_reader.h:170
config_reader::ConfigReader::Table::IsNumber
bool IsNumber(const char *exp)
Definition: config_reader.cc:452
config_reader::ConfigReader::FileFlags
FileFlags
Definition: config_reader.h:50
config_reader::ConfigReader::GetPosReal
bool GetPosReal(const char *exp, float *val)
Definition: config_reader.cc:650
config_reader::ConfigReader::IsFileModified
bool IsFileModified()
Definition: config_reader.cc:557
config_reader::ConfigReader::CheckFilesUpdated
void CheckFilesUpdated(std::function< void(void)>)
Definition: config_reader.cc:567
config_reader::ConfigReader::PutObjectOnLuaStack
bool PutObjectOnLuaStack(const char *exp)
Definition: config_reader.cc:834
config_reader::ConfigReader::GetStr
bool GetStr(const char *exp, std::string *str)
Definition: config_reader.cc:594
config_reader::ConfigReader::ReadFile
bool ReadFile(const char *filename, unsigned flags)
Definition: config_reader.cc:747
config_reader::ConfigReader::watch_files_
WatchFiles watch_files_
Definition: config_reader.h:172
config_reader::WatchFiles
Definition: watch_files.h:63
config_reader::ConfigReader::IsOpen
bool IsOpen()
Definition: config_reader.cc:505
config_reader::ConfigReader::ShowError
void ShowError(int err_code, const char *filename)
Definition: config_reader.cc:723
config_reader::ConfigReader::Table::GetPosReal
bool GetPosReal(const char *exp, float *val)
Definition: config_reader.cc:323
config_reader::ConfigReader::OutputGetValueError
void OutputGetValueError(const char *exp, const char *type)
Definition: config_reader.cc:906
config_reader::ConfigReader::GetBool
bool GetBool(const char *exp, bool *val)
Definition: config_reader.cc:602
config_reader::FileExists
bool FileExists(const char *filename)
Definition: config_reader.cc:41
config_reader::ConfigReader::Optional
@ Optional
Definition: config_reader.h:51
config_reader::ConfigReader::modified_
bool modified_
Definition: config_reader.h:173
config_reader::ConfigReader::ReadLongLong
bool ReadLongLong(const char *exp, int64_t *val)
Definition: config_reader.cc:967
config_reader::ConfigReader::Table::GetTable
bool GetTable(const char *exp, Table *table)
Definition: config_reader.cc:123
config_reader::ConfigReader::FileHeader::filename_
std::string filename_
Definition: config_reader.h:134
config_reader::ConfigReader::AddFile
void AddFile(const char *filename, unsigned flags=0)
Definition: config_reader.cc:517
init.h
config_reader::ConfigReader::InitLua
bool InitLua()
Definition: config_reader.cc:691
config_reader::ConfigReader::Table::CheckValExists
bool CheckValExists(const char *exp)
Definition: config_reader.cc:105
config_reader::ConfigReader::ReadStr
bool ReadStr(const char *exp, std::string *str)
Definition: config_reader.cc:931
config_reader::ConfigReader::Table::Config
ConfigReader * Config()
Definition: config_reader.h:92
config_reader::ConfigReader::FileHeader
Definition: config_reader.h:132
config_reader::ConfigReader::GetLongLong
bool GetLongLong(const char *exp, int64_t *val)
Definition: config_reader.cc:618
config_reader::ConfigReader::Table::GetSize
int GetSize()
Definition: config_reader.cc:119
config_reader::WatchFiles::Watch
Definition: watch_files.h:68
config_reader::ConfigReader::FileHeader::FileHeader
FileHeader()
Definition: config_reader.h:137
config_reader::ConfigReader::Table::GetInt
bool GetInt(const char *exp, int *val)
Definition: config_reader.cc:198
Init
bool Init(const ros::NodeHandle &nh)
Definition: fam_cmd_i2c_node.cc:62
config_reader::ConfigReader
Definition: config_reader.h:48
config_reader::ConfigReader::Reset
void Reset()
Definition: config_reader.cc:498
config_reader::ConfigReader::path_
std::string path_
Definition: config_reader.h:174
config_reader::ConfigReader::ReadUInt
bool ReadUInt(const char *exp, unsigned int *val)
Definition: config_reader.cc:979
config_reader::ConfigReader::ReadInt
bool ReadInt(const char *exp, int *val)
Definition: config_reader.cc:955
config_reader::ConfigReader::Table::GetReal
bool GetReal(const char *exp, float *val)
Definition: config_reader.cc:273
config_reader::ConfigReader::Table::GetStr
bool GetStr(const char *exp, std::string *str)
Definition: config_reader.cc:148
config_reader::ConfigReader::ReadReal
bool ReadReal(const char *exp, float *val)
Definition: config_reader.cc:995
config_reader::ConfigReader::IsNumber
bool IsNumber()
Definition: config_reader.cc:1111
config_reader::ConfigReader::Table::GetLongLong
bool GetLongLong(const char *exp, int64_t *val)
Definition: config_reader.cc:223
config_reader::ConfigReader::l_
lua_State * l_
Definition: config_reader.h:171
config_reader::ConfigReader::CloseLua
void CloseLua()
Definition: config_reader.cc:710
config_reader::ConfigReader::ReadBool
bool ReadBool(const char *exp, bool *val)
Definition: config_reader.cc:943
config_reader::ConfigReader::Table
Definition: config_reader.h:54
config_reader::ConfigReader::Table::Table
Table()
Definition: config_reader.cc:47
config_reader::ConfigReader::GetReal
bool GetReal(const char *exp, float *val)
Definition: config_reader.cc:634
config_reader::ConfigReader::ConfigReader
ConfigReader()
Definition: config_reader.cc:473
config_reader::ConfigReader::IsTopValid
bool IsTopValid()
Definition: config_reader.cc:892
config_reader::ConfigReader::Table::GetBool
bool GetBool(const char *exp, bool *val)
Definition: config_reader.cc:173
config_reader::ConfigReader::SetPath
void SetPath(const char *path)
Definition: config_reader.cc:509
config_reader::ConfigReader::ReadTable
bool ReadTable(const char *exp, Table *table)
Definition: config_reader.cc:916
config_reader::ConfigReader::AddStandard
void AddStandard()
Definition: config_reader.cc:829
config_reader::ConfigReader::GetInt
bool GetInt(const char *exp, int *val)
Definition: config_reader.cc:610
config_reader::ConfigReader::CheckValExists
bool CheckValExists(const char *exp)
Definition: config_reader.cc:578
config_reader::ConfigReader::Close
void Close()
Definition: config_reader.cc:494
config_reader::ConfigReader::AddImports
void AddImports()
Definition: config_reader.cc:770
config_reader::ConfigReader::FileHeader::flags_
unsigned flags_
Definition: config_reader.h:135