CBMC
get_base_name.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: CM Wintersteiger
6 
7 Date:
8 
9 \*******************************************************************/
10 
11 #include "get_base_name.h"
12 
16 std::string get_base_name(const std::string &in, bool strip_suffix)
17 {
18 #ifdef _WIN32
19  // Windows now allows both '/' and '\\'
20  const std::size_t slash_pos = in.find_last_of("\\/");
21 #else
22  const std::size_t slash_pos = in.rfind('/');
23 #endif
24 
25  std::size_t start_pos =
26  (slash_pos == std::string::npos) ? 0 : slash_pos + 1;
27 
28  std::size_t char_count = std::string::npos;
29 
30  if(strip_suffix)
31  {
32  std::size_t dot_pos = in.rfind('.');
33  if(dot_pos != std::string::npos && dot_pos >= start_pos)
34  char_count = dot_pos - start_pos;
35  }
36 
37  return std::string(in, start_pos, char_count);
38 }
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension