utils.cc

Go to the documentation of this file.
00001 /*
00002  * This file is part of openSDK.
00003  *
00004  * Copyright (C) 2006-2007 openSDK team
00005  *
00006  * openSDK is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; version 2.
00009  *
00010  * openSDK is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with openSDK; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  *     $Id: utils.cc,v 1.6 2007/01/26 23:12:05 nuno-lopes Exp $
00020  */
00021 
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <sys/mman.h>
00025 #include <unistd.h>
00026 #include <fcntl.h>
00027 #include <utils.h>
00028 #include <Platform.h>
00029 
00030 using namespace std;
00031 
00033 const void* file_get_contents(const char *filename, size_t* size)
00034 {
00035         struct stat sbuf;
00036         int fd;
00037         const char *str;
00038 
00039         if ((fd = open(filename, O_RDONLY)) == -1 || fstat(fd, &sbuf) == -1) {
00040                 *size = (size_t)-1;
00041                 return NULL;
00042         }
00043 
00044         if ((*size = sbuf.st_size) == 0) {
00045                 str = NULL;
00046 
00047         } else if ((str = (const char*)mmap(0, *size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
00048                 str  = NULL;
00049                 *size = (size_t)-1;
00050         }
00051 
00052         close(fd);
00053         return str;
00054 }
00055 
00057 bool file_unmap_contents(void* base, size_t size)
00058 {
00059         return (munmap(base, size) == 0 ? true : false);
00060 }
00061 
00063 bool getNonEmptyLine(fstream& file, string& line, bool onlyMySection, bool* mySection)
00064 {
00065         while(getline(file, line)) {
00066                 size_t idx = line.find_first_not_of(" \t");
00067                 if (idx != string::npos && idx != 0) {
00068                         line.erase(0, idx);
00069                 }
00070 
00071                 // skip comments and blank lines
00072                 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
00073                         continue;
00074                 }
00075 
00076                 if (onlyMySection) {
00077                         if (line[0] == '[') { // start of a new section
00078                                 *mySection = (line == "[" ROBOTDESIGN "]");
00079                                 continue;
00080                         } else if(!*mySection) {
00081                                 continue;
00082                         }
00083                 }
00084 
00085                 size_t end = line.find_first_of("\r\n");
00086                 if (end != string::npos) {
00087                         line[end] = '\0';
00088                 }
00089 
00090                 // skip empty lines
00091                 if (end == 0 || !line[0]) {
00092                         continue;
00093                 }
00094 
00095                 return true;
00096         }
00097 
00098         return false;
00099 }

Generated on Sun Dec 2 23:04:31 2007 for openSDK by  doxygen 1.3.9.1