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 <string.h>
32 #include <algorithm>
33
34 #include "file_util.hh"
35
get_file_extension(const std::string & file,std::string & ext)36 bool PF::get_file_extension(const std::string & file, std::string & ext)
37 {
38 #ifdef WIN32
39 const char * dir_separator = "\\";
40 #else
41 const char * dir_separator = "/";
42 #endif
43 std::size_t ext_pos = file.rfind(".");
44 std::size_t dir_pos = file.rfind(dir_separator);
45
46 if(ext_pos>dir_pos+1)
47 {
48 ext.append(file.begin()+ext_pos+1,file.end());
49 std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
50 return true;
51 }
52
53 return false;
54 }
55
56
replace_file_extension(std::string file,std::string new_ext)57 std::string PF::replace_file_extension(std::string file, std::string new_ext)
58 {
59 #ifdef WIN32
60 const char * dir_separator = "\\";
61 #else
62 const char * dir_separator = "/";
63 #endif
64 std::size_t ext_pos = file.rfind(".");
65 std::size_t dir_pos = file.rfind(dir_separator);
66
67 std::string new_file;
68
69 if(ext_pos>dir_pos+1) {
70 new_file.append(file.begin(),file.begin()+ext_pos+1);
71 new_file += new_ext;
72 }
73
74 return new_file;
75 }
76
77
pf_path_get_basename(const gchar * file_name)78 gchar* PF::pf_path_get_basename( const gchar *file_name )
79 {
80 size_t len = strlen( file_name );
81 for(int i = len-1; i >= 0; i--) {
82 //printf("pf_path_get_basename(): i=%d name=\"%s\"\n",i,&(file_name[i]));
83 if( file_name[i] == '/' || file_name[i] == '\\' ) {
84 if( i == 0 ) return g_strdup( &(file_name[i+1]) );
85 if( file_name[i-1] != '\\' ) {
86 // the slash/backslash is not escaped
87 return g_strdup( &(file_name[i+1]) );
88 } else {
89 i -= 1;
90 }
91 }
92 }
93 return g_strdup( &(file_name[0]) );
94 }
95
96
pf_escape_xml(const std::string str)97 std::string PF::pf_escape_xml(const std::string str)
98 {
99 std::string result;
100 for(unsigned int i = 0; i < str.size(); i++) {
101 if( str[i] == '&' ) {
102 result.append( "&" );
103 } else if( str[i] == '<' ) {
104 result.append( "<" );
105 } else {
106 result.push_back( str[i] );
107 }
108 }
109 return result;
110 }
111