PandA-2024.02
fileIO.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
45 #ifndef FILEIO_HPP
46 #define FILEIO_HPP
47 
48 #include "gzstream.hpp"
49 #include "refcount.hpp"
50 
51 #include <filesystem>
52 #include <iostream>
53 #include <string>
54 
56 
58 #define TIMEOUT 124
59 
61 #define ULIMIT 153
62 
68 
73 
82 fileIO_istreamRef fileIO_istream_open(const std::string& name);
83 
88 {
89  return fileIO_istreamRef(new std::istringstream(input));
90 }
91 
98 inline fileIO_ostreamRef fileIO_ostream_open(const std::string& name)
99 {
100  return fileIO_ostreamRef(new ogzstream((name).c_str()));
101 }
102 
106 inline void CopyStdout(const std::string& filename)
107 {
108  FILE* filese;
109  filese = fopen(filename.c_str(), "r");
110  char buffer[255];
111  size_t nBytes;
112  while((nBytes = fread(buffer, 1, sizeof(buffer), filese)) > 0)
113  {
114  size_t wBytes = fwrite(buffer, 1, nBytes, stdout);
115  if(wBytes < nBytes)
116  {
117  break;
118  }
119  }
120  fclose(filese);
121 }
122 
123 inline std::string GetCurrentPath()
124 {
125  std::string current_dir;
126  if(getenv("OWD"))
127  {
128  current_dir = getenv("OWD");
129  }
130  else
131  {
132  current_dir = std::filesystem::current_path().string();
133  }
134 #ifdef _WIN32
135  boost::replace_all(current_dir, "\\", "/");
136 #endif
137  return current_dir;
138 }
139 
140 inline std::string GetPath(std::filesystem::path path)
141 {
142  if(path.is_relative())
143  {
144  path = std::filesystem::path(GetCurrentPath()) / path;
145  }
146  return path.string();
147 }
148 
149 inline std::string relocate_compiler_path(const std::string& path, bool resolve_path = false)
150 {
151  if(getenv("MINGW_INST_DIR"))
152  {
153  if(resolve_path)
154  {
155  const auto app_prefix = getenv("MINGW_INST_DIR");
156  return app_prefix + path;
157  }
158  return "$MINGW_INST_DIR" + path;
159  }
160  else if(getenv("APPDIR"))
161  {
162  if(resolve_path)
163  {
164  const auto app_prefix = getenv("APPDIR");
165  return app_prefix + path;
166  }
167  return "$APPDIR" + path;
168  }
169 #ifdef _WIN32
170  else
171  {
172  return "c:/msys64/" + path;
173  }
174 #else
175  else
176  {
177  return path;
178  }
179 #endif
180 }
181 
187 inline void CopyFile(std::filesystem::path file_source, std::filesystem::path file_target)
188 {
189  if(file_source.string() == "-")
190  {
191  std::string line;
192  std::ofstream new_file(file_target.string().c_str());
193  while(std::cin)
194  {
195  std::getline(std::cin, line);
196  new_file << line << std::endl;
197  }
198  }
199  else
200  {
201  std::filesystem::copy_file(file_source, file_target, std::filesystem::copy_options::overwrite_existing);
202  }
203 }
204 
217 int PandaSystem(const ParameterConstRef Param, const std::string& system_command, bool host_exec = true,
218  const std::string& output = "", const unsigned int type = 3, const bool background = false,
219  const size_t timeout = 0);
220 
221 bool NaturalVersionOrder(const std::filesystem::path& _x, const std::filesystem::path& _y);
222 
223 std::filesystem::path unique_path(const std::filesystem::path& model);
224 
225 #endif
fileIO_ostreamRef fileIO_ostream_open(const std::string &name)
this function returns an ostream compressed or not.
Definition: fileIO.hpp:98
refcount< std::istream > fileIO_istreamRef
RefCount type definition for the input stream object.
Definition: fileIO.hpp:66
std::string filename
int input[SIZE]
Definition: hash.h:1
int PandaSystem(const ParameterConstRef Param, const std::string &system_command, bool host_exec=true, const std::string &output="", const unsigned int type=3, const bool background=false, const size_t timeout=0)
System call forcing execution with bash.
Definition: fileIO.cpp:78
fileIO_istreamRef fileIO_istream_open_from_string(const std::string &input)
Create a fileIO_istreamRef starting from a string.
Definition: fileIO.hpp:87
volatile int output[DIM_Y][DIM_X]
Definition: los.h:1
void line(int x1, int y1, int x2, int y2, unsigned int color)
Definition: main.c:110
refcount< std::ostream > fileIO_ostreamRef
RefCount type definition for the input stream object.
Definition: fileIO.hpp:72
bool NaturalVersionOrder(const std::filesystem::path &_x, const std::filesystem::path &_y)
Definition: fileIO.cpp:195
void CopyFile(std::filesystem::path file_source, std::filesystem::path file_target)
Copy file; if target already exist, overwrite.
Definition: fileIO.hpp:187
fileIO_istreamRef fileIO_istream_open(const std::string &name)
this function returns an istream compressed or not.
Definition: fileIO.cpp:55
Template definition of refcount.
CONSTREF_FORWARD_DECL(Parameter)
std::string GetPath(std::filesystem::path path)
Definition: fileIO.hpp:140
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
std::string GetCurrentPath()
Definition: fileIO.hpp:123
void CopyStdout(const std::string &filename)
Copy a file to the standard output.
Definition: fileIO.hpp:106
std::filesystem::path unique_path(const std::filesystem::path &model)
Definition: fileIO.cpp:286
std::string relocate_compiler_path(const std::string &path, bool resolve_path=false)
Definition: fileIO.hpp:149

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13