Changeset 145:ace18c6c0493 in rrlib_util


Ignore:
Timestamp:
17.05.2016 16:00:07 (4 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Rebase:
64333035623138393666396266343736616636373365663064666166353538616637646630323033
Message:

cleaned code; added function for retrieving extension from a file name

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • fileio.cpp

    r142 r145  
    179179// DeleteFile() 
    180180//---------------------------------------------------------------------- 
    181 bool DeleteFile(const std::string& filename) 
    182 { 
    183   if (remove(filename.c_str()) != 0) 
    184   { 
    185     throw runtime_error("Could not delete file <" + filename + ">: " + strerror(errno)); 
     181bool DeleteFile(const std::string& file_name) 
     182{ 
     183  if (remove(file_name.c_str()) != 0) 
     184  { 
     185    throw runtime_error("Could not delete file <" + file_name + ">: " + strerror(errno)); 
    186186  } 
    187187  return true; 
     
    203203// FileExists() 
    204204//---------------------------------------------------------------------- 
    205 bool FileExists(const std::string &filename) 
    206 { 
    207   return (access(filename.c_str(), F_OK) != -1); 
     205bool FileExists(const std::string &file_name) 
     206{ 
     207  return (access(file_name.c_str(), F_OK) != -1); 
    208208} // FileExists() 
    209209 
     
    212212// CountLineNumbers() 
    213213//---------------------------------------------------------------------- 
    214 size_t CountLineNumbers(const std::string &filename) 
    215 { 
    216   std::ifstream file(filename); 
     214size_t CountLineNumbers(const std::string &file_name) 
     215{ 
     216  std::ifstream file(file_name); 
    217217  if (!file.good()) 
    218     throw runtime_error("Could not open file <" + filename + ">."); 
     218    throw runtime_error("Could not open file <" + file_name + ">."); 
    219219  return std::count(std::istreambuf_iterator<char>(file), 
    220220                    std::istreambuf_iterator<char>(), '\n'); 
     
    323323 
    324324//---------------------------------------------------------------------- 
     325// GetFileExtension() 
     326//---------------------------------------------------------------------- 
     327std::string GetFileExtension(const std::string &file_name) 
     328{ 
     329  auto pos = file_name.rfind('.'); 
     330  if (pos != std::string::npos) 
     331  { 
     332    return file_name.substr(pos + 1); 
     333  } 
     334  else 
     335  { 
     336    RRLIB_LOG_PRINT(WARNING, "No file extension found in file name <", file_name, ">."); 
     337    return ""; 
     338  } 
     339} // GetFileExtension() 
     340 
     341 
     342//---------------------------------------------------------------------- 
    325343// End of namespace declaration 
    326344//---------------------------------------------------------------------- 
  • fileio.h

    r142 r145  
    102102 
    103103/*! 
    104  * \brief Removes the file with \param filename. 
     104 * \brief Removes the file with \param file_name. 
    105105 * \return true if file could be removed 
    106106 * \throws runtime_error if file removal fails 
    107107 */ 
    108 bool DeleteFile(const std::string& filename); 
     108bool DeleteFile(const std::string& file_name); 
    109109 
    110110/*! 
    111  * \brief Removes the directory with \param directoryname. 
     111 * \brief Removes the directory with \param directory_name. 
    112112 * \return true if directory could be removed 
    113113 * \throws runtime_error if directory removal fails 
    114114 */ 
    115 bool DeleteDirectory(const std::string& directoryname); 
     115bool DeleteDirectory(const std::string& directory_name); 
    116116 
    117117/*! 
    118  * \brief Tests if file with \param filename exists 
     118 * \brief Tests if file with \param file_name exists 
    119119 * 
    120120 * \return true if file exists, false otherwise 
    121121 */ 
    122 bool FileExists(const std::string &filename); 
     122bool FileExists(const std::string &file_name); 
    123123 
    124124/*! 
    125  * \brief Counts number of lines in \param filename 
     125 * \brief Counts number of lines in \param file_name 
    126126 * 
    127127 * \return numboer of lines 
    128128 * \throws runtime_error if file cannot be opened 
    129129 */ 
    130 size_t CountLineNumbers(const std::string &filename); 
     130size_t CountLineNumbers(const std::string &file_name); 
    131131 
    132 /*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value. 
     132/*! Expands the \param file_name via a pipe and echo command in order to replace all contained environment variables with their actual value. 
    133133 * 
    134  * \param filename file name to be expanded (will contain the result afterwards) 
     134 * \param file_name file name to be expanded (will contain the result afterwards) 
    135135 * 
    136136 * \return true if file name could be expanded 
    137137 * \throws runtime_error if expansion of file name failed 
    138138 */ 
    139 bool ShellExpandFilename(std::string &filename) __attribute__((__warn_unused_result__)); 
     139bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__)); 
    140140 
    141 /*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value. 
     141/*! Expands the \param file_name via a pipe and echo command in order to replace all contained environment variables with their actual value. 
    142142 * 
    143  * \param filename file name to be expanded 
     143 * \param file_name file name to be expanded 
    144144 * 
    145145 * \return expanded file name in case of success 
     
    148148std::string ShellExpandFilename(const std::string &file_name)  __attribute__((__warn_unused_result__)); 
    149149 
    150 /*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value. 
     150/*! Expands the \param file_name via a pipe and echo command in order to replace all contained environment variables with their actual value. 
    151151 * 
    152  * \param filename file name to be expanded 
     152 * \param file_name file name to be expanded 
    153153 * \param result will contain the expanded file name 
    154154 * 
     
    167167void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext); 
    168168 
     169/* 
     170 * Extracts the file extension from \param file_name 
     171 * \param file_name file name with extension 
     172 * \returns file extension (may be empty) 
     173 */ 
     174std::string GetFileExtension(const std::string &file_name); 
     175 
     176 
    169177//---------------------------------------------------------------------- 
    170178// End of namespace declaration 
  • sFileIOUtils.cpp

    r142 r145  
    159159} // SplitFullQualifiedFilename() 
    160160 
     161//---------------------------------------------------------------------- 
     162// class sFileIOUtils::GetFileExtension() 
     163//---------------------------------------------------------------------- 
     164std::string sFileIOUtils::GetFileExtension(const std::string &filename) 
     165{ 
     166  return fileio::GetFileExtension(filename); 
     167} // GetFileExtension() 
    161168 
    162169//---------------------------------------------------------------------- 
  • sFileIOUtils.h

    r142 r145  
    200200  __attribute__((deprecated("USE rrlib::util::fileio::SplitFullQualifiedFilename()"))); 
    201201 
    202 }; 
    203  
     202  /* 
     203   * Extracts the file extension from a given filename 
     204   * \param filename complete file name 
     205   * \returns file extension if existing, an empty string else 
     206   */ 
     207  static std::string GetFileExtension(const std::string &filename) 
     208  __attribute__((deprecated("USE rrlib::util::fileio::GetFileExtension()"))); 
     209 
     210} __attribute__((deprecated("USE functions from rrlib::util::fileio"))); 
    204211 
    205212//====================================================================== 
Note: See TracChangeset for help on using the changeset viewer.