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 #include <string.h> //memcpy
0010 
0011 namespace tools {
0012 
0013 inline bool strings2buf(const std::vector<std::string>& a_strings,size_t& a_size,char*& a_buffer) {
0014   // For {"aa","bbb"}, it produces "aa0bbb00".
0015   // For {""}, it produces "00".
0016   // For {}, it produces "0".
0017   size_t number = a_strings.size();
0018   a_size = 0;
0019   for(size_t index=0;index<number;index++) a_size += a_strings[index].size()+1;
0020   a_size++;
0021   a_buffer = new char[a_size];
0022   if(!a_buffer) {a_size = 0;return false;}
0023   char* pos = a_buffer;
0024   size_t array_size;
0025   for(size_t index=0;index<number;index++) {
0026     const std::string& _s = a_strings[index];
0027     array_size = _s.size()+1;
0028     ::memcpy(pos,_s.c_str(),array_size);
0029     pos += array_size;
0030   }
0031   *pos = '\0';
0032   return true;
0033 }
0034 
0035 inline bool buf2strings(size_t a_size,char* a_buffer,std::vector<std::string>& a_strings) {
0036   if(a_size<=1) return false;
0037   // We assume here a_size>=1
0038   // Exa : if ab0cde00, then a_strings should contain two strings ab and cde.
0039   a_strings.clear();
0040   char* begin = a_buffer;
0041   char* pos = a_buffer;
0042   size_t number = a_size-1;
0043   for(size_t index=0;index<number;index++) {
0044     if((*pos)=='\0') {
0045       size_t l = pos - begin;
0046       std::string _s(l,0);
0047       char* data = (char*)_s.c_str();
0048       ::memcpy(data,begin,l);
0049       a_strings.push_back(_s);
0050       begin = pos+1;
0051     }
0052     pos++;
0053   }
0054   return true;
0055 }
0056 
0057 }
0058 
0059 #endif
0060