1 /*
2 
3  */
4 
5 /*
6 
7     Copyright (C) 2014 Ferrero Andrea
8 
9     This program is free software: you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation, either version 3 of the License, or
12     (at your option) any later version.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  */
24 
25 /*
26 
27     These files are distributed with PhotoFlow - http://aferrero2707.github.io/PhotoFlow/
28 
29  */
30 
31 #include <algorithm>
32 #include <glibmm.h>
33 #include "fileutils.hh"
34 
getFileExtension(const std::string dir_separator,const std::string file,std::string & ext)35 bool PF::getFileExtension(const std::string dir_separator, const std::string file, std::string & ext)
36 {
37   std::size_t ext_pos = file.rfind(".");
38   //std::size_t dir_pos = (dir_separator.empty()) ? 0 : file.rfind(dir_separator);
39 
40   //if(ext_pos>dir_pos+1) {
41   if(ext_pos>0) {
42     ext.append(file.begin()+ext_pos+1,file.end());
43     std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
44     return true;
45   }
46 
47   return false;
48 }
49 
50 
51 
replaceFileExtension(const std::string file,std::string ext)52 std::string PF::replaceFileExtension(const std::string file, std::string ext)
53 {
54   std::string result;
55   std::size_t ext_pos = file.rfind(".");
56 
57   if(ext_pos>0) {
58     result.append(file.begin(), file.begin()+ext_pos+1);
59     result += ext;
60   }
61 
62   return result;
63 }
64 
65 
66 
getFileExtensionLowcase(const std::string dir_separator,const std::string file,std::string & ext)67 bool PF::getFileExtensionLowcase(const std::string dir_separator, const std::string file, std::string & ext)
68 {
69   if(!getFileExtension(dir_separator, file, ext)) return false;
70   std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
71   return true;
72 }
73 
74 
75 
getFileName(const std::string dir_separator,const std::string file,std::string & name)76 bool PF::getFileName(const std::string dir_separator, const std::string file, std::string & name)
77 {
78   gchar* basename = g_path_get_basename( file.c_str() );
79   std::string tname = basename;
80   g_free( basename );
81   std::size_t ext_pos = tname.rfind(".");
82   //std::size_t dir_pos = (dir_separator.empty()) ? 0 : file.rfind(dir_separator);
83 
84   //if(ext_pos>dir_pos+1) {
85   //  name.append(file.begin()+dir_pos, file.begin()+ext_pos);
86   if(ext_pos>0) {
87     name.append(tname.begin(), tname.begin()+ext_pos);
88     //std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
89     return true;
90   }
91 
92   return false;
93 }
94 
95 
96 
getDirName(const std::string dir_separator,const std::string file,std::string & name)97 bool PF::getDirName(const std::string dir_separator, const std::string file, std::string & name)
98 {
99   gchar* dirname = g_path_get_dirname( file.c_str() );
100   name = dirname;
101   g_free( dirname );
102   return true;
103 }
104 
105