File: Simple.cs

package info (click to toggle)
taoframework 2.1.svn20090801-15.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,312 kB
  • sloc: cs: 113,093; makefile: 908; sh: 107; ansic: 7
file content (145 lines) | stat: -rw-r--r-- 5,449 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#region License
/*
MIT License
Copyright 2003-2006 Tao Framework Team
http://www.taoframework.com
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License

using System;
using System.IO;
using System.Text;
using Tao.PhysFs;

namespace PhysFsExamples
{
	/// <summary>
	/// PhysFS Tutorial - http://icculus.org/physfs/physfstut.txt
	/// </summary>
	/// <remarks>Here are some simple steps to getting PhysFS working in your program.
	/// <p>
	///		Originally created by some friendly people in #icculus.org in the freenode IRC network.
	///	</p>
	/// <p>
	///		C# Implementation by Rob Loach (http://www.robloach.net).
	/// </p>
	/// </remarks>
	public class Simple
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		public static void Main(string[] args)
		{
            string filePath = Path.Combine("..", "..");
            string fileDirectory = "Data";
            string fileName = "PhysFsExamples.Simple.MyZip.zip";

            /* 
			 * First thing you need to do is initialize the filesystem.  You simply call 
			 * PHYSFS_init();
			 * to do this.
			 * 
			 * From the docs:
			 * argv0 the argv[0] string passed to your program's mainline. This may be NULL 
			 * on most platforms (such as ones without a standard main() function), but you 
			 * should always try to pass something in here. Unix-like systems such as Linux 
			 * _need_ to pass argv[0] from main() in here.
			 */
			Fs.PHYSFS_init("init");

			/*
			 * After you have it initialized, then you need to set up any archives that 
			 * will be read from.  You can do this by using the 
			 * PHYSFS_AddToSearchPath(char*, int); function.  The 1 makes it added to the 
			 * end of the search path so that it is the last looked at.  You could instead 
			 * put a 0 there and have it be the first thing looked at.
			 */
            Fs.PHYSFS_addToSearchPath(Path.Combine(Path.Combine(filePath, fileDirectory), fileName), 1);
            Fs.PHYSFS_addToSearchPath(Path.Combine(fileDirectory, fileName), 1);

			/*
			 * Now that we have initialized physfs and added an archive to its search path, 
			 * we can do some file reading.  First thing you will want to do is check to 
			 * make sure that the file exists.  You can do this by calling 
			 * PHYSFS_exists(char*);
			 */
			Console.WriteLine("Exists: " + Fs.PHYSFS_exists("myfile.txt"));

			/*
			 * Then you can use PHYSFS_openRead(char*); to get a PHYSFS_file pointer to 
			 * that file.  There is also available: PHYSFS_openWrite(char*); and 
			 * PHYSFS_openAppend(char*);
			 * 
			 * NOTE: If you would like to do any writing you need to set a dir to write 
			 * too.  You can do this by using PHYSFS_setWriteDir(const char *newDir);
			 */
			IntPtr myfile = Fs.PHYSFS_openRead("myfile.txt");

			/*
			 * If you are going to be reading this file into memory, you can use 
			 * PHYSFS_fileLength(PHYSFS_file*) to find out how many bytes you need to 
			 * allocate for the file.  You can also use this to check and make sure the 
			 * file isn't larger than the max file size you want to open.
			 */
            long file_size = Fs.PHYSFS_fileLength(myfile);
            Console.WriteLine("Filesize: " + file_size);

			/*
			 * Now it is time to read the file into memory.  In this tutorial I am simply 
			 * going to read it all into a byte array using PHYSFS_read (PHYSFS_file *fp, 
			 * your_buffer, size_of_object_to_read, number_of_objects_to_read); and it 
			 * returns the number of objects read.
			 */
            byte[] array;
            Fs.PHYSFS_read(myfile, out array, 1, (uint)file_size);
			for(int i = 0; i < array.Length; i++)
			{
				Console.Write((char)array[i]);
			}

			/*
			 * When you are finished with your file you need to close it.  You can do so 
			 * with the function PHYSFS_close(PHYSFS_file*);
			 */
			Fs.PHYSFS_close(myfile);

			/*
			 * When you are finished with PHYSFS completely, you need to call the 
			 * PHYSFS_deinit(void) function.
			 * 
			 * From the docs:
			 * This closes any files opened via PhysicsFS, blanks the search/write paths, 
			 * frees memory, and invalidates all of your file handles.
			 */
			Fs.PHYSFS_deinit();

			/*
			 * Ok that is it for the tutorial.  It should give you a rough overview of how 
			 * things are structured and you can check the online docs for more information 
			 * on using the rest of the API.
			 */
			Console.ReadLine();
		}
	}
}