Changeset 142:9fdf72ae3cdd in rrlib_util


Ignore:
Timestamp:
09.05.2016 15:38:16 (4 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Message:

reorganized sFileIOUtils (now only functions from rrlib/util/fileio.h should be used); added unit tests for fileio

Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • make.xml

    r126 r142  
    1818  </library> 
    1919 
    20   <library name="legacy" 
    21          libs="boost_iostreams"> 
     20  <library name="fileio"> 
     21    <sources> 
     22      fileio.cpp 
     23    </sources> 
     24  </library> 
     25 
     26  <library name="legacy"> 
    2227    <sources> 
    2328      final_class.h 
    24       sFileIOUtils.cpp 
    2529      sStringUtils.cpp 
    2630      tTime.cpp 
     
    2933    </sources> 
    3034  </library> 
     35   
     36  <library name="deprecated" libs="boost_iostreams"> 
     37    <sources> 
     38      sFileIOUtils.cpp 
     39    </sources> 
     40  </library> 
     41   
    3142   
    3243  <library name="unit_tests" 
  • sFileIOUtils.cpp

    r138 r142  
    3434#include "rrlib/util/sFileIOUtils.h" 
    3535 
    36 #include <algorithm> 
    37 #include <iostream> 
    38 #include <fstream> 
    39 #include <sstream> 
    40 #include <iterator> 
    41  
    42 extern "C" 
    43 { 
    44 #include <netdb.h> // struct hostent, gethostbyname 
    45 #include <unistd.h> 
    46 #include <dirent.h> 
    47 #include <wordexp.h> 
    48 } 
    49  
    50 #include "rrlib/util/sStringUtils.h" 
    51 #include "rrlib/util/join.h" 
    52 #include "rrlib/logging/messages.h" 
     36#include "rrlib/util/fileio.h" 
    5337 
    5438//---------------------------------------------------------------------- 
    5539// Debug 
    5640//---------------------------------------------------------------------- 
    57 #include <cassert> 
    58 #include <cstring> 
    59 #include <cerrno> 
    6041 
    6142//---------------------------------------------------------------------- 
    6243// Namespace usage 
    6344//---------------------------------------------------------------------- 
    64 using namespace std; 
    6545 
    6646//---------------------------------------------------------------------- 
     
    7353 
    7454//---------------------------------------------------------------------- 
    75 // initialization of static class vars 
     55// class sFileIOUtils::GetAllFilesInDirectory() 
    7656//---------------------------------------------------------------------- 
    77 std::string sFileIOUtils::cached_local_host = ""; 
    78 std::map<std::string, std::string> sFileIOUtils::host_name_to_ip_cache = std::map<std::string, std::string>(); 
    79 std::map<std::string, std::string> sFileIOUtils::resource_cache = std::map<std::string, std::string>(); 
    80  
    81  
    82  
    8357bool sFileIOUtils::GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files) 
    8458{ 
    85   DIR *dp; 
    86   struct dirent *dirp; 
    87   if ((dp  = opendir(dir.c_str())) == NULL) 
    88   { 
    89     return false; 
    90   } 
    91  
    92   while ((dirp = readdir(dp)) != NULL) 
    93   { 
    94     files.push_back(std::string(dirp->d_name)); 
    95     //std::cout << "Found: " << dirp->d_name << std::endl; 
    96   } 
    97  
    98   closedir(dp); 
    99   return true; 
     59  return fileio::GetAllFilesInDirectory(dir, files); 
    10060} 
    101  
    10261 
    10362 
     
    10766std::string sFileIOUtils::GetCurrentDirectory() 
    10867{ 
    109   char *temp_path(NULL); 
    110   // POSIX.1 extension allocates buffer for path if given buffer is NULL and size = 0 
    111   temp_path = getcwd(temp_path, 0); 
    112   std::string path(temp_path); 
    113   path += "/"; 
    114   free(temp_path); 
    115   return path; 
     68  return fileio::GetCurrentDirectory(); 
    11669} 
     70 
    11771 
    11872//---------------------------------------------------------------------- 
    11973// class sFileIOUtils::ChangeCurrentDirectory() 
    12074//---------------------------------------------------------------------- 
    121 int sFileIOUtils::ChangeCurrentDirectory(const std::string& dir) 
     75bool sFileIOUtils::ChangeCurrentDirectory(const std::string& dir) 
    12276{ 
    123   return chdir(dir.c_str()); 
     77  return fileio::ChangeCurrentDirectory(dir); 
    12478} 
     79 
    12580 
    12681//---------------------------------------------------------------------- 
    12782// class sFileIOUtils::CreateDirectory() 
    12883//---------------------------------------------------------------------- 
    129 bool sFileIOUtils::CreateDirectory(const std::string& path)    //throw(runtime_error) 
     84bool sFileIOUtils::CreateDirectory(const std::string& path) 
    13085{ 
    131   std::stringstream sys_call; 
    132   sys_call << "mkdir -p " << path; 
     86  return fileio::CreateDirectory(path); 
     87} 
    13388 
    134   if (system(sys_call.str().c_str()) == -1) 
    135   { 
    136     RRLIB_LOG_PRINT(USER, "sFileIOUtils>> Error creating dir: '", path, "'"); 
    137 //     std::stringstream error_msg; 
    138 //     error_msg << "sFileIOUtils>> Error creating Dir:\n" 
    139 //     << path << endl; 
    140     //throw runtime_error(error_msg.str()); 
    141     return false; 
    142   } 
    143   else 
    144   { 
    145 //     RRLIB_LOG_PRINT(USER, "Directory: '", path, "' successfully created."); 
    146     return true; 
    147   } 
    148 } 
    14989 
    15090//---------------------------------------------------------------------- 
     
    15393std::string sFileIOUtils::CreateTempfile() 
    15494{ 
    155   FILE * pipe = popen("mktemp", "r"); 
    156   char buf[ 1024 ]; 
    157   if (fgets(buf, sizeof(buf), pipe) == 0) 
    158   { 
    159     RRLIB_LOG_PRINT(ERROR, "Error creating temp file!"); 
    160     exit(EXIT_FAILURE); 
    161   } 
    162   std::string tempfile = buf; 
    163   tempfile = tempfile.substr(0, tempfile.find("\n")); 
    164   pclose(pipe); 
    165   return tempfile; 
     95  return fileio::CreateTempFile(); 
    16696} // CreateTempfile() 
     97 
    16798 
    16899//---------------------------------------------------------------------- 
     
    171102void sFileIOUtils::DeleteFile(const std::string& filename) 
    172103{ 
    173   unlink(filename.c_str()); 
     104  fileio::DeleteFile(filename); 
    174105} // DeleteFile() 
    175106 
    176 //---------------------------------------------------------------------- 
    177 // class sFileIOUtils::CompressFile() 
    178 //---------------------------------------------------------------------- 
    179 void sFileIOUtils::CompressFile(const std::string& input_filename, std::string& output_filename) 
    180 { 
    181   if (!sStringUtils::EndsWith(output_filename, ".gz")) 
    182     output_filename += ".gz"; 
    183   std::stringstream cmd; 
    184   cmd << "gzip -c " << input_filename << " > " << output_filename; 
    185   if (system(cmd.str().c_str()) != 0) 
    186   { 
    187     RRLIB_LOG_PRINT(ERROR, "Execution of command '", cmd.str(), "' failed!"); 
    188     exit(EXIT_FAILURE); 
    189   } 
    190 } // CompressFile() 
    191  
    192 //---------------------------------------------------------------------- 
    193 // class sFileIOUtils::DecompressFile() 
    194 //---------------------------------------------------------------------- 
    195 void sFileIOUtils::DecompressFile(const std::string& input_filename, const std::string& output_filename) 
    196 { 
    197   assert(sStringUtils::EndsWith(input_filename, ".gz")); 
    198   std::stringstream cmd; 
    199   cmd << "gunzip -c " << input_filename << " > " << output_filename; 
    200   if (system(cmd.str().c_str()) != 0) 
    201   { 
    202     RRLIB_LOG_PRINT(ERROR, "Execution of command '", cmd.str(), "' failed!"); 
    203     exit(EXIT_FAILURE); 
    204   } 
    205 } // DecompressFile() 
    206107 
    207108//---------------------------------------------------------------------- 
     
    210111bool sFileIOUtils::FileExists(const std::string &filename) 
    211112{ 
    212   // catch empty file 
    213   if (filename == "") 
    214   { 
    215     return false; 
    216   } 
    217  
    218   // test if file exists 
    219   FILE* file = fopen(filename.c_str(), "rb"); 
    220   if (file == 0) 
    221   { 
    222     return false; 
    223   } 
    224   fclose(file); 
    225  
    226   return true; 
     113  return fileio::FileExists(filename); 
    227114} // FileExists() 
    228115 
     
    233120size_t sFileIOUtils::CountLineNumbers(const std::string &filename) 
    234121{ 
    235   std::ifstream file(filename); 
    236   return std::count(std::istreambuf_iterator<char>(file), 
    237                     std::istreambuf_iterator<char>(), '\n'); 
     122  return fileio::CountLineNumbers(filename); 
    238123} // CountLineNumbers() 
    239  
    240124 
    241125 
     
    245129bool sFileIOUtils::ShellExpandFilename(std::string &file_name) 
    246130{ 
    247   return ShellExpandFilename(file_name, file_name); 
     131  return fileio::ShellExpandFilename(file_name); 
    248132} // ShellExpandFilename() 
     133 
    249134 
    250135//---------------------------------------------------------------------- 
     
    253138std::string sFileIOUtils::ShellExpandFilename(const std::string &file_name) 
    254139{ 
    255   std::string result(""); 
    256   if (ShellExpandFilename(result, file_name)) 
    257     return result; 
    258   else 
    259     return ""; 
     140  return fileio::ShellExpandFilename(file_name); 
    260141} // ShellExpandFilename() 
    261142 
     
    266147bool sFileIOUtils::ShellExpandFilename(std::string &result, const std::string &file_name) 
    267148{ 
    268   //result = "";  //in case &result == &file_name, then it causes trouble. 
    269   wordexp_t expansion; 
    270   int error = wordexp(file_name.c_str(), &expansion, WRDE_SHOWERR | WRDE_UNDEF); 
    271   if (error) 
    272   { 
    273     const char *error_msg = 0; 
    274     switch (error) 
    275     { 
    276     case WRDE_BADCHAR: 
    277       error_msg = "Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }"; 
    278       break; 
    279     case WRDE_BADVAL: 
    280       error_msg = "An undefined shell variable was referenced"; 
    281       break; 
    282     case WRDE_NOSPACE: 
    283       error_msg = "Out of memory"; 
    284       break; 
    285     case WRDE_SYNTAX: 
    286       error_msg = "Shell syntax error, such as unbalanced parentheses or unmatched quotes"; 
    287       break; 
    288     default: 
    289       error_msg = "Unknown error"; 
    290     } 
    291     RRLIB_LOG_PRINT(ERROR, "Could not expand '", file_name, "': ", error_msg, "!"); 
    292     return false; 
    293   } 
     149  return fileio::ShellExpandFilename(result, file_name); 
     150} // ShellExpandFilename() 
    294151 
    295   std::stringstream stream; 
    296   stream << util::Join(expansion.we_wordv + expansion.we_offs, expansion.we_wordv + expansion.we_offs + expansion.we_wordc, " "); 
    297   wordfree(&expansion); 
    298  
    299   result = stream.str(); 
    300  
    301   return true; 
    302 } // ShellExpandFilename() 
    303152 
    304153//---------------------------------------------------------------------- 
     
    307156void sFileIOUtils::SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext) 
    308157{ 
    309  
    310   std::string::size_type pos_slash = complete_name.find_last_of("/"); 
    311   std::string::size_type pos_name = pos_slash + 1; 
    312  
    313   if (pos_slash == std::string::npos) 
    314   { 
    315     // no slash found 
    316     file_dir = "./"; 
    317     pos_name = 0; 
    318     pos_slash = 0; 
    319   } 
    320   else 
    321   { 
    322     file_dir = complete_name.substr(0, pos_slash); 
    323   } 
    324  
    325   //    cerr << "pos_slash: " << pos_slash << endl; 
    326   std::string::size_type pos_dot = complete_name.find_last_of("."); 
    327   if (pos_dot == std::string::npos || pos_dot < pos_slash) 
    328   { 
    329     // no dot found or dot in directory 
    330     file_base = complete_name.substr(pos_name); 
    331     file_ext = ""; 
    332   } 
    333   else 
    334   { 
    335     file_base = complete_name.substr(pos_name, pos_dot - pos_name); 
    336     file_ext = complete_name.substr(pos_dot); 
    337   } 
    338  
     158  return fileio::SplitFullQualifiedFilename(complete_name, file_dir, file_base, file_ext); 
    339159} // SplitFullQualifiedFilename() 
    340160 
    341  
    342 //---------------------------------------------------------------------- 
    343 // class sFileIOUtils::GetHostName() 
    344 //---------------------------------------------------------------------- 
    345 std::string sFileIOUtils::GetHostName() 
    346 { 
    347   char name[1024]; 
    348   int result = gethostname(name, 1024); 
    349   if (result) 
    350   { 
    351     RRLIB_LOG_PRINT(ERROR, "got error: <", strerror(errno), ">"); 
    352     exit(EXIT_FAILURE); 
    353   } 
    354   RRLIB_LOG_PRINT(DEBUG, "got hostname: <", name, ">"); 
    355  
    356   return name; 
    357 } // GetHostName() 
    358  
    359 //---------------------------------------------------------------------- 
    360 // class sFileIOUtils::HostToIpViaGetHostByName() 
    361 //---------------------------------------------------------------------- 
    362 struct ::in_addr sFileIOUtils::HostToIpViaGetHostByName(const std::string& name) 
    363 { 
    364   struct in_addr address; 
    365   address.s_addr = 0; 
    366   struct hostent *host_ent = gethostbyname(name.c_str());       //alternative (from kernel/tTCPSocket): GetHostByName( name ); 
    367   if (host_ent) 
    368   { 
    369     address = * ((struct in_addr *) host_ent->h_addr_list[ 0 ]); 
    370     RRLIB_LOG_PRINT(USER, "ip address of host <", name, "> = <", inet_ntoa(address), ">"); 
    371   } 
    372   else 
    373   { 
    374     RRLIB_LOG_PRINT(ERROR, "could not get ip address of host <", name, ">"); 
    375   } 
    376   return address; 
    377 } //HostToIpViaGetHostByName() 
    378  
    379  
    380 //---------------------------------------------------------------------- 
    381 // class sFileIOUtils::HostToIpViaNslookup() 
    382 //---------------------------------------------------------------------- 
    383 struct in_addr sFileIOUtils::HostToIpViaNslookup(const std::string & name) 
    384 { 
    385   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "started with host <", name, ">"); 
    386   struct in_addr address; 
    387   address.s_addr = 0; 
    388  
    389   std::stringstream command; 
    390   command << "nslookup \"" << name << "\""; 
    391   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "command = <", command.str(), ">"); 
    392   FILE * pipe = popen(command.str().c_str(), "r"); 
    393   if (!pipe) 
    394   { 
    395     perror("error\n"); 
    396     return address; 
    397   } 
    398   std::stringstream result; 
    399   char buf[ 512 ]; 
    400   while (fgets(buf, 512, pipe) != NULL) 
    401   { 
    402     result << buf; 
    403   } 
    404   pclose(pipe); 
    405   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "host = <", name, ">, result = <", result.str(), ">"); 
    406  
    407   std::string result_str(result.str()); 
    408   std::string search_token("Name:"); 
    409   std::string::size_type pos = result_str.find(search_token); 
    410   if (pos == std::string::npos) 
    411   { 
    412     RRLIB_LOG_PRINT(ERROR, "Could not find token <", search_token, "> in nslookup result ... returning <", inet_ntoa(address), ">"); 
    413     return address; 
    414   } 
    415   result_str.erase(0, pos + search_token.length()); 
    416   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "pos = ", pos); 
    417  
    418   search_token = "Address:"; 
    419   pos = result_str.find(search_token); 
    420   if (pos == std::string::npos) 
    421   { 
    422     RRLIB_LOG_PRINT(ERROR, "Could not find token <", search_token, "> in nslookup result ... returning <", inet_ntoa(address), ">"); 
    423     return address; 
    424   } 
    425   RRLIB_LOG_PRINT(USER, "pos = ", pos); 
    426  
    427   std::string found_name(result_str.substr(0, pos)); 
    428   sStringUtils::TrimWhitespace(found_name); 
    429   std::string ip(result_str.substr(pos + search_token.length())); 
    430   sStringUtils::TrimWhitespace(ip); 
    431  
    432   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "found_name <", found_name, "> , name <", name, ">"); 
    433  
    434   if (found_name != name) 
    435   { 
    436     RRLIB_LOG_PRINT(ERROR, "nslookup failed .... returning <", inet_ntoa(address), ">"); 
    437     return address; 
    438   } 
    439   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "found_name <", found_name, "> , ip <", ip, ">"); 
    440  
    441   inet_aton(ip.c_str(), &address); 
    442  
    443   return address; 
    444 } // HostToIpViaNslookup() 
    445  
    446  
    447 //---------------------------------------------------------------------- 
    448 // class sFileIOUtils::HostToIpViaHost() 
    449 //---------------------------------------------------------------------- 
    450 struct in_addr sFileIOUtils::HostToIpViaHost(const std::string & name) 
    451 { 
    452   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "started with host <", name, ">"); 
    453   struct in_addr address; 
    454   address.s_addr = 0; 
    455  
    456   std::stringstream command; 
    457   command << "host -t A \"" << name << "\""; 
    458   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "command = <", command.str(), ">"); 
    459   FILE * pipe = popen(command.str().c_str(), "r"); 
    460   if (!pipe) 
    461   { 
    462     perror("error\n"); 
    463     return address; 
    464   } 
    465   std::stringstream result; 
    466   char buf[ 512 ]; 
    467   while (fgets(buf, 512, pipe) != NULL) 
    468   { 
    469     result << buf; 
    470   } 
    471   pclose(pipe); 
    472   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "host = <", name, "> , result = <", result.str(), ">"); 
    473  
    474   string result_str(result.str()); 
    475   string search("has address"); 
    476   size_t pos = result_str.find(search); 
    477   if (pos == string::npos) 
    478   { 
    479     RRLIB_LOG_PRINT(ERROR, "could not find address token in host call"); 
    480     return address; 
    481   } 
    482   string address_str(result_str.substr(pos + search.size(), result_str.size())); 
    483   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "token = <", address_str, ">"); 
    484   sStringUtils::TrimWhitespace(address_str); 
    485   if (inet_aton(address_str.c_str(), &address) == 0) 
    486     RRLIB_LOG_PRINT(ERROR, "could not resolve address <", address_str, ">"); 
    487   else 
    488     RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "address = <", inet_ntoa(address), ">"); 
    489   return address; 
    490 } // HostToIpViaHost() 
    491  
    492 //---------------------------------------------------------------------- 
    493 // class sFileIOUtils::HostToIpViaHostsFile() 
    494 //---------------------------------------------------------------------- 
    495 struct in_addr sFileIOUtils::HostToIpViaHostsFile(const std::string & name) 
    496 { 
    497   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "started with host <", name, ">"); 
    498   struct in_addr address; 
    499   address.s_addr = 0; 
    500  
    501   ifstream hosts_file("/etc/hosts"); 
    502  
    503   char line[ 1024 ]; 
    504   std::string line_str, ip_address; 
    505   std::vector<std::string> tokens; 
    506   bool found(false); 
    507   while (!hosts_file.eof() && !found) 
    508   { 
    509     hosts_file.getline(line, 1024); 
    510     line_str = std::string(line); 
    511     if (line_str.length() > 0 && line_str.find("#") == std::string::npos) 
    512     { 
    513       tokens.clear(); 
    514       sStringUtils::Tokenize(line_str, tokens, " \t"); 
    515  
    516       if (find(tokens.begin(), tokens.end(), name) != tokens.end()) 
    517       { 
    518         for (std::vector<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter) 
    519         { 
    520           if (inet_aton(iter->c_str(), &address)) 
    521           { 
    522             ip_address = *iter; 
    523             RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "sFileIOUtils::HostToIpViaHostsFile() >>> got ip <", ip_address, "> of host <", name, "> from hosts file"); 
    524             found = true; 
    525             break; 
    526           } 
    527         } 
    528       } 
    529     } 
    530   } 
    531   hosts_file.close(); 
    532   return address; 
    533 } // HostToIpViaHostsFile() 
    534  
    535  
    536 //---------------------------------------------------------------------- 
    537 // class sFileIOUtils::HostToIp() 
    538 //---------------------------------------------------------------------- 
    539 struct in_addr sFileIOUtils::HostToIp(const std::string & name) 
    540 { 
    541   RRLIB_LOG_PRINT(USER, "started with host <", name, ">"); 
    542  
    543   struct in_addr address; 
    544   address.s_addr = 0; 
    545  
    546   if ((address = sFileIOUtils::HostToIpViaHostsFile(name)).s_addr != 0) 
    547   { 
    548     RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> from hosts_file"); 
    549   } 
    550   else if ((address = sFileIOUtils::HostToIpViaHost(name)).s_addr != 0) 
    551   { 
    552     RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> via <host -t A>"); 
    553   } 
    554   else if ((address = sFileIOUtils::HostToIpViaNslookup(name)).s_addr != 0) 
    555   { 
    556     RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> via <nslookup>"); 
    557   } 
    558   else 
    559   { 
    560     RRLIB_LOG_PRINT(ERROR, "could not get ip of host <", name, ">"); 
    561   } 
    562  
    563   return address; 
    564 } // HostToIp() 
    565  
    566  
    567 //---------------------------------------------------------------------- 
    568 // class sFileIOUtils::RSyncFile() 
    569 //---------------------------------------------------------------------- 
    570 int sFileIOUtils::RSyncFile(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::string& source_file_name, const std::string& target_directory, const std::string &optional_rsync_flags) 
    571 { 
    572   std::stringstream rsync_command; 
    573   rsync_command << "rsync -av" << optional_rsync_flags << " "; 
    574   if (source_host_ip_address == "") 
    575   { 
    576     rsync_command << inet_ntoa(sFileIOUtils::HostToIp(source_host_name)); 
    577   } 
    578   else 
    579   { 
    580     rsync_command << source_host_ip_address; 
    581   } 
    582   rsync_command << ":" 
    583                 << source_directory 
    584                 << source_file_name 
    585                 << " " 
    586                 << target_directory; 
    587  
    588   RRLIB_LOG_PRINT(USER, "executing <", rsync_command.str(), "> ..."); 
    589   int ret = system(rsync_command.str().c_str()); 
    590   RRLIB_LOG_PRINT(USER, " ... done.\n"); 
    591  
    592   RRLIB_LOG_PRINT(ERROR, "finished with result ", ret); 
    593   return ret; 
    594 } // RSyncFile() 
    595  
    596  
    597 //---------------------------------------------------------------------- 
    598 // class sFileIOUtils::RSyncFiles() 
    599 //---------------------------------------------------------------------- 
    600 int sFileIOUtils::RSyncFiles(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::vector<std::string>& source_file_names, const std::string& target_directory, const std::string &optional_rsync_flags) 
    601 { 
    602   std::string file_list_file_name(target_directory + "sfileioutils_rsyncfiles.txt"); 
    603   ofstream tmp_file_list(file_list_file_name.c_str()); 
    604   std::ostream_iterator<std::string> tmp_file_list_iterator(tmp_file_list, "\n"); 
    605   unique_copy(source_file_names.begin(), source_file_names.end(), tmp_file_list_iterator); 
    606   tmp_file_list.close(); 
    607  
    608   std::stringstream rsync_command; 
    609   rsync_command << "rsync -avr" << optional_rsync_flags << " --files-from=" << file_list_file_name << " "; 
    610   if (source_host_ip_address == "") 
    611   { 
    612     rsync_command << inet_ntoa(sFileIOUtils::HostToIp(source_host_name)); 
    613   } 
    614   else 
    615   { 
    616     rsync_command << source_host_ip_address; 
    617   } 
    618   rsync_command << ":" 
    619                 << source_directory 
    620                 << " " 
    621                 << target_directory; 
    622  
    623   RRLIB_LOG_PRINT(USER, "executing <", rsync_command.str(), "> ..."); 
    624   int ret = system(rsync_command.str().c_str()); 
    625   RRLIB_LOG_PRINT(USER, "... done.\n"); 
    626  
    627   RRLIB_LOG_PRINT(ERROR, "finished with result ", ret); 
    628   return ret; 
    629 } // RSyncFiles() 
    630  
    631  
    632 //---------------------------------------------------------------------- 
    633 // class sFileIOUtils::CheckAndGetFile() 
    634 //---------------------------------------------------------------------- 
    635 bool sFileIOUtils::CheckAndGetFile(const std::string &file_name, std::string &full_local_file_name, const std::string& resource_repository, const std::string& resource_server, const std::string& local_resource_directory, const std::string& server_resource_directory, bool use_cache) 
    636 { 
    637   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "started with local_resource_directory <", local_resource_directory, ">, resource_repository <", resource_repository, ">, file_name <", file_name, ">, resource_server <", resource_server, ">, server_resource_directory <", server_resource_directory, ">"); 
    638  
    639   //!##################################################################################################### 
    640   //! step 0: check whether token "resource_repository + filename" is in local cache 
    641   //!         and thus file is *assumed* to have already been loaded since last clearing of cache 
    642   //!##################################################################################################### 
    643   std::map<std::string, std::string>::iterator resource_cache_entry; 
    644   if (use_cache && (resource_cache_entry = sFileIOUtils::resource_cache.find(resource_repository + file_name)) != sFileIOUtils::resource_cache.end()) 
    645   { 
    646     full_local_file_name = resource_cache_entry->second; 
    647     return true; 
    648   } 
    649  
    650  
    651   //!##################################################################################################### 
    652   //! step 1: check whether file can be loaded from local host in local directory 
    653   //!         location of file is: "./ + resource_repository + filename" 
    654   //!         --> resource_repository is extracted from relative path within given scene description file 
    655   //!##################################################################################################### 
    656   full_local_file_name = "./" + resource_repository + file_name; 
    657  
    658   RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "1. check: trying to load <", full_local_file_name, ">"); 
    659   ifstream try_1(full_local_file_name.c_str()); 
    660   if (try_1) 
    661   { 
    662     try_1.close(); 
    663   } 
    664   else 
    665   { 
    666     //!################################################################################################################# 
    667     //! step 2: check whether file can be loaded from local host in given local_resource_directory 
    668     //!         location of file is: "local_resource_directory + resource_repository + filename" 
    669     //!         --> resource_repository is extracted from relative path within given scene description file 
    670     //!################################################################################################################# 
    671  
    672     // replace possible environment variables in local_resource_directory by corresponding values 
    673     std::string expanded_local_resource_directory; 
    674     if (!sFileIOUtils::ShellExpandFilename(expanded_local_resource_directory, local_resource_directory + "/")) 
    675     { 
    676       RRLIB_LOG_PRINT(ERROR, "Could not expand local resource directory!"); 
    677       return false; 
    678     } 
    679     full_local_file_name = (expanded_local_resource_directory + resource_repository + file_name); 
    680  
    681     RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "2. check: trying to load <", full_local_file_name, ">"); 
    682     ifstream try_2(full_local_file_name.c_str()); 
    683     if (try_2) 
    684     { 
    685       try_2.close(); 
    686     } 
    687     else 
    688     { 
    689       //!################################################################################################################# 
    690       //! step 3: check whether file can be loaded from resource server in given server_resource_directory 
    691       //!         location of file is: "server_resource_directory + resource_repository + filename" 
    692       //!################################################################################################################# 
    693  
    694       // get local host name 
    695       std::string local_host; 
    696       if (use_cache && sFileIOUtils::cached_local_host.length() > 0) 
    697       { 
    698         local_host = sFileIOUtils::cached_local_host; 
    699         RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "retrieved local_host <", local_host, "> from cache"); 
    700       } 
    701       else 
    702       { 
    703         local_host = sFileIOUtils::cached_local_host = sFileIOUtils::GetHostName(); 
    704       } 
    705  
    706       // get server ip address 
    707       std::string server(resource_server); 
    708       std::string server_ip_address; 
    709       if (server != "") 
    710       { 
    711         // retrieve ip address of optional resource server 
    712         std::map<std::string, std::string>::iterator pos; 
    713         if (use_cache && ((pos = sFileIOUtils::host_name_to_ip_cache.find(server)) != sFileIOUtils::host_name_to_ip_cache.end())) 
    714         { 
    715           server_ip_address = pos->second; 
    716           RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "retrieved ip address <", server_ip_address, "> of host <", server, "> from cache"); 
    717         } 
    718         else 
    719         { 
    720           server_ip_address = inet_ntoa(sFileIOUtils::HostToIp(server)); 
    721           RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got ip address <", server_ip_address, "> of host <", server, ">"); 
    722  
    723           if (server_ip_address == "0.0.0.0") 
    724           { 
    725             RRLIB_LOG_PRINT(ERROR, "zero ip address ... clearing server"); 
    726             server = ""; 
    727           } 
    728           else 
    729           { 
    730             sFileIOUtils::host_name_to_ip_cache[ server ] = server_ip_address; 
    731           } 
    732         } 
    733       } 
    734  
    735       // start check 
    736       if (server != "")   //&& server != local_host) 
    737       { 
    738         RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "3. check: trying to rsync <", server_ip_address, ":", server_resource_directory + resource_repository + file_name, "> to <", expanded_local_resource_directory, ">"); 
    739  
    740         // rsync from <resource_server:server_resource_directory + resource_repository> 
    741         //       to <local_resource_directory> on local host 
    742  
    743         if (sFileIOUtils::RSyncFile("", server_ip_address, server_resource_directory + "/./" + resource_repository, file_name, expanded_local_resource_directory, "R") != 0) 
    744         { 
    745           RRLIB_LOG_PRINT(ERROR, "could not rsync file <", file_name, "> from server <", server, "> at <", server_resource_directory + resource_repository, "> ... skipping"); 
    746           return false; 
    747         } 
    748         full_local_file_name = (expanded_local_resource_directory + resource_repository + file_name); 
    749       } 
    750       else 
    751       { 
    752         RRLIB_LOG_PRINT(ERROR, "could neither load <", file_name, "> locally nor from resource server ... skipping"); 
    753         return false; 
    754       } 
    755     } 
    756   } 
    757  
    758   //! ------------------------------- final check just to be sure that requested file is available -------------------- 
    759   ifstream try_3(full_local_file_name.c_str()); 
    760   if (!try_3) 
    761   { 
    762     RRLIB_LOG_PRINT(ERROR, "final check failed: could not load <", full_local_file_name, "> ... skipping"); 
    763     return false; 
    764   } 
    765   else 
    766   { 
    767     try_3.close(); 
    768   } 
    769   sFileIOUtils::resource_cache[ resource_repository + file_name ] = full_local_file_name; 
    770   return true; 
    771 } // CheckAndGetFile() 
    772  
    773  
    774 //---------------------------------------------------------------------- 
    775 // class sFileIOUtils::ClearCaches() 
    776 //---------------------------------------------------------------------- 
    777 void sFileIOUtils::ClearCaches() 
    778 { 
    779   sFileIOUtils::cached_local_host = ""; 
    780   sFileIOUtils::host_name_to_ip_cache.clear(); 
    781 } // ClearCaches() 
    782  
    783  
    784 //---------------------------------------------------------------------- 
    785 // class sFileIOUtils::ClearResourceCache() 
    786 //---------------------------------------------------------------------- 
    787 void sFileIOUtils::ClearResourceCache() 
    788 { 
    789   sFileIOUtils::resource_cache.clear(); 
    790 } // ClearResourceCache() 
    791161 
    792162//---------------------------------------------------------------------- 
  • sFileIOUtils.h

    r111 r142  
    3737 
    3838//rrlib includes 
    39 #include "rrlib/util/sStringUtils.h" 
     39#include "rrlib/util/string.h" 
    4040#include "rrlib/logging/messages.h" 
    4141 
     
    5151#include <boost/iostreams/filter/bzip2.hpp> 
    5252 
    53 extern "C" 
    54 { 
    55 #include <arpa/inet.h> // inet_ntoa 
    56 } 
    57  
    5853//---------------------------------------------------------------------- 
    5954// Namespace declaration 
     
    7267   * \return Returns whether or not the read was possible 
    7368   */ 
    74   static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files); 
     69  static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files) 
     70  __attribute__((deprecated("USE rrlib::util::fileio::GetAllFilesInDirectory()"))); 
    7571 
    7672  /*! 
     
    7975   * \return Returns a string containing the current directory 
    8076   */ 
    81   static std::string GetCurrentDirectory(); 
     77  static std::string GetCurrentDirectory() 
     78  __attribute__((deprecated("USE rrlib::util::fileio::GetCurrentDirectory()"))); 
     79 
    8280  /*! 
    8381   * \brief Setsthe current directory 
     
    8684   * \return returns an error code similar to the chdir C command 
    8785   */ 
    88   static int ChangeCurrentDirectory(const std::string& dir); 
     86  static bool ChangeCurrentDirectory(const std::string& dir) 
     87  __attribute__((deprecated("USE rrlib::util::fileio::ChangeCurrentDirectory()"))); 
    8988 
    9089  /*! 
     
    9594   * The existance of the folder is not regarded as a failure here! 
    9695   */ 
    97   static bool CreateDirectory(const std::string& path); // throw(runtime_error); 
     96  static bool CreateDirectory(const std::string& path) 
     97  __attribute__((deprecated("USE rrlib::util::fileio::CreateDirectory()"))); 
    9898 
    9999  /*! 
     
    102102   * \return the new temp file 
    103103   */ 
    104   static std::string CreateTempfile(); 
     104  static std::string CreateTempfile() 
     105  __attribute__((deprecated("USE rrlib::util::fileio::CreateTempFile()"))); 
    105106 
    106107  /*! 
    107108   * \brief Removes file with given name. 
    108109   */ 
    109   static void DeleteFile(const std::string& filename); 
    110  
    111   /*! 
    112    * \brief Calls "gzip" on input file and writes result to output file. 
    113    * 
    114    * \note output file gets suffix ".gz" automatically if not yet the case. 
    115    */ 
    116   static void CompressFile(const std::string& input_filename, std::string& output_filename); 
    117  
    118   /*! 
    119    * \brief Calls "gunzip" on input file and writes result to output file. 
    120    * 
    121    * \note input file must have suffix ".gz". 
    122    */ 
    123   static void DecompressFile(const std::string& input_filename, const std::string& output_filename); 
    124  
     110  static void DeleteFile(const std::string& filename) 
     111  __attribute__((deprecated("USE rrlib::util::fileio::DeleteFile()"))); 
    125112 
    126113  /*! 
     
    129116   * \returns true if file exists 
    130117   */ 
    131   static bool FileExists(const std::string &filename); 
     118  static bool FileExists(const std::string &filename) 
     119  __attribute__((deprecated("USE rrlib::util::fileio::FileExists()"))); 
    132120 
    133121  /*! 
     
    136124   * \returns # lines 
    137125   */ 
    138   static size_t CountLineNumbers(const std::string &filename); 
     126  static size_t CountLineNumbers(const std::string &filename) 
     127  __attribute__((deprecated("USE rrlib::util::fileio::CountLineNumbers()"))); 
    139128 
    140129 
     
    148137  template <class ConstForwardIterator> 
    149138  static bool WriteContainerToFile(ConstForwardIterator begin, ConstForwardIterator end, const std::string& filename) 
    150   { 
    151     std::ofstream output_file_stream; 
    152     RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">"); 
    153     if (sStringUtils::EndsWith(filename, ".bz2")) 
    154     { 
    155       RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "compressing"); 
    156       boost::iostreams::filtering_ostream out; 
    157       out.push(boost::iostreams::bzip2_compressor()); 
    158       output_file_stream.open(filename.c_str()); 
    159       if (output_file_stream) 
    160       { 
    161         out.push(output_file_stream); 
    162         std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(out)); 
    163         return true; 
    164       } 
    165     } 
    166     else 
    167     { 
    168       output_file_stream.open(filename.c_str()); 
    169       if (output_file_stream) 
    170       { 
    171         std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(output_file_stream)); 
    172         output_file_stream.close(); 
    173         return true; 
    174       } 
    175     } 
    176     return false; 
    177   } 
     139  __attribute__((deprecated("USE rrlib::serialization instead"))); 
     140 
    178141 
    179142  template <class Container> 
    180143  static bool WriteContainerToFile(const Container &container, const std::string& filename) 
    181   { 
    182     return WriteContainerToFile(container.begin(), container.end(), filename); 
    183   } 
     144  __attribute__((deprecated("USE rrlib::serialization instead"))); 
     145 
    184146 
    185147  /*! 
     
    192154  template <class Container> 
    193155  static bool ReadContainerFromFile(Container &container, const std::string& filename) 
    194   { 
    195     std::ifstream input_file_stream; 
    196     RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">"); 
    197     if (sStringUtils::EndsWith(filename, ".bz2")) 
    198     { 
    199       RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "decompressing"); 
    200       boost::iostreams::filtering_istream in; 
    201       in.push(boost::iostreams::bzip2_decompressor()); 
    202       input_file_stream.open(filename.c_str()); 
    203       if (input_file_stream) 
    204       { 
    205         in.push(input_file_stream); 
    206         std::copy(std::istream_iterator<typename Container::value_type>(in), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container)); 
    207         RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename); 
    208         return true; 
    209       } 
    210     } 
    211     else 
    212     { 
    213       input_file_stream.open(filename.c_str()); 
    214       if (input_file_stream) 
    215       { 
    216         std::copy(std::istream_iterator<typename Container::value_type>(input_file_stream), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container)); 
    217         RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename); 
    218       } 
    219       input_file_stream.close(); 
    220       return true; 
    221     } 
    222     return false; 
    223   } 
    224  
    225  
     156  __attribute__((deprecated("USE rrlib::serialization instead"))); 
    226157 
    227158 
     
    232163   * \returns Whether the method could be executed without failure or not 
    233164   */ 
    234   static bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__)); 
     165  static bool ShellExpandFilename(std::string &file_name) 
     166  __attribute__((__warn_unused_result__)) 
     167  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()"))); 
    235168 
    236169  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value. 
     
    240173   * \returns Expanded file name 
    241174   */ 
    242   static std::string ShellExpandFilename(const std::string &file_name)  __attribute__((__warn_unused_result__)); 
     175  static std::string ShellExpandFilename(const std::string &file_name) 
     176  __attribute__((__warn_unused_result__)) 
     177  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()"))); 
    243178 
    244179  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value. 
     
    251186  * \returns Whether the method could be executed without failure or not 
    252187  */ 
    253   static bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__)); 
    254  
     188  static bool ShellExpandFilename(std::string &result, const std::string& file_name) 
     189  __attribute__((__warn_unused_result__)) 
     190  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()"))); 
    255191 
    256192  /*! 
     
    261197   *    \param file_ext file extension (output) 
    262198   */ 
    263   static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext); 
    264  
    265  
    266   /*! 
    267    * Retrieves network host name via "hostname" and returns result. 
    268    */ 
    269   static std::string GetHostName(); 
    270  
    271   /*! 
    272    * Retrieves ip address of host with given network name via "gethostbyname" and returns result. 
    273    */ 
    274   static struct in_addr HostToIpViaGetHostByName(const std::string& name); 
    275  
    276   /*! 
    277    * Retrieves ip address of host with given network name via "nslookup" and returns result. 
    278    */ 
    279   static struct in_addr HostToIpViaNslookup(const std::string& name); 
    280  
    281   /*! 
    282    * Retrieves ip address of host with given network name via "host" and returns result. 
    283    */ 
    284   static struct in_addr HostToIpViaHost(const std::string& name); 
    285  
    286   /*! 
    287    * Retrieves ip address of host with given network name from "/etc/hosts" and returns result. 
    288    */ 
    289   static struct in_addr HostToIpViaHostsFile(const std::string& name); 
    290  
    291   /*! 
    292    * Retrieves ip address of host with given network name using 
    293    * 1. \sa HostToIpViaHostsFile() 
    294    * 2. \sa HostToIpViaHostx() 
    295    * 2. \sa HostToIpViaNslookup() 
    296    */ 
    297   static struct in_addr HostToIp(const std::string& name); 
    298  
    299   /*! 
    300    * Fetches file with given source_file_name from server (either addressed by network name or ip address) at given source_directory 
    301    * and puts it to the given target_directory (via <system( rsync -av ...)> ). 
    302    * 
    303    * \params source_host_name network name of server 
    304    * \params source_host_ip_address ip address name of server (used alternatively to its name) 
    305    * \params source_directory directory on server 
    306    * \params source_file_name file to fetch from server 
    307    * \params target_directory local directory for fetched file 
    308    * \params optional_rsync_flags optional flags for rsync command, appended to -av 
    309    * 
    310    * \returns result of system call 
    311    */ 
    312   static int RSyncFile(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::string& source_file_name, const std::string& target_directory, const std::string &optional_rsync_flags = ""); 
    313  
    314  
    315   /*! 
    316    * Fetches files with given source_file_names from server (either addressed by network name or ip address) at given source_directory 
    317    * and puts it to the given target_directory (via <system( rsync -avr --files-from ...)> ). Uses temp file on local host to 
    318    * compose list with files to fetch. 
    319    * 
    320    * \params source_host_name network name of server 
    321    * \params source_host_ip_address ip address name of server (used alternatively to its name) 
    322    * \params source_directory directory on server 
    323    * \params source_file_names files to fetch from server 
    324    * \params target_directory local directory for fetched file 
    325    * \params optional_rsync_flags optional flags for rsync command, appended to -avr 
    326    * 
    327    * \returns result of system call 
    328    */ 
    329   static int RSyncFiles(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::vector<std::string>& source_file_names, const std::string& target_directory, const std::string &optional_rsync_flags = ""); 
    330  
    331  
    332   /*! 
    333    * Checks whether given file is available on local host or can be rsync'ed from resource server and returns full qualified file_name on local host. 
    334    * The following checks are performed (with decreasing priority): 
    335    * 1. check location of file: "./ + resource_repository + filename" 
    336    * 2. check location of file: "local_resource_directory + resource_repository + filename" 
    337    * 3. check location of file: "server_resource_directory + resource_repository + filename" --> rsync to "local_resource_directory + resource_repository + file_name" 
    338    * As soon as one check succeeds the remaining checks are not executed. 
    339    * 
    340    * \param file_name name of file to search and fetch (may contain relative path) 
    341    * \param full_local_file_name full qualified name of file on local host after successful check/fetch operation (result) 
    342    * \param resource_repository relative path to be used as resource repository (on local host and on server, may be empty) 
    343    * \param resource_server network name of resource server (optional) 
    344    * \param local_resource_directory absolute path to resource_repository on local host (may be empty) 
    345    * \param server_resource_directory absolute path to resource_repository on server (may be empty) 
    346    * \param use_cache whether to take local host name and server ip address from local static cache or not 
    347    * 
    348    * \returns success of check/fetch operation 
    349    */ 
    350   static bool CheckAndGetFile(const std::string &file_name, std::string &full_local_file_name, const std::string& resource_repository = "", const std::string& resource_server = "", const std::string& local_resource_directory = "", const std::string& server_resource_directory = "", bool use_cache = false); 
    351  
    352   /*! 
    353    * Clear cached local host name and server ip addresses 
    354    * \sa CheckAndGetFile() 
    355    */ 
    356   static void ClearCaches(); 
    357  
    358   /*! 
    359    * Clear cached resource file names 
    360    * \sa CheckAndGetFile() 
    361    */ 
    362   static void ClearResourceCache(); 
    363  
    364 private: 
    365   static std::string cached_local_host; //!< local static variable to store name of local host 
    366   static std::map<std::string, std::string> host_name_to_ip_cache; //!< local static variable to store server ip addresses 
    367   static std::map<std::string, std::string> resource_cache; //!< local static variable to store names of already loaded resource files 
    368 }; 
     199  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext) 
     200  __attribute__((deprecated("USE rrlib::util::fileio::SplitFullQualifiedFilename()"))); 
     201 
     202}; 
     203 
     204 
     205//====================================================================== 
     206// Implementation of template functions 
     207//====================================================================== 
     208 
     209//---------------------------------------------------------------------- 
     210// class sFileIOUtils::WriteContainerToFile() 
     211//---------------------------------------------------------------------- 
     212template <class ConstForwardIterator> 
     213bool sFileIOUtils::WriteContainerToFile(ConstForwardIterator begin, ConstForwardIterator end, const std::string& filename) 
     214{ 
     215  std::ofstream output_file_stream; 
     216  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">"); 
     217  if (util::EndsWith(filename, ".bz2")) 
     218  { 
     219    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "compressing"); 
     220    boost::iostreams::filtering_ostream out; 
     221    out.push(boost::iostreams::bzip2_compressor()); 
     222    output_file_stream.open(filename.c_str()); 
     223    if (output_file_stream) 
     224    { 
     225      out.push(output_file_stream); 
     226      std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(out)); 
     227      return true; 
     228    } 
     229  } 
     230  else 
     231  { 
     232    output_file_stream.open(filename.c_str()); 
     233    if (output_file_stream) 
     234    { 
     235      std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(output_file_stream)); 
     236      output_file_stream.close(); 
     237      return true; 
     238    } 
     239  } 
     240  return false; 
     241}; 
     242 
     243//---------------------------------------------------------------------- 
     244// class sFileIOUtils::WriteContainerToFile() 
     245//---------------------------------------------------------------------- 
     246template <class Container> 
     247bool sFileIOUtils::WriteContainerToFile(const Container &container, const std::string& filename) 
     248{ 
     249  return WriteContainerToFile(container.begin(), container.end(), filename); 
     250}; 
     251 
     252//---------------------------------------------------------------------- 
     253// class sFileIOUtils::ReadContainerFromFile() 
     254//---------------------------------------------------------------------- 
     255template <class Container> 
     256bool sFileIOUtils::ReadContainerFromFile(Container &container, const std::string& filename) 
     257{ 
     258  std::ifstream input_file_stream; 
     259  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">"); 
     260  if (util::EndsWith(filename, ".bz2")) 
     261  { 
     262    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "decompressing"); 
     263    boost::iostreams::filtering_istream in; 
     264    in.push(boost::iostreams::bzip2_decompressor()); 
     265    input_file_stream.open(filename.c_str()); 
     266    if (input_file_stream) 
     267    { 
     268      in.push(input_file_stream); 
     269      std::copy(std::istream_iterator<typename Container::value_type>(in), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container)); 
     270      RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename); 
     271      return true; 
     272    } 
     273  } 
     274  else 
     275  { 
     276    input_file_stream.open(filename.c_str()); 
     277    if (input_file_stream) 
     278    { 
     279      std::copy(std::istream_iterator<typename Container::value_type>(input_file_stream), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container)); 
     280      RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename); 
     281    } 
     282    input_file_stream.close(); 
     283    return true; 
     284  } 
     285  return false; 
     286}; 
     287 
     288 
    369289//---------------------------------------------------------------------- 
    370290// End of namespace declaration 
  • tests/make.xml

    r115 r142  
    66  <program name="tagged_pointer" sources="tagged_pointer.cpp" /> 
    77  <program name="string" sources="string.cpp" /> 
     8  <program name="fileio" sources="fileio.cpp" /> 
    89 
    910</targets> 
Note: See TracChangeset for help on using the changeset viewer.