Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/buf2lines is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_buf2lines
0005 #define tools_buf2lines
0006 
0007 #include <vector>
0008 #include <string>
0009 
0010 namespace tools {
0011 
0012 inline void buf2lines(char* a_buffer,std::vector<std::string>& a_lines) {
0013   // WARNING : it is assumed that last char of a_buffer is 0.
0014   //           This function seeks '\n' and replaces them with '0'.
0015   a_lines.clear();
0016   char* pos = a_buffer;
0017   char* beg = pos;
0018   while(true) {
0019     if((*pos=='\r')&&(*(pos+1)=='\n')) {
0020       *pos = 0;
0021       a_lines.push_back(beg);
0022       pos += 2;
0023       beg = pos;
0024     } else if(*pos=='\n') {
0025       *pos = 0;
0026       a_lines.push_back(beg);
0027       pos++;
0028       beg = pos;
0029     } else if(*pos==0) {
0030       a_lines.push_back(beg);
0031       return;
0032     } else {
0033       pos++;
0034     }
0035   }
0036 }
0037 
0038 inline void buf2lines(size_t a_size,const char* a_buffer,std::vector<std::string>& a_lines) {
0039   a_lines.clear();
0040   if(!a_size) return;
0041   std::string line;
0042   char* pos = (char*)a_buffer;
0043   const char* end = a_buffer+a_size-1;
0044   while(true) {
0045     if(pos>end) {
0046       if(line.size()) a_lines.push_back(line);
0047       return;
0048     }
0049     if((*pos=='\r')&&((pos+1)<=end)&&(*(pos+1)=='\n')) {
0050       a_lines.push_back(line);
0051       line.clear();
0052       pos += 2;
0053     } else if(*pos=='\n') {
0054       a_lines.push_back(line);
0055       line.clear();
0056       pos++;
0057     } else {
0058       line += *pos;
0059       pos++;
0060     }
0061   }
0062 }
0063 
0064 }
0065 
0066 #include <string.h> //memcpy
0067 
0068 namespace tools {
0069 
0070 // used in exlib/hdf5/T_tools,tools :
0071 /*
0072 inline bool strings2buf(size_t a_number,const char** a_strings,size_t& a_size,char*& a_buffer) {
0073   // For {"aa","bbb"}, it produces "aa0bbb00".
0074   // For {""}, it produces "00".
0075   // For {}, it produces "0".
0076   a_size = 0;
0077   for(size_t index=0;index<a_number;index++) a_size += ::strlen(a_strings[index])+1;
0078   a_size++;
0079   a_buffer = new char[a_size];
0080   if(!a_buffer) {a_size = 0;return false;}
0081   char* pos = a_buffer;
0082   size_t array_size;
0083   for(size_t index=0;index<a_number;index++) {
0084     const char* _s = a_strings[index];
0085     array_size = ::strlen(_s)+1;
0086     ::memcpy(pos,_s,array_size);
0087     pos += array_size;
0088   }
0089   *pos = '\0';
0090   return true;
0091 }
0092 */
0093 
0094 inline bool strings2buf(const std::vector<std::string>& a_strings,size_t& a_size,char*& a_buffer) {
0095   // For {"aa","bbb"}, it produces "aa0bbb00".
0096   // For {""}, it produces "00".
0097   // For {}, it produces "0".
0098   size_t number = a_strings.size();
0099   a_size = 0;
0100   for(size_t index=0;index<number;index++) a_size += a_strings[index].size()+1;
0101   a_size++;
0102   a_buffer = new char[a_size];
0103   if(!a_buffer) {a_size = 0;return false;}
0104   char* pos = a_buffer;
0105   size_t array_size;
0106   for(size_t index=0;index<number;index++) {
0107     const std::string& _s = a_strings[index];
0108     array_size = _s.size()+1;
0109     ::memcpy(pos,_s.c_str(),array_size);
0110     pos += array_size;
0111   }
0112   *pos = '\0';
0113   return true;
0114 }
0115 
0116 inline bool buf2strings(size_t a_size,char* a_buffer,std::vector<std::string>& a_strings) {
0117   if(a_size<=1) return false;
0118   // We assume here a_size>=1
0119   // Exa : if ab0cde00, then a_strings should contain two strings ab and cde.
0120   a_strings.clear();
0121   char* begin = a_buffer;
0122   char* pos = a_buffer;
0123   size_t number = a_size-1;
0124   for(size_t index=0;index<number;index++) {
0125     if((*pos)=='\0') {
0126       size_t l = pos - begin;
0127       std::string _s(l,0);
0128       char* data = (char*)_s.c_str();
0129       ::memcpy(data,begin,l);
0130       a_strings.push_back(_s);
0131       begin = pos+1;
0132     }
0133     pos++;
0134   }
0135   //pos++;
0136   return true;
0137 }
0138 
0139 }
0140 
0141 #endif
0142