Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_buf2lines 4 #ifndef tools_buf2lines 5 #define tools_buf2lines 5 #define tools_buf2lines 6 6 7 #include <vector> 7 #include <vector> 8 #include <string> 8 #include <string> 9 9 10 namespace tools { 10 namespace tools { 11 11 12 inline void buf2lines(char* a_buffer,std::vect 12 inline void buf2lines(char* a_buffer,std::vector<std::string>& a_lines) { 13 // WARNING : it is assumed that last char of 13 // WARNING : it is assumed that last char of a_buffer is 0. 14 // This function seeks '\n' and re 14 // This function seeks '\n' and replaces them with '0'. 15 a_lines.clear(); 15 a_lines.clear(); 16 char* pos = a_buffer; 16 char* pos = a_buffer; 17 char* beg = pos; 17 char* beg = pos; 18 while(true) { 18 while(true) { 19 if((*pos=='\r')&&(*(pos+1)=='\n')) { 19 if((*pos=='\r')&&(*(pos+1)=='\n')) { 20 *pos = 0; 20 *pos = 0; 21 a_lines.push_back(beg); 21 a_lines.push_back(beg); 22 pos += 2; 22 pos += 2; 23 beg = pos; 23 beg = pos; 24 } else if(*pos=='\n') { 24 } else if(*pos=='\n') { 25 *pos = 0; 25 *pos = 0; 26 a_lines.push_back(beg); 26 a_lines.push_back(beg); 27 pos++; 27 pos++; 28 beg = pos; 28 beg = pos; 29 } else if(*pos==0) { 29 } else if(*pos==0) { 30 a_lines.push_back(beg); 30 a_lines.push_back(beg); 31 return; 31 return; 32 } else { 32 } else { 33 pos++; 33 pos++; 34 } 34 } 35 } 35 } 36 } 36 } 37 37 38 inline void buf2lines(size_t a_size,const char 38 inline void buf2lines(size_t a_size,const char* a_buffer,std::vector<std::string>& a_lines) { 39 a_lines.clear(); 39 a_lines.clear(); 40 if(!a_size) return; 40 if(!a_size) return; 41 std::string line; 41 std::string line; 42 char* pos = (char*)a_buffer; 42 char* pos = (char*)a_buffer; 43 const char* end = a_buffer+a_size-1; 43 const char* end = a_buffer+a_size-1; 44 while(true) { 44 while(true) { 45 if(pos>end) { 45 if(pos>end) { 46 if(line.size()) a_lines.push_back(line); 46 if(line.size()) a_lines.push_back(line); 47 return; 47 return; 48 } 48 } 49 if((*pos=='\r')&&((pos+1)<=end)&&(*(pos+1) 49 if((*pos=='\r')&&((pos+1)<=end)&&(*(pos+1)=='\n')) { 50 a_lines.push_back(line); 50 a_lines.push_back(line); 51 line.clear(); 51 line.clear(); 52 pos += 2; 52 pos += 2; 53 } else if(*pos=='\n') { 53 } else if(*pos=='\n') { 54 a_lines.push_back(line); 54 a_lines.push_back(line); 55 line.clear(); 55 line.clear(); 56 pos++; 56 pos++; 57 } else { 57 } else { 58 line += *pos; 58 line += *pos; 59 pos++; 59 pos++; 60 } 60 } 61 } 61 } 62 } 62 } 63 63 64 } 64 } 65 65 66 #include <string.h> //memcpy 66 #include <string.h> //memcpy 67 67 68 namespace tools { 68 namespace tools { 69 69 70 // used in exlib/hdf5/T_tools,tools : 70 // used in exlib/hdf5/T_tools,tools : 71 /* 71 /* 72 inline bool strings2buf(size_t a_number,const 72 inline bool strings2buf(size_t a_number,const char** a_strings,size_t& a_size,char*& a_buffer) { 73 // For {"aa","bbb"}, it produces "aa0bbb00". 73 // For {"aa","bbb"}, it produces "aa0bbb00". 74 // For {""}, it produces "00". 74 // For {""}, it produces "00". 75 // For {}, it produces "0". 75 // For {}, it produces "0". 76 a_size = 0; 76 a_size = 0; 77 for(size_t index=0;index<a_number;index++) a 77 for(size_t index=0;index<a_number;index++) a_size += ::strlen(a_strings[index])+1; 78 a_size++; 78 a_size++; 79 a_buffer = new char[a_size]; 79 a_buffer = new char[a_size]; 80 if(!a_buffer) {a_size = 0;return false;} 80 if(!a_buffer) {a_size = 0;return false;} 81 char* pos = a_buffer; 81 char* pos = a_buffer; 82 size_t array_size; 82 size_t array_size; 83 for(size_t index=0;index<a_number;index++) { 83 for(size_t index=0;index<a_number;index++) { 84 const char* _s = a_strings[index]; 84 const char* _s = a_strings[index]; 85 array_size = ::strlen(_s)+1; 85 array_size = ::strlen(_s)+1; 86 ::memcpy(pos,_s,array_size); 86 ::memcpy(pos,_s,array_size); 87 pos += array_size; 87 pos += array_size; 88 } 88 } 89 *pos = '\0'; 89 *pos = '\0'; 90 return true; 90 return true; 91 } 91 } 92 */ 92 */ 93 93 94 inline bool strings2buf(const std::vector<std: 94 inline bool strings2buf(const std::vector<std::string>& a_strings,size_t& a_size,char*& a_buffer) { 95 // For {"aa","bbb"}, it produces "aa0bbb00". 95 // For {"aa","bbb"}, it produces "aa0bbb00". 96 // For {""}, it produces "00". 96 // For {""}, it produces "00". 97 // For {}, it produces "0". 97 // For {}, it produces "0". 98 size_t number = a_strings.size(); 98 size_t number = a_strings.size(); 99 a_size = 0; 99 a_size = 0; 100 for(size_t index=0;index<number;index++) a_s 100 for(size_t index=0;index<number;index++) a_size += a_strings[index].size()+1; 101 a_size++; 101 a_size++; 102 a_buffer = new char[a_size]; 102 a_buffer = new char[a_size]; 103 if(!a_buffer) {a_size = 0;return false;} 103 if(!a_buffer) {a_size = 0;return false;} 104 char* pos = a_buffer; 104 char* pos = a_buffer; 105 size_t array_size; 105 size_t array_size; 106 for(size_t index=0;index<number;index++) { 106 for(size_t index=0;index<number;index++) { 107 const std::string& _s = a_strings[index]; 107 const std::string& _s = a_strings[index]; 108 array_size = _s.size()+1; 108 array_size = _s.size()+1; 109 ::memcpy(pos,_s.c_str(),array_size); 109 ::memcpy(pos,_s.c_str(),array_size); 110 pos += array_size; 110 pos += array_size; 111 } 111 } 112 *pos = '\0'; 112 *pos = '\0'; 113 return true; 113 return true; 114 } 114 } 115 115 116 inline bool buf2strings(size_t a_size,char* a_ 116 inline bool buf2strings(size_t a_size,char* a_buffer,std::vector<std::string>& a_strings) { 117 if(a_size<=1) return false; 117 if(a_size<=1) return false; 118 // We assume here a_size>=1 118 // We assume here a_size>=1 119 // Exa : if ab0cde00, then a_strings should 119 // Exa : if ab0cde00, then a_strings should contain two strings ab and cde. 120 a_strings.clear(); 120 a_strings.clear(); 121 char* begin = a_buffer; 121 char* begin = a_buffer; 122 char* pos = a_buffer; 122 char* pos = a_buffer; 123 size_t number = a_size-1; 123 size_t number = a_size-1; 124 for(size_t index=0;index<number;index++) { 124 for(size_t index=0;index<number;index++) { 125 if((*pos)=='\0') { 125 if((*pos)=='\0') { 126 size_t l = pos - begin; 126 size_t l = pos - begin; 127 std::string _s(l,0); 127 std::string _s(l,0); 128 char* data = (char*)_s.c_str(); 128 char* data = (char*)_s.c_str(); 129 ::memcpy(data,begin,l); 129 ::memcpy(data,begin,l); 130 a_strings.push_back(_s); 130 a_strings.push_back(_s); 131 begin = pos+1; 131 begin = pos+1; 132 } 132 } 133 pos++; 133 pos++; 134 } 134 } 135 //pos++; 135 //pos++; 136 return true; 136 return true; 137 } 137 } 138 138 139 } 139 } 140 140 141 #endif 141 #endif 142 142