00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00072 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
00073 continue;
00074 }
00075
00076 if (onlyMySection) {
00077 if (line[0] == '[') {
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
00091 if (end == 0 || !line[0]) {
00092 continue;
00093 }
00094
00095 return true;
00096 }
00097
00098 return false;
00099 }