Line data Source code
1 : /*
2 : * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include <dirent.h>
34 : #include <unistd.h>
35 : #include <limits.h>
36 : #include <sys/types.h>
37 : #include <sys/stat.h>
38 : #include "common/util/file_util.h"
39 : #include "common/profiler/profiler.h"
40 : #include "common/error.h"
41 :
42 : #ifndef PATH_MAX
43 : // Normally, this should not happen: PATH_MAX is a POSIX constant.
44 : // But if it happens, 1024 should be enough.
45 : // Try to avoid this constant where possible!
46 : #define PATH_MAX 1024
47 : #endif
48 :
49 : using namespace std;
50 :
51 : namespace ug{
52 :
53 : //todo: implement with ftello and fseeko
54 8 : size_t FileSize( const char *filename )
55 : {
56 : PROFILE_FUNC(); // since i/o
57 :
58 8 : if( !FileExists( filename ) ) {
59 0 : UG_THROW( "The file " << filename << " could not be found." );
60 : }
61 :
62 8 : ifstream ifs;
63 8 : ifs.open( filename, ios_base::in );
64 :
65 8 : if( !ifs.good() ) {
66 0 : UG_THROW( "The file " << filename << " could not be opened." );
67 : }
68 8 : ifs.seekg( 0, ios_base::end );
69 8 : size_t length = ifs.tellg();
70 8 : ifs.close();
71 8 : return length;
72 8 : }
73 :
74 0 : bool DirectoryExists(const char* dirname)
75 : {
76 0 : DIR* curDir = opendir(dirname);
77 0 : if(!curDir)
78 : return false;
79 :
80 0 : closedir(curDir);
81 0 : return true;
82 : }
83 :
84 :
85 : // This method returns a list of all directories in a directory
86 2 : bool GetDirectoriesInDirectory(std::vector<std::string>& dirsOut, const char* dir)
87 : {
88 : PROFILE_FUNC();
89 : dirsOut.clear();
90 :
91 2 : DIR* curDir = opendir(dir);
92 :
93 2 : if(!curDir) return false;
94 :
95 : string tFilename;
96 :
97 9 : while(dirent* entry = readdir(curDir)){
98 : // get information on the file
99 : tFilename = dir;
100 8 : tFilename.append("/").append(entry->d_name);
101 :
102 : struct stat statbuf;
103 8 : stat(tFilename.c_str(), &statbuf);
104 :
105 8 : if(S_ISDIR(statbuf.st_mode))
106 8 : dirsOut.push_back(entry->d_name);
107 8 : }
108 :
109 1 : closedir(curDir);
110 :
111 : return true;
112 : }
113 :
114 : // This method returns a list of all files in a directory
115 3 : bool GetFilesInDirectory(std::vector<std::string>& filesOut, const char* dir)
116 : {
117 : PROFILE_FUNC();
118 : filesOut.clear();
119 :
120 3 : DIR* curDir = opendir(dir);
121 :
122 3 : if(!curDir) return false;
123 :
124 : string tFilename;
125 :
126 13 : while(dirent* entry = readdir(curDir)){
127 : // get information on the file
128 : tFilename = dir;
129 11 : tFilename.append("/").append(entry->d_name);
130 :
131 : struct stat statbuf;
132 11 : stat(tFilename.c_str(), &statbuf);
133 :
134 11 : if(S_ISREG(statbuf.st_mode))
135 10 : filesOut.push_back(entry->d_name);
136 11 : }
137 :
138 2 : closedir(curDir);
139 :
140 : return true;
141 : }
142 :
143 :
144 0 : bool CreateDirectoryTMP(const char *directory)
145 : {
146 0 : return CreateDirectory(directory);
147 : }
148 :
149 0 : bool CreateDirectory(const char *directory)
150 : {
151 0 : return mkdir(directory, 0777) == 0;
152 : }
153 :
154 0 : bool CreateDirectory(const char *directory, int mode)
155 : {
156 0 : return mkdir(directory, mode) == 0;
157 : }
158 :
159 0 : std::string GetTmpPath()
160 : {
161 0 : return string("/tmp");
162 : }
163 :
164 0 : void ChangeDirectory(std::string dir)
165 : {
166 0 : if(chdir(dir.c_str()) != 0){
167 0 : UG_THROW("ChangeDirectory failed.");
168 : }
169 0 : }
170 :
171 0 : std::string CurrentWorkingDirectory()
172 : {
173 : char p_w_d [PATH_MAX];
174 :
175 0 : if (getcwd (p_w_d, sizeof (p_w_d)) == NULL)
176 0 : UG_THROW ("CurrentWorkingDirectory: Failed to get the current working path!");
177 0 : return std::string (p_w_d);
178 : }
179 :
180 : }// end of namespace
|