Warning, /include/Geant4/tools/get_lines 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_get_lines
0005 #define tools_get_lines
0006
0007 #include <cstdlib> // malloc,free
0008 #include <cstring> // strcpy
0009 #include <string>
0010 #include <vector>
0011
0012 namespace tools
0013 {
0014
0015 inline char* str_dup(const char* a_cstr)
0016 {
0017 return ::strcpy((char*)::malloc(::strlen(a_cstr) + 1), a_cstr);
0018 }
0019
0020 inline void str_del(char*& a_cstr)
0021 {
0022 if (a_cstr == NULL) return;
0023 ::free(a_cstr);
0024 a_cstr = NULL;
0025 }
0026
0027 inline void get_lines(const std::string& a_string,std::vector<std::string>& a_lines){
0028 // a_string is a list separated by "\n" or "\\n".
0029 // For "xxx\n\nxxx", {"xxx","","xxx"} will be created.
0030 // WARNING : if a_string is a Windows file name, it may
0031 // contains a \n which is not a delimiter ; like ..\data\ntuples.hbook.
0032 a_lines.clear();
0033 size_t length = a_string.length();
0034 if(!length) return;
0035 char* cstring = str_dup(a_string.c_str());
0036 if(!cstring) return;
0037 size_t pos = 0;
0038 length++;
0039 for(size_t count=0;count<length;count++) {
0040 if( (cstring[count]=='\n') ||
0041 (cstring[count]=='\0') ||
0042 ( (cstring[count]=='\\') && (cstring[count+1]=='n') ) ) {
0043 char shift_one = (cstring[count]=='\n' ? 1 : 0);
0044 cstring[count] = '\0';
0045 a_lines.push_back(cstring+pos);
0046 if(shift_one==1) {
0047 pos = count+1;
0048 } else {
0049 pos = count+2;
0050 count++;
0051 }
0052 }
0053 }
0054 str_del(cstring);
0055 }
0056
0057 }
0058
0059 #endif