g4tools  5.4.0
buf2lines
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
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::vector<std::string>& a_lines) {
13  // WARNING : it is assumed that last char of a_buffer is 0.
14  // This function seeks '\n' and replaces them with '0'.
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* a_buffer,std::vector<std::string>& a_lines) {
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)=='\n')) {
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 char** a_strings,size_t& a_size,char*& a_buffer) {
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_size += ::strlen(a_strings[index])+1;
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::string>& a_strings,size_t& a_size,char*& a_buffer) {
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_size += a_strings[index].size()+1;
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_buffer,std::vector<std::string>& a_strings) {
117  if(a_size<=1) return false;
118  // We assume here a_size>=1
119  // Exa : if ab0cde00, then a_strings should contain two strings ab and cde.
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 
tools::buf2strings
bool buf2strings(size_t a_size, char *a_buffer, std::vector< std::string > &a_strings)
Definition: buf2lines:116
tools::waxml::begin
void begin(std::ostream &a_writer)
Definition: begend:15
tools::buf2lines
void buf2lines(char *a_buffer, std::vector< std::string > &a_lines)
Definition: buf2lines:12
tools::file::size
bool size(const std::string &a_file, long &a_size)
Definition: fsize:13
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::waxml::end
void end(std::ostream &a_writer)
Definition: begend:31
tools::line
Definition: line:13
tools::strings2buf
bool strings2buf(const std::vector< std::string > &a_strings, size_t &a_size, char *&a_buffer)
Definition: buf2lines:94