1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 3 /* 4 * Main authors: 5 * Guido Tack <guido.tack@monash.edu> 6 */ 7 8 /* This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 11 12 #pragma once 13 14 #include <string> 15 #include <vector> 16 17 // Macro so that we can use overloaded wide versions of fstream::open for Windows 18 #ifdef _WIN32 19 #define FILE_PATH(path) MiniZinc::FileUtils::utf8_to_wide(path) 20 #else 21 #define FILE_PATH(path) (path) 22 #endif 23 24 namespace MiniZinc { 25 namespace FileUtils { 26 27 /// Return full path to current executable 28 std::string progpath(); 29 /// Test if \a filename exists 30 bool file_exists(const std::string& filename); 31 /// Test if \a dirname exists and is a directory 32 bool directory_exists(const std::string& dirname); 33 /// Find executable \a filename anywhere on the path 34 /// On Windows, also check extensions .exe and .bat 35 std::string find_executable(const std::string& filename); 36 /// Return full path to file. If \a basePath is not empty, also try resolving 37 /// relative paths with respect to \a basePath. 38 std::string file_path(const std::string& filename, const std::string& basePath = std::string()); 39 /// Return directory name containing \a filename 40 std::string dir_name(const std::string& filename); 41 /// Return base name of \a filename (without dir_name) 42 std::string base_name(const std::string& filename); 43 /// Check whether path is absolute 44 bool is_absolute(const std::string& path); 45 /// Return list of files with extension \a ext in directory \a dir 46 std::vector<std::string> directory_list(const std::string& dir, 47 const std::string& ext = std::string("*")); 48 /// Return share/minizinc directory if present anywhere above the executable 49 std::string share_directory(); 50 /// Return current working directory 51 std::string working_directory(); 52 /// Get global configuration file name (in share/minizinc directory) 53 std::string global_config_file(); 54 /// Get per-user configuration file name (usually in home directory or AppData directory) 55 std::string user_config_file(); 56 /// Get per-user configuration directory name (usually in home directory or AppData directory) 57 std::string user_config_dir(); 58 /// Parse command line \a s into individual arguments 59 std::vector<std::string> parse_cmd_line(const std::string& s); 60 /// Combine individual arguments \a cmd into properly quoted command line 61 std::string combine_cmd_line(const std::vector<std::string>& cmd); 62 63 /// Create a temporary file 64 class TmpFile { 65 private: 66 std::string _name; 67 #ifdef _WIN32 68 std::vector<std::string> _tmpNames; 69 #endif 70 #ifndef _WIN32 71 int _tmpfileDesc; 72 #endif 73 public: 74 // Constructor for file with extension \a ext 75 TmpFile(const std::string& ext); 76 /// Destructor (removes file) 77 ~TmpFile(); name() const78 std::string name() const { return _name; } 79 }; 80 81 /// Create a temporary directory 82 class TmpDir { 83 private: 84 std::string _name; 85 86 public: 87 // Constructor for difrectory 88 TmpDir(); 89 /// Destructor (removes directory) 90 ~TmpDir(); name() const91 std::string name() const { return _name; } 92 }; 93 94 /// Inflate string \a s 95 void inflate_string(std::string& s); 96 /// Deflate string \a s 97 std::string deflate_string(const std::string& s); 98 /// Encode string into base 64 99 std::string encode_base64(const std::string& s); 100 /// Decode string from base 64 101 std::string decode_base64(const std::string& s); 102 103 #ifdef _WIN32 104 /// Convert UTF-16 string to UTF-8 105 std::string wide_to_utf8(const wchar_t* str, int size = -1); 106 /// Convert UTF-16 string to UTF-8 107 std::string wide_to_utf8(const std::wstring& str); 108 /// Convert UTF-8 string to UTF-16 109 std::wstring utf8_to_wide(const std::string& str); 110 #endif 111 } // namespace FileUtils 112 } // namespace MiniZinc 113