Fawkes API  Fawkes Development Version
xabsl_tools.cpp
1 
2 /***************************************************************************
3  * xabsl_tools.cpp - Tools required for XABSL
4  *
5  * Created: Wed Aug 06 17:25:51 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "xabsl_tools.h"
24 
25 #include <core/exception.h>
26 #include <utils/logging/logger.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 
31 /** @class XabslLoggingErrorHandler "xabsl_tools.h"
32  * Logging error handler for XABSL integration.
33  * Simple error handler that prints errors to the Fawkes log.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param logger Fawkes logger
39  */
41 {
42  __logger = logger;
43 }
44 
45 
46 /** Print error message.
47  * @param text text of the error message
48  */
49 void
51 {
52  __logger->log_error("XABSL", "%s", text);
53 }
54 
55 
56 /** Print info message.
57  * @param text text of the info message
58  */
59 void
61 {
62  __logger->log_info("XABSL", "%s", text);
63 }
64 
65 
66 /** @class XabslFileInputSource "xabsl_tools.h"
67  * File input class for Xabsl integration.
68  * @author Tim Niemueller
69  */
70 
71 /** Constructor.
72  * @param filename name of the file to read
73  */
75 {
76  __filename = strdup(filename);
77  __f = NULL;
78 }
79 
80 
81 /** Destructor. */
83 {
84  close();
85  free(__filename);
86 }
87 
88 
89 /** Open file.
90  * @return true if file has been opened successfully, false otherwise
91  */
92 bool
94 {
95  close();
96  __f = fopen(__filename, "r");
97  return (__f != NULL);
98 }
99 
100 
101 /** Close file. */
102 void
104 {
105  if ( __f ) fclose(__f);
106  __f = NULL;
107 }
108 
109 
110 /** Read a double value from the file.
111  * @return value read from the file
112  */
113 double
115 {
116  char buf[20];
117  if (read_from_file(buf, sizeof(buf)-1)) {
118  return atof(buf);
119  } else {
120  return 0.;
121  }
122 }
123 
124 
125 /** Read a string from the file.
126  * @param buf buffer where the string is stored
127  * @param buf_length maximum length of the string to be read, warning, this
128  * method will write one more byte than buf_length. This is done to be compatible
129  * with broken Xabsl.
130  * @return true on success, false otherwise
131  */
132 bool
133 XabslFileInputSource::readString(char *buf, int buf_length)
134 {
135  return read_from_file(buf, buf_length);
136 }
137 
138 
139 /** Omit comments. */
140 void
141 XabslFileInputSource::omit_comment()
142 {
143  while ( !feof(__f) ) {
144  char c;
145  if (fread(&c, 1, 1, __f)) {
146  if ( c == '\n') return;
147  } else {
148  return;
149  }
150  }
151 }
152 
153 /** Read and possibly omit whitespace.
154  * @param omit_whitespace if true whitespace is omitted
155  * @return first char read or 0 on error
156  */
157 char
158 XabslFileInputSource::read_and_omit_whitespace(bool omit_whitespace)
159 {
160  while ( ! feof(__f) ) {
161  char c;
162  if (fread(&c, 1, 1, __f)) {
163  if ( c == '/' ) {
164  omit_comment();
165  continue;
166  }
167  if ( (c != ' ') && (c != '\n') && (c != '\r') && (c != '\t') ) {
168  return c;
169  } else if ( ! omit_whitespace ) {
170  return 0;
171  }
172  } else {
173  throw fawkes::Exception ("XabslFileInputSource: omit_whitespace() fread failed");
174  }
175  }
176 
177  return 0;
178 }
179 
180 /** Read bytes from file.
181  * @param buf buffer where the string is stored
182  * @param buf_length maximum length of the string to be read, warning, this
183  * method will write one more byte than buf_length. This is done to be compatible
184  * with broken Xabsl.
185  * @return true if anything was read from the file, false if nothing has been read
186  */
187 bool
188 XabslFileInputSource::read_from_file(char *buf, size_t buf_length)
189 {
190  if ( ! __f || feof(__f) ) return false;
191 
192  memset(buf, 0, buf_length);
193  size_t cur_length = 0;
194  bool is_first = true;
195  while (! feof(__f) && (cur_length < buf_length)) {
196  char c = read_and_omit_whitespace(is_first);
197  is_first = false;
198  if (c) {
199  buf[cur_length++] = c;
200  buf[cur_length] = 0;
201  } else {
202  return (cur_length > 0);
203  }
204  }
205 
206  return (cur_length > 0);
207 }
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
~XabslFileInputSource()
Destructor.
Definition: xabsl_tools.cpp:82
virtual void printError(const char *text)
Print error message.
Definition: xabsl_tools.cpp:50
virtual void printMessage(const char *text)
Print info message.
Definition: xabsl_tools.cpp:60
virtual double readValue()
Read a double value from the file.
virtual void close()
Close file.
Base class for exceptions in Fawkes.
Definition: exception.h:36
XabslLoggingErrorHandler(fawkes::Logger *logger)
Constructor.
Definition: xabsl_tools.cpp:40
virtual bool open()
Open file.
Definition: xabsl_tools.cpp:93
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
XabslFileInputSource(const char *filename)
Constructor.
Definition: xabsl_tools.cpp:74
virtual bool readString(char *destination, int maxLength)
Read a string from the file.
Interface for logging.
Definition: logger.h:34