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