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 <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 /** Print error message.
46  * @param text text of the error message
47  */
48 void
50 {
51  logger_->log_error("XABSL", "%s", text);
52 }
53 
54 /** Print info message.
55  * @param text text of the info message
56  */
57 void
59 {
60  logger_->log_info("XABSL", "%s", text);
61 }
62 
63 /** @class XabslFileInputSource "xabsl_tools.h"
64  * File input class for Xabsl integration.
65  * @author Tim Niemueller
66  */
67 
68 /** Constructor.
69  * @param filename name of the file to read
70  */
72 {
73  filename_ = strdup(filename);
74  f_ = NULL;
75 }
76 
77 /** Destructor. */
79 {
80  close();
81  free(filename_);
82 }
83 
84 /** Open file.
85  * @return true if file has been opened successfully, false otherwise
86  */
87 bool
89 {
90  close();
91  f_ = fopen(filename_, "r");
92  return (f_ != NULL);
93 }
94 
95 /** Close file. */
96 void
98 {
99  if (f_)
100  fclose(f_);
101  f_ = NULL;
102 }
103 
104 /** Read a double value from the file.
105  * @return value read from the file
106  */
107 double
109 {
110  char buf[20];
111  if (read_from_file(buf, sizeof(buf) - 1)) {
112  return atof(buf);
113  } else {
114  return 0.;
115  }
116 }
117 
118 /** Read a string from the file.
119  * @param buf buffer where the string is stored
120  * @param buf_length maximum length of the string to be read, warning, this
121  * method will write one more byte than buf_length. This is done to be compatible
122  * with broken Xabsl.
123  * @return true on success, false otherwise
124  */
125 bool
126 XabslFileInputSource::readString(char *buf, int buf_length)
127 {
128  return read_from_file(buf, buf_length);
129 }
130 
131 /** Omit comments. */
132 void
133 XabslFileInputSource::omit_comment()
134 {
135  while (!feof(f_)) {
136  char c;
137  if (fread(&c, 1, 1, f_)) {
138  if (c == '\n')
139  return;
140  } else {
141  return;
142  }
143  }
144 }
145 
146 /** Read and possibly omit whitespace.
147  * @param omit_whitespace if true whitespace is omitted
148  * @return first char read or 0 on error
149  */
150 char
151 XabslFileInputSource::read_and_omit_whitespace(bool omit_whitespace)
152 {
153  while (!feof(f_)) {
154  char c;
155  if (fread(&c, 1, 1, f_)) {
156  if (c == '/') {
157  omit_comment();
158  continue;
159  }
160  if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) {
161  return c;
162  } else if (!omit_whitespace) {
163  return 0;
164  }
165  } else {
166  throw fawkes::Exception("XabslFileInputSource: omit_whitespace() fread failed");
167  }
168  }
169 
170  return 0;
171 }
172 
173 /** Read bytes from file.
174  * @param buf buffer where the string is stored
175  * @param buf_length maximum length of the string to be read, warning, this
176  * method will write one more byte than buf_length. This is done to be compatible
177  * with broken Xabsl.
178  * @return true if anything was read from the file, false if nothing has been read
179  */
180 bool
181 XabslFileInputSource::read_from_file(char *buf, size_t buf_length)
182 {
183  if (!f_ || feof(f_))
184  return false;
185 
186  memset(buf, 0, buf_length);
187  size_t cur_length = 0;
188  bool is_first = true;
189  while (!feof(f_) && (cur_length < buf_length)) {
190  char c = read_and_omit_whitespace(is_first);
191  is_first = false;
192  if (c) {
193  buf[cur_length++] = c;
194  buf[cur_length] = 0;
195  } else {
196  return (cur_length > 0);
197  }
198  }
199 
200  return (cur_length > 0);
201 }
XabslFileInputSource::close
virtual void close()
Close file.
Definition: xabsl_tools.cpp:97
XabslFileInputSource::open
virtual bool open()
Open file.
Definition: xabsl_tools.cpp:88
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
XabslFileInputSource::XabslFileInputSource
XabslFileInputSource(const char *filename)
Constructor.
Definition: xabsl_tools.cpp:71
XabslFileInputSource::readString
virtual bool readString(char *destination, int maxLength)
Read a string from the file.
Definition: xabsl_tools.cpp:126
fawkes::Logger::log_error
virtual void log_error(const char *component, const char *format,...)=0
fawkes::Logger
Definition: logger.h:41
XabslFileInputSource::readValue
virtual double readValue()
Read a double value from the file.
Definition: xabsl_tools.cpp:108
XabslLoggingErrorHandler::printError
virtual void printError(const char *text)
Print error message.
Definition: xabsl_tools.cpp:49
XabslFileInputSource::~XabslFileInputSource
~XabslFileInputSource()
Destructor.
Definition: xabsl_tools.cpp:78
XabslLoggingErrorHandler::printMessage
virtual void printMessage(const char *text)
Print info message.
Definition: xabsl_tools.cpp:58
XabslLoggingErrorHandler::XabslLoggingErrorHandler
XabslLoggingErrorHandler(fawkes::Logger *logger)
Constructor.
Definition: xabsl_tools.cpp:40
fawkes::Exception
Definition: exception.h:41