-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.cpp
131 lines (118 loc) · 3.26 KB
/
file_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <[email protected]>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <minizinc/file_utils.hh>
#include <minizinc/config.hh>
#ifdef HAS_PIDPATH
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libproc.h>
#include <unistd.h>
#elif defined(HAS_GETMODULEFILENAME) || defined(HAS_GETFILEATTRIBUTES)
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
namespace MiniZinc { namespace FileUtils {
#ifdef HAS_PIDPATH
std::string progpath(void) {
pid_t pid = getpid();
char path[PROC_PIDPATHINFO_MAXSIZE];
int ret = proc_pidpath (pid, path, sizeof(path));
if ( ret <= 0 ) {
return "";
} else {
std::string p(path);
size_t slash = p.find_last_of("/");
if (slash != std::string::npos) {
p = p.substr(0,slash);
}
return p;
}
}
#elif defined(HAS_GETMODULEFILENAME)
std::string progpath(void) {
char path[MAX_PATH];
int ret = GetModuleFileName(NULL, path, MAX_PATH);
if ( ret <= 0 ) {
return "";
} else {
std::string p(path);
size_t slash = p.find_last_of("/\\");
if (slash != std::string::npos) {
p = p.substr(0,slash);
}
return p;
}
}
#else
std::string progpath(void) {
const int bufsz = 2000;
char path[bufsz+1];
ssize_t sz = readlink("/proc/self/exe", path, bufsz);
if ( sz < 0 ) {
return "";
} else {
path[sz] = '\0';
std::string p(path);
size_t slash = p.find_last_of("/");
if (slash != std::string::npos) {
p = p.substr(0,slash);
}
return p;
}
}
#endif
bool file_exists(const std::string& filename) {
#if defined(HAS_GETFILEATTRIBUTES)
DWORD dwAttrib = GetFileAttributes(filename.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#else
struct stat info;
return stat(filename.c_str(), &info)==0 && (info.st_mode & S_IFREG);
#endif
}
bool directory_exists(const std::string& dirname) {
#if defined(HAS_GETFILEATTRIBUTES)
DWORD dwAttrib = GetFileAttributes(dirname.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#else
struct stat info;
return stat(dirname.c_str(), &info)==0 && (info.st_mode & S_IFDIR);
#endif
}
std::string file_path(const std::string& filename) {
#ifdef _MSC_VER
LPSTR lpBuffer, lpFilePart;
DWORD nBufferLength = GetFullPathName(filename.c_str(), 0,0,&lpFilePart);
if (!(lpBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * nBufferLength)))
return 0;
std::string ret;
if (!GetFullPathName(filename.c_str(), nBufferLength, lpBuffer, &lpFilePart)) {
ret = "";
} else {
ret = std::string(lpBuffer);
}
LocalFree(lpBuffer);
return ret;
#else
char* rp = realpath(filename.c_str(), NULL);
std::string rp_s(rp);
free(rp);
return rp_s;
#endif
}
}}