SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
makedir.h
1 #ifndef MAKE_DIR
2 #define MAKE_DIR
3 
4 #ifndef _WIN32
5 
6 //#include "jlss.h"
7 //#include "emalloc.h"
8 
9 #include <stdlib.h>
10 #include <errno.h>
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif /* HAVE_UNISTD_H */
14 
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <cassert>
18 //#include "sysstat.h" /* Fix up for Windows - inc mode_t */
19 
20 #include <pwd.h>
21 
22 typedef struct stat Stat;
23 
24 #ifndef lint
25 /* Prevent over-aggressive optimizers from eliminating ID string */
26 const char jlss_id_mkpath_c[] = "@(#)$Id: mkpath.c,v 1.12 2008/05/19 00:43:33 jleffler Exp $";
27 #endif /* lint */
28 
29 int do_mkdir(const char *path, mode_t mode)
30 {
31  Stat st;
32  int status = 0;
33 
34  if (stat(path, &st) != 0)
35  {
36  /* Directory does not exist */
37  if (mkdir(path, mode) != 0)
38  status = -1;
39  }
40  else if (!S_ISDIR(st.st_mode))
41  {
42  errno = ENOTDIR;
43  status = -1;
44  }
45 
46  return(status);
47 }
48 
55 int mkpath(const char *path, mode_t mode)
56 {
57  char *pp;
58  char *sp;
59  int status;
60  char *copypath = strdup(path);
61 
62  status = 0;
63  pp = copypath;
64  while (status == 0 && (sp = strchr(pp, '/')) != 0)
65  {
66  if (sp != pp)
67  {
68  /* Neither root nor double slash in path */
69  *sp = '\0';
70  status = do_mkdir(copypath, mode);
71  *sp = '/';
72  }
73  pp = sp + 1;
74  }
75  if (status == 0)
76  status = do_mkdir(path, mode);
77  free(copypath);
78  return (status);
79 }
80 
81 
82 
83 bool fileExists(const std::string& filename)
84 {
85  struct stat buf;
86  if (stat(filename.c_str(), &buf) != -1)
87  {
88  return true;
89  }
90  return false;
91 }
92 
93 
94 std::string convertIntToString(int val)
95 {
96  std::ostringstream convert;
97  convert << val;
98  return convert.str();
99 }
100 
101 
102 void createPath(std::string &path)
103 {
104  if (mkpath(path.c_str(), 0777) != 0)
105  {
106  assert(false);
107  }
108 }
109 
110 
111 
112 std::string getUserHomeDirectory()
113 {
114  std::string homedir;
115 
116  // first check for HOME environment variable...
117  char *var = getenv("HOME");
118  if(!var)
119  {
120  struct passwd *pw = getpwuid(getuid());
121  homedir = pw->pw_dir;
122  }
123  else
124  homedir = var;
125 
126  return homedir;
127 }
128 
129 
130 std::string getPMDirectory()
131 {
132  return getUserHomeDirectory() + "/.skepu/";
133 }
134 
135 
136 
137 std::string::value_type up_char(std::string::value_type ch)
138 {
139  return std::use_facet< std::ctype< std::string::value_type > >( std::locale() ).toupper( ch );
140 }
141 
142 std::string::value_type lower_char(std::string::value_type ch)
143 {
144  return std::use_facet< std::ctype< std::string::value_type > >( std::locale() ).tolower( ch );
145 }
146 
148 std::string capitalizeString(const std::string &src)
149 {
150  std::string result;
151  std::transform( src.begin(), src.end(), std::back_inserter( result ), up_char );
152  return result;
153 }
154 
155 
157 std::string unCapitalizeString(const std::string &src)
158 {
159  std::string result;
160  std::transform( src.begin(), src.end(), std::back_inserter( result ), lower_char );
161  return result;
162 }
163 
164 
165 #endif
166 
167 #endif
168 
169