Upload 2.0.2
[physicsfs] / extras / PhysFS.NET / PhysFS.cs
1 /* PhysFS.cs - (c)2003 Gregory S. Read\r
2  * Provides access to PhysFS API calls not specific to file handle access.\r
3  */\r
4 using System;\r
5 \r
6 namespace PhysFS_NET\r
7 {\r
8    public class PhysFS\r
9    {\r
10       /* Initialize\r
11        * Inits the PhysFS API.  This normally does not need to be called unless\r
12        * the API has been manually deinitialized since the PhysFS_DLL class\r
13        * initializes just before the first call is made into the DLL.\r
14        * Parameters\r
15        *    none\r
16        * Returns\r
17        *    none\r
18        * Exceptions\r
19        *    PhysFSException - An error occured in the PhysFS API\r
20        */\r
21       public static void Initialize()\r
22       {\r
23          // Initialize the physfs library, raise an exception if error\r
24          if(PhysFS_DLL.PHYSFS_init("") == 0)\r
25             throw new PhysFSException();\r
26       }\r
27 \r
28       /* Deinitialize\r
29        * Deinits the PhysFS API.  It is recommended that this method be called\r
30        * by the application before exiting in order to gracefully deallocate\r
31        * resources and close all filehandles, etc.\r
32        * Parameters\r
33        *    none\r
34        * Returns\r
35        *    none\r
36        * Exceptions\r
37        *    PhysFSException - An error occured in the PhysFS API\r
38        */\r
39       public static void Deinitialize()\r
40       {\r
41          // Deinit, raise an exception if an error occured\r
42          if(PhysFS_DLL.PHYSFS_deinit() == 0)\r
43             throw new PhysFSException();\r
44       }\r
45 \r
46       /* BaseDir\r
47        * Gets the base directory configured for PhysFS.  See the PhysFS API\r
48        * documentation for more information.\r
49        * Parameters\r
50        *    none\r
51        * Returns\r
52        *    A string value representing the Base Directory\r
53        * Exceptions\r
54        *    none\r
55        */\r
56       public static string BaseDir\r
57       {\r
58          get\r
59          {\r
60             // Return the current base directory\r
61             return PhysFS_DLL.PHYSFS_getBaseDir();\r
62          }\r
63       }\r
64 \r
65       /* WriteDir\r
66        * Gets or sets the write directory configured for PhysFS.  See the PhysFS API\r
67        * documentation for more information.\r
68        * Parameters\r
69        *    set - Path to set the WriteDir property to\r
70        * Returns\r
71        *    A string value representing the Write Directory\r
72        * Exceptions\r
73        *    PhysFSException - An error occured in the PhysFS API when\r
74        *       settings the write directory.\r
75        */\r
76       public static string WriteDir\r
77       {\r
78          get\r
79          {\r
80             // Return the current write directory\r
81             return PhysFS_DLL.PHYSFS_getWriteDir();\r
82          }\r
83          set\r
84          {\r
85             // Set the write directory and raise an exception if an error occured\r
86             if(PhysFS_DLL.PHYSFS_setWriteDir(value) == 0)\r
87                throw new PhysFSException();\r
88          }\r
89       }\r
90 \r
91       /* UserDir\r
92        * Gets or sets the write directory configured for PhysFS.  See the PhysFS API\r
93        * documentation for more information.\r
94        * Parameters\r
95        *    set - Path to set the WriteDir property to\r
96        * Returns\r
97        *    A string value representing the Write Directory\r
98        * Exceptions\r
99        *    PhysFSException - An error occured in the PhysFS API when\r
100        *       settings the write directory.\r
101        */\r
102       public static string UserDir\r
103       {\r
104          get\r
105          {\r
106             // Return the current user directory\r
107             return PhysFS_DLL.PHYSFS_getUserDir();\r
108          }\r
109       }\r
110       public static void AddToSearchPath(string NewDir, bool Append)\r
111       {\r
112          if(PhysFS_DLL.PHYSFS_addToSearchPath(NewDir, Append?1:0) == 0)\r
113             throw new PhysFSException();\r
114       }\r
115       public static void RemoveFromSearchPath(string OldDir)\r
116       {\r
117          if(PhysFS_DLL.PHYSFS_removeFromSearchPath(OldDir) == 0)\r
118             throw new PhysFSException();\r
119       }\r
120       public unsafe static string[] GetSearchPath()\r
121       {\r
122          byte** p;                              // Searchpath list from PhysFS dll\r
123          string[] pathlist;     // List converted to an array\r
124 \r
125          // Get the CDROM drive listing\r
126          p = PhysFS_DLL.PHYSFS_getSearchPath();\r
127          // Convert the C-style array to a .NET style array\r
128          pathlist = PhysFS_DLL.BytePPToArray(p);\r
129          // Free the original list since we're done with it\r
130          PhysFS_DLL.PHYSFS_freeList(p);\r
131 \r
132          return pathlist;\r
133       }\r
134       public unsafe static string[] GetCDROMDrives()\r
135       {\r
136          byte** p;                              // CDROM list from PhysFS dll\r
137          string[] cdromlist;    // List converted to an array\r
138 \r
139          // Get the CDROM drive listing\r
140          p = PhysFS_DLL.PHYSFS_getCdRomDirs();\r
141          // Convert the C-style array to a .NET style array\r
142          cdromlist = PhysFS_DLL.BytePPToArray(p);\r
143          // Free the original list since we're done with it\r
144          PhysFS_DLL.PHYSFS_freeList(p);\r
145 \r
146          return cdromlist;\r
147       }\r
148       public static void MkDir(string Dirname)\r
149       {\r
150          if(PhysFS_DLL.PHYSFS_mkdir(Dirname) == 0)\r
151             throw new PhysFSException();\r
152       }\r
153       public static void Delete(string Filename)\r
154       {\r
155          if(PhysFS_DLL.PHYSFS_delete(Filename) == 0)\r
156             throw new PhysFSException();\r
157       }\r
158       public static string GetRealDir(string Filename)\r
159       {\r
160          string RetValue;\r
161 \r
162          RetValue = PhysFS_DLL.PHYSFS_getRealDir(Filename);\r
163          if(RetValue == null)\r
164             throw new PhysFSException("File not found in search path.");\r
165 \r
166          // Return the real file path of the specified filename\r
167          return RetValue;\r
168       }\r
169       public unsafe static string[] EnumerateFiles(string Dirname)\r
170       {\r
171          byte** p;                              // File list from PhysFS dll\r
172          string[] filelist;     // List converted to an array\r
173 \r
174          // Get the CDROM drive listing\r
175          p = PhysFS_DLL.PHYSFS_enumerateFiles(Dirname);\r
176          // Convert the C-style array to a .NET style array\r
177          filelist = PhysFS_DLL.BytePPToArray(p);\r
178          // Free the original list since we're done with it\r
179          PhysFS_DLL.PHYSFS_freeList(p);\r
180 \r
181          return filelist;\r
182       }\r
183       public static bool IsDirectory(string Filename)\r
184       {\r
185          // Return true if non-zero, otherwise return false\r
186          return (PhysFS_DLL.PHYSFS_isDirectory(Filename) == 0)?false:true;\r
187       }\r
188    }\r
189 }\r