ALPSCore reference
filename_operations.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 
12 
13 namespace alps { namespace fs {
14  std::string get_extension(const std::string& filename)
15  {
16  using std::string;
17  typedef std::string::size_type size_type;
18 
19  size_type basedir_len=filename.rfind('/');
20  if (basedir_len==string::npos)
21  basedir_len=0;
22  else
23  ++basedir_len;
24 
25  // special cases: "dir/." and "dir/.."
26  if (filename.compare(basedir_len, string::npos, ".")==0 ||
27  filename.compare(basedir_len, string::npos, "..")==0)
28  return "";
29 
30  size_type last_dot_pos=filename.rfind('.');
31  if (last_dot_pos==string::npos || last_dot_pos<basedir_len) // no dot in filename
32  return "";
33 
34  return filename.substr(last_dot_pos);
35  }
36 
37  std::string remove_extensions(const std::string& filename)
38  {
39  using std::string;
40  typedef std::string::size_type size_type;
41  size_type basedir_len=filename.rfind('/');
42  if (basedir_len==string::npos)
43  basedir_len=0;
44  else if (basedir_len==filename.size()) // special case: "dir/"
45  return filename;
46  else
47  ++basedir_len;
48 
49  // special cases: "dir/." and "dir/.."
50  if (filename.compare(basedir_len, string::npos, ".")==0 ||
51  filename.compare(basedir_len, string::npos, "..")==0)
52  return filename;
53 
54  // special case: "dir/...ext" == "dir/.."+".ext" ("ext" may be "")
55  if (filename.compare(basedir_len, 3, "...")==0)
56  return filename.substr(0,basedir_len+2);
57 
58  // special case: "dir/..ext" == "dir/." + ".ext"
59  if (filename.compare(basedir_len, 2, "..")==0)
60  return filename.substr(0,basedir_len+1);
61 
62  size_type first_dot_pos=filename.find('.', basedir_len);
63  return filename.substr(0,first_dot_pos); // works also if file has no extensions
64  }
65 
66  std::string get_basename(const std::string& filename)
67  {
68  using std::string;
69  typedef std::string::size_type size_type;
70 
71  size_type basedir_len=filename.rfind('/');
72  if (basedir_len==string::npos) return filename;
73  if (basedir_len+1==filename.size()) {
74  return basedir_len==0 ? "/" : "."; // special case of "/"
75  }
76  return filename.substr(basedir_len+1);
77  }
78 
79  std::string get_dirname(const std::string& filename)
80  {
81  using std::string;
82  typedef std::string::size_type size_type;
83 
84  size_type basedir_len=filename.rfind('/');
85  if (basedir_len==string::npos) return "";
86  if (basedir_len==0 && filename.size()!=1) return "/"; // special case of "/file_in_root"
87 
88  return filename.substr(0,basedir_len);
89  }
90 } } // alps::fs::
std::string get_basename(const std::string &path)
Returns the base name of the file (removing leading directories)
std::string remove_extensions(const std::string &filename)
Returns the file name stem with ALL extensions removed.
std::string get_dirname(const std::string &path)
Returns the parent direcory name of the file (removing trailing filename)
std::string get_extension(const std::string &filename)
Returns the file name extension.