]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_file_utils.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_file_utils.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_file_utils.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_growable_array.h"
32
33 namespace vogl
34 {
35     class dynamic_string;
36     typedef vogl::vector<dynamic_string> dynamic_string_array;
37
38     struct file_utils
39     {
40         // Returns true if pSrcFilename is older than pDstFilename
41         static bool is_read_only(const char *pFilename);
42         static bool disable_read_only(const char *pFilename);
43         static bool is_older_than(const char *pSrcFilename, const char *pDstFilename);
44         static bool does_file_exist(const char *pFilename);
45         static bool does_dir_exist(const char *pDir);
46         static bool get_file_size(const char *pFilename, uint64_t &file_size);
47         static bool get_file_size(const char *pFilename, uint32 &file_size);
48         static void delete_file(const char *pFilename);
49
50         static bool is_path_separator(char c);
51         static bool is_path_or_drive_separator(char c);
52         static bool is_drive_separator(char c);
53
54         static bool split_path(const char *p, dynamic_string *pDrive, dynamic_string *pDir, dynamic_string *pFilename, dynamic_string *pExt);
55         static bool split_path(const char *p, dynamic_string &path, dynamic_string &filename);
56
57         static bool get_pathname(const char *p, dynamic_string &path);
58         static bool get_filename(const char *p, dynamic_string &filename);
59         static dynamic_string get_pathname(const char *p)
60         {
61             dynamic_string result;
62             get_pathname(p, result);
63             return result;
64         }
65         static dynamic_string get_filename(const char *p)
66         {
67             dynamic_string result;
68             get_filename(p, result);
69             return result;
70         }
71
72         static void combine_path(dynamic_string &dst, const char *pA, const char *pB);
73         static void combine_path(dynamic_string &dst, const char *pA, const char *pB, const char *pC);
74         static void combine_path_and_extension(dynamic_string &dst, const dynamic_string *pA, const dynamic_string *pB, const dynamic_string *pC, const dynamic_string *pExt);
75         static void combine_path_and_extension(dynamic_string &dst, const char *pA, const char *pB, const char *pC, const char *pExt);
76
77         static bool full_path(dynamic_string &path);
78
79         // Returns extension, *without* a leading '.'
80         static bool get_extension(dynamic_string &filename);
81         static bool remove_extension(dynamic_string &filename);
82         static bool create_directory(const char *pPath);
83         static bool create_directories(const dynamic_string &path, bool remove_filename);
84         static bool create_directories_from_full_path(const dynamic_string &path);
85         static void trim_trailing_seperator(dynamic_string &path);
86         static bool add_default_extension(dynamic_string &path, const char *pExt); // pExt should begin with a '.'
87
88         static int wildcmp(const char *pWild, const char *pString);
89
90         // use vogl_free() on the returned buffer
91         static void *read_file_to_heap(const char *pPath, size_t &data_size);
92         static bool read_file_to_vec(const char *pPath, uint8_vec &data);
93
94         // Does not use ftell when reading file (ftell doesn't work on virtual /proc files)
95         static bool read_proc_file(const char *pPath, vogl::growable_array<char, 2048> &data);
96
97         static bool write_buf_to_file(const char *pPath, const void *pData, size_t data_size);
98         static bool write_vec_to_file(const char *pPath, const uint8_vec &data);
99
100         enum read_text_file_flags_t
101         {
102             cRTFTrim = 1,                 // trims whitespace at beginning and end of each line
103             cRTFTrimEnd = 2,              // trims whitepsace at end of each line
104             cRTFIgnoreEmptyLines = 4,     // filters out empty lines (after optional trimming)
105             cRTFIgnoreCommentedLines = 8, // filters out "//" commented lines
106             cRTFPrintErrorMessages = 16,  // print error messages to console
107             cRTFPrintWarningMessages = 32 // print error messages to console
108         };
109
110         // TODO: This only works on files with valid file sizes (i.e. it won't work on /proc/self/status).
111         static bool read_text_file(const char *pPath, dynamic_string_array &lines, uint flags = 0);
112
113         static bool read_text_file_crt(const char *pPath, dynamic_string_array &lines);
114
115         static bool write_text_file(const char *pPath, const dynamic_string_array &lines, bool unix_line_endings = false);
116
117         // write_string_to_file() doesn't write any line ending characters - you're on your own.
118         static bool write_string_to_file(const char *pPath, const dynamic_string &str);
119
120         // pPath can be NULL, if so the system "/tmp/" path is used
121         static dynamic_string generate_temp_filename(const char *pPath = "");
122
123         static bool change_directory(const char *pPath);
124
125         static bool create_directories(const char *pPath, bool remove_filename);
126
127         static char *get_exec_filename(char *pPath, size_t dest_len);
128
129     }; // struct file_utils
130
131 } // namespace vogl