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