coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fs_win.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * *
12  * Redistributions in binary form must reproduce the above copyright *
13  * notice, this list of conditions and the following disclaimer in the *
14  * documentation and/or other materials provided with the distribution. *
15  * *
16  * Neither the name of Mike Kinghan nor the names of its contributors *
17  * may be used to endorse or promote products derived from this software *
18  * without specific prior written permission. *
19  * *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
31  * DAMAGE. *
32  * *
33  **************************************************************************/
34 
39 #include "platform.h"
40 
41 #ifdef WINDOWS
42 
43 #include "filesys.h"
44 #include "syserr.h"
45 #include "diagnostic.h"
46 #include "path.h"
47 #include <cassert>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <windows.h>
51 #include <direct.h>
52 
53 using namespace std;
54 
55 namespace fs {
56 
57 string real_path(string const & relname)
58 {
59  char buf[MAX_PATH] = "";
60  _fullpath(buf,relname.c_str(),MAX_PATH);
61  return buf;
62 }
63 
64 std::string cwd()
65 {
66  char buf[MAX_PATH] = "";
67  GetCurrentDirectory(MAX_PATH,buf);
68  return buf;
69 }
70 
71 bool is_absolute(std::string pathname)
72 {
73  return ((!pathname.empty() && pathname[0] == '\\') ||
74  (pathname.length() >= 3 && pathname[1] == ':' &&
75  pathname[2] == '\\'));
76 }
77 
78 obj_type_t obj_type(string const & name)
79 {
80  WIN32_FILE_ATTRIBUTE_DATA obj_info;
81  int res = GetFileAttributesEx(name.c_str(),GetFileExInfoStandard,&obj_info);
82  if (res) {
83  if (obj_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
84  return OBJ_DIR;
85  } else {
86  return OBJ_FILE;
87  }
88  }
89  return OBJ_NONE;
90 }
91 
92 void make_dir(std::string const & abs_path, bool recursive)
93 {
94  int result = 0;
95  if (recursive) {
96  path_t path(abs_path);
97  int elements = int(path.elements()) - 1;
98  errno = 0;
99  for (int element = 0; !result && element < elements;) {
100  result = _mkdir(path.segment(0,++element).c_str());
101  result *= errno != EEXIST;
102  }
103  }
104  if (!result) {
105  result = _mkdir(abs_path.c_str());
106  result *= errno != EEXIST;
107  }
108  if (result) {
109  abend_cant_create_dir() << "Can't create directory \"" << abs_path
110  << "\": " << system_error_message(errno) << emit();
111  }
112 }
113 
114 permissions get_permissions(std::string const & filename)
115 {
116  return -1;
117 }
118 
119 int set_permissions(std::string const & filename, permissions p)
120 {
121  return 0;
122 }
123 
124 } // namespace fs
125 #endif
126 
127 /* EOF*/
unsigned obj_type_t
Abstract type of filesystem object types.
Definition: filesys.h:61
permissions get_permissions(std::string const &filename)
void make_dir(std::string const &abs_path, bool recursive=true)
Create a directory given an absolute path name.
A file.
Definition: filesys.h:55
std::string segment(size_t start=0, size_t len=std::string::npos) const
Get a sub-sequence of the path's elements as a string.
Definition: path.h:165
bool is_absolute(std::string pathname)
Say whether a filename is absolute or relative.
abend_msg< 16 > abend_cant_create_dir
Report can't create directory.
Definition: diagnostic.h:818
std::string cwd()
Get the absolute real pathname of the current working directory.
chew_mode::name const name
An exemplar chew_mode::name
Definition: chew.h:229
The tag class is inserted in a diagnostic_base to tell it to emit itself.
Definition: diagnostic.h:77
size_t elements() const
Get the number of elements in the path
Definition: path.h:101
int set_permissions(std::string const &filename, permissions p)
std::string abs_path(std::string const &filename)
Get the absolute pathname for a filename.
std::string real_path(std::string const &relname)
Get the absolute real pathname of a file or directory name.
obj_type_t obj_type(std::string const &name)
Get the type of the object putatively designated by a filename.
A directory.
Definition: filesys.h:57
No such object.
Definition: filesys.h:51
int permissions
Type of file permissions mask.
Definition: filesys.h:147
string system_error_message(unsigned errnum)
Get the system error message for a system error number.
Definition: syserr.cpp:89