00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "xabsl_tools.h"
00024
00025 #include <core/exception.h>
00026 #include <utils/logging/logger.h>
00027
00028 #include <cstdlib>
00029 #include <cstring>
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 XabslLoggingErrorHandler::XabslLoggingErrorHandler(fawkes::Logger *logger)
00041 {
00042 __logger = logger;
00043 }
00044
00045
00046
00047
00048
00049 void
00050 XabslLoggingErrorHandler::printError(const char *text)
00051 {
00052 __logger->log_error("XABSL", "%s", text);
00053 }
00054
00055
00056
00057
00058
00059 void
00060 XabslLoggingErrorHandler::printMessage(const char *text)
00061 {
00062 __logger->log_info("XABSL", "%s", text);
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 XabslFileInputSource::XabslFileInputSource(const char *filename)
00075 {
00076 __filename = strdup(filename);
00077 __f = NULL;
00078 }
00079
00080
00081
00082 XabslFileInputSource::~XabslFileInputSource()
00083 {
00084 close();
00085 free(__filename);
00086 }
00087
00088
00089
00090
00091
00092 bool
00093 XabslFileInputSource::open()
00094 {
00095 close();
00096 __f = fopen(__filename, "r");
00097 return (__f != NULL);
00098 }
00099
00100
00101
00102 void
00103 XabslFileInputSource::close()
00104 {
00105 if ( __f ) fclose(__f);
00106 __f = NULL;
00107 }
00108
00109
00110
00111
00112
00113 double
00114 XabslFileInputSource::readValue()
00115 {
00116 char buf[20];
00117 if (read_from_file(buf, sizeof(buf)-1)) {
00118 return atof(buf);
00119 } else {
00120 return 0.;
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 bool
00133 XabslFileInputSource::readString(char *buf, int buf_length)
00134 {
00135 return read_from_file(buf, buf_length);
00136 }
00137
00138
00139
00140 void
00141 XabslFileInputSource::omit_comment()
00142 {
00143 while ( !feof(__f) ) {
00144 char c;
00145 if (fread(&c, 1, 1, __f)) {
00146 if ( c == '\n') return;
00147 } else {
00148 return;
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156
00157 char
00158 XabslFileInputSource::read_and_omit_whitespace(bool omit_whitespace)
00159 {
00160 while ( ! feof(__f) ) {
00161 char c;
00162 if (fread(&c, 1, 1, __f)) {
00163 if ( c == '/' ) {
00164 omit_comment();
00165 continue;
00166 }
00167 if ( (c != ' ') && (c != '\n') && (c != '\r') && (c != '\t') ) {
00168 return c;
00169 } else if ( ! omit_whitespace ) {
00170 return 0;
00171 }
00172 } else {
00173 throw fawkes::Exception ("XabslFileInputSource: omit_whitespace() fread failed");
00174 }
00175 }
00176
00177 return 0;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187 bool
00188 XabslFileInputSource::read_from_file(char *buf, size_t buf_length)
00189 {
00190 if ( ! __f || feof(__f) ) return false;
00191
00192 memset(buf, 0, buf_length);
00193 size_t cur_length = 0;
00194 bool is_first = true;
00195 while (! feof(__f) && (cur_length < buf_length)) {
00196 char c = read_and_omit_whitespace(is_first);
00197 is_first = false;
00198 if (c) {
00199 buf[cur_length++] = c;
00200 buf[cur_length] = 0;
00201 } else {
00202 return (cur_length > 0);
00203 }
00204 }
00205
00206 return (cur_length > 0);
00207 }