1 /*
2  * CRRCsim - the Charles River Radio Control Club Flight Simulator Project
3  *
4  * Copyright (C) 2004-2006, 2008, 2010 Jens Wilhelm Wulf (original author)
5  * Copyright (C) 2005, 2006 Jan Reucker
6  * Copyright (C) 2006 Todd Templeton
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
24 #ifndef __FILESYSTOOLS_H
25 #define __FILESYSTOOLS_H
26 
27 #include <string>
28 #include <vector>
29 
30 /**
31  * Class for file(system) related methods which are not application-specific.
32  */
33 class FileSysTools
34 {
35 public:
36 
37   /**
38    * Utility function which makes sure some path exists -- parts of it are created,
39    * if needed.
40    */
41   static void makeSurePathExists(std::string path);
42 
43   /**
44    *  Get the full path to the given data item
45    *
46    * Finds most local path to a datafile, for example
47    * "sounds/fan.wav", "models/allegro.air" or "textures/beachsand.rgb".
48    * Search order depends on operating system.
49    * This function should be used if one doesn't know where a data file
50    * really is.
51    *
52    *  Pass the filename and relative path to this function and it
53    *  will search for a suitable file in the search path. The
54    *  absolute path to this file, including the filename,
55    *  will be returned.
56    *
57    *  \param item       data item to search for (filename and relative path)
58    *  \param fThrowEx   if set to true, an exception will be thrown if no
59    *                    matching file was found
60    *
61    *  \return absolute path to the file, empty string on error
62    */
63   static std::string getDataPath(std::string item, bool fThrowEx = false);
64 
65   /**
66    * Provide a list of possible locations for data files or
67    * other directories. If you provide a dirname, this string will
68    * be appended to each path. The locations heavily depend on
69    * the platform CRRCsim is running on.
70    *
71    * Example: getSearchPathList(list, "models") will fill "list"
72    * with entries like "models", "/home/johndoe/.crrcsim/models",
73    * "/usr/local/share/crrcsim/models" and so on.
74    *
75    * \param pathlist Reference to a list that will be filled with the pathnames
76    * \param dirname  string to be appended to each path
77    */
78   static void getSearchPathList(std::vector<std::string>& pathlist,
79                                 std::string dirname = "");
80 
81   /**
82    *  Test if a file exists.
83    *
84    *  \param  path   File name and path to test.
85    *  \retval true   if file exists
86    *  \retval false  if file does not exist
87    */
88   static bool fileExists(std::string path);
89 
90   /**
91    * Get the path to the CRRCsim directory inside the
92    * user's home directory. This usually is OS-dependent.
93    */
94   static std::string getHomePath();
95 
96   /**
97    * Set application name
98    *
99    * The application name will be used to form some of directory names used in this class.
100    */
SetAppname(std::string name)101   static void SetAppname(std::string name) { appname = name; };
102 
103   /**
104    * Returns name and suffix of a file, strips leading directory information.
105    *
106    * @author Jens W. Wulf
107    */
108   static std::string name(std::string pathAndName);
109 
110   /**
111    * Moves a file.
112    * dest can be a file or a directory (ends with '/').
113    *
114    * @author Jens W. Wulf
115    */
116   static int move(std::string dest, std::string src);
117 
118 private:
119   static std::string appname;
120 };
121 
122 #endif
123