2 * fgcom - VoIP-Client for the FlightGear-Radio-Infrastructure
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 #include <WinSock2.h> // which included <Windows.h>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <mach-o/dyld.h> /* for _NSGetExecutablePath() */
39 #include <simgear/debug/logstream.hxx>
43 #define bcopy(from, to, n) memcpy(to, from, n)
47 static int s_file_handle;
48 static char *s_content;
53 * \fn void parser_init(void)
55 * \brief Starts parser initialization.
58 int parser_init(const char *filename)
65 oflag |= _O_BINARY; /* if comparing to stat size then must be binary */
70 if((s_file_handle = open(filename, oflag)) < 0)
71 return (s_file_handle);
73 fstat(s_file_handle, &l_stat);
76 if((s_content = (char *)malloc((l_stat.st_size + 1) * sizeof(char))) != NULL)
78 if((l_nbytes = read(s_file_handle, s_content, l_stat.st_size)) == l_stat.st_size)
81 s_size = l_stat.st_size;
91 * \fn void parser_exit(void)
93 * \brief Exits parser.
96 void parser_exit(void)
103 * \fn int parser_get_next_value(const char *line, float *value)
105 * \brief Extract a numeric value.
107 * \param line pointer on the line extracted from the input file.
108 * \param value pointer on the returned value.
110 * \return Returns 0 if value successfully extracted. Otherwhise, returns
111 * a negative value meaning that an error occured.
114 int parser_get_next_value(double *value)
121 /* Check if we are already at the end of the string. */
122 if(s_index >= s_size)
125 /* Enter main parser loop. */
126 while((s_index < s_size) && (l_status == 0))
128 /* Search for something different than an espace or tab. */
129 while( (s_content[s_index] == ' ' || s_content[s_index] == '\t') &&
133 /* If we have reached end of file, we exit now. */
134 if (s_index >= s_size)
137 /* If character is a CR, we restart for next line. */
138 if(s_content[s_index] == '\n')
144 /* Is it a comment ? */
145 if(s_content[s_index] == '#')
147 /* Yes, go until end of line. */
148 while((s_content[s_index] != '\n') && (s_index < s_size))
153 /* We have found something that is not a comment. */
154 while((s_content[s_index] < '0' || s_content[s_index] > '9') && (s_index < s_size))
160 while( ((s_content[l_j] >= '0' && s_content[l_j] <= '9') ||
161 (s_content[l_j] == '.' || s_content[l_j] == ',')) &&
162 ((s_content[l_j] != '\n') && (l_j < (unsigned int)s_size)) )
165 l_size = l_j - s_index + 1;
166 if((l_buf = (char *)malloc(l_size * sizeof(char))) != NULL)
168 /* Initialize buffer with O. */
169 memset((void *)l_buf, 0, l_size);
170 bcopy((const void *)(s_content + s_index), (void *)l_buf, l_size - 1);
171 /* Convert string into double. */
172 *value = atof(l_buf);
173 /* Buffer is not needed any longer. */
175 /* Prepare for next value. */
181 } /* while((s_index < s_size) && (l_status == 0)) */
186 /* cross-platform stat and check if directory or file
187 * return 2 == directory
189 * return 0 == neither
190 * NOTE: Think this fails in Windows if
191 * the path has a trailing path separator
192 * and in some cases even if the path contains a forward (unix) separator
193 * TODO: Should copy the path, and fix it for stat
197 #define M_ISDIR(a) (a & _S_IFDIR)
199 #define M_ISDIR S_ISDIR
202 int is_file_or_directory( const char * path )
205 if ( stat(path,&buf) == 0 ) {
206 if (M_ISDIR(buf.st_mode))
213 /* trim to base binary path IN BUFFER,
214 * essentially removing the executable name
216 void trim_base_path_ib( char *path )
218 size_t len = strlen(path);
222 for (i = 0; i < len; i++) {
224 if (( c == '/' ) || ( c == '\\')) {
225 off = i + 1; // get after separator
236 * get data path per OS
237 * In Windows and OSX the compiler only supplies a partial data file path,
238 * so this is to get the current binary installed path.
239 * In *nix the full path is supplied, so this does nothing, except zero the path
242 int get_data_path_per_os( char *path, size_t len )
245 // if we're running as part of an application bundle, return the bundle's
246 // resources directory
247 // The following code looks for the base package inside the application
248 // bundle, in the standard Contents/Resources location.
250 CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
252 CFURLGetFileSystemRepresentation(resourcesUrl, true, (UInt8*) path, len);
253 CFRelease(resourcesUrl);
254 // append trailing seperator since CF doesn't
261 // we're unbundled, simply return the executable path
262 unsigned int size = (unsigned int) len;
263 if (_NSGetExecutablePath(path, &size) == 0) {
265 trim_base_path_ib(path);
267 SG_LOG( SG_GENERAL, SG_ALERT, "ERROR: path buffer too small; need size " << size );
270 #elif defined(_MSC_VER)
271 unsigned int size = GetModuleFileName(NULL,path, len);
272 if (size && (size != len)) {
274 trim_base_path_ib(path);
277 SG_LOG( SG_GENERAL, SG_ALERT, "ERROR: GetModuleFileName: path buffer too small; need size more than " << len );
279 SG_LOG( SG_GENERAL, SG_ALERT, "ERROR: GetModuleFileName FAILED!" );
285 #endif // MACOSX | _MSC_VER | others
289 /* eof - utils.cpp */