author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 07 Oct 2019 14:18:31 +0200 | |
changeset 15447 | 6031c0cfec89 |
parent 14440 | a1613788130d |
permissions | -rw-r--r-- |
14440
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
1 |
use std::ffi::CString; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
2 |
use std::io::{ Read, Write, Seek, SeekFrom, Result }; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
3 |
use std::mem; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
4 |
use libc::{ c_int, c_char, c_void }; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
5 |
use primitives::*; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
6 |
use super::{ PhysFSContext }; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
7 |
use super::util::physfs_error_as_io_error; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
8 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
9 |
#[link(name = "physfs")] |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
10 |
extern { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
11 |
// valid filehandle on success, NULL on failure |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
12 |
fn PHYSFS_openAppend(filename: *const c_char) -> *const RawFile; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
13 |
fn PHYSFS_openRead(filename: *const c_char) -> *const RawFile; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
14 |
fn PHYSFS_openWrite(filename: *const c_char) -> *const RawFile; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
15 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
16 |
// nonzero on success, 0 on failure (and the handle stays open) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
17 |
// The docs make it sound like failure is rare. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
18 |
fn PHYSFS_close(file: *const RawFile) -> c_int; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
19 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
20 |
// Number of bytes read on success, -1 on failure. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
21 |
fn PHYSFS_read(file: *const RawFile, buffer: *mut c_void, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
22 |
obj_size: PHYSFS_uint32, obj_count: PHYSFS_uint32) -> PHYSFS_sint64; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
23 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
24 |
// Number of bytes written on success, -1 on failure. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
25 |
fn PHYSFS_write(file: *const RawFile, buffer: *const c_void, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
26 |
obj_size: PHYSFS_uint32, obj_count: PHYSFS_uint32) -> PHYSFS_sint64; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
27 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
28 |
// Flush buffered file; no-op for unbuffered files. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
29 |
fn PHYSFS_flush(file: *const RawFile) -> c_int; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
30 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
31 |
// Seek to position in file; nonzero on succss, zero on error. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
32 |
fn PHYSFS_seek(file: *const RawFile, pos: PHYSFS_uint64) -> c_int; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
33 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
34 |
// Current position in file, -1 on failure. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
35 |
fn PHYSFS_tell(file: *const RawFile) -> PHYSFS_sint64; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
36 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
37 |
// nonzero if EOF, zero if not. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
38 |
fn PHYSFS_eof(file: *const RawFile) -> c_int; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
39 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
40 |
// Determine file size; returns -1 if impossible |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
41 |
fn PHYSFS_fileLength(file: *const RawFile) -> PHYSFS_sint64; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
42 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
43 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
44 |
/// Possible ways to open a file. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
45 |
#[derive(Copy, Clone)] |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
46 |
pub enum Mode { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
47 |
/// Append to the end of the file. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
48 |
Append, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
49 |
/// Read from the file. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
50 |
Read, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
51 |
/// Write to the file, overwriting previous data. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
52 |
Write, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
53 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
54 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
55 |
/// A wrapper for the PHYSFS_File type. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
56 |
#[repr(C)] |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
57 |
struct RawFile { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
58 |
opaque: *const c_void, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
59 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
60 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
61 |
/// A file handle. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
62 |
#[allow(dead_code)] |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
63 |
pub struct File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
64 |
raw: *const RawFile, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
65 |
mode: Mode, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
66 |
context: &'f PhysFSContext, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
67 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
68 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
69 |
impl<'f> File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
70 |
/// Opens a file with a specific mode. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
71 |
pub fn open<'g>(context: &'g PhysFSContext, filename: String, mode: Mode) -> Result<File<'g>> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
72 |
let c_filename = try!(CString::new(filename)); |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
73 |
let raw = unsafe { match mode { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
74 |
Mode::Append => PHYSFS_openAppend(c_filename.as_ptr()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
75 |
Mode::Read => PHYSFS_openRead(c_filename.as_ptr()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
76 |
Mode::Write => PHYSFS_openWrite(c_filename.as_ptr()) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
77 |
}}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
78 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
79 |
if raw.is_null() { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
80 |
Err(physfs_error_as_io_error()) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
81 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
82 |
else { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
83 |
Ok(File{raw: raw, mode: mode, context: context}) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
84 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
85 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
86 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
87 |
/// Closes a file handle. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
88 |
fn close(&self) -> Result<()> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
89 |
match unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
90 |
PHYSFS_close(self.raw) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
91 |
} { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
92 |
0 => Err(physfs_error_as_io_error()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
93 |
_ => Ok(()) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
94 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
95 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
96 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
97 |
/// Checks whether eof is reached or not. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
98 |
pub fn eof(&self) -> bool { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
99 |
let ret = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
100 |
PHYSFS_eof(self.raw) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
101 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
102 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
103 |
ret != 0 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
104 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
105 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
106 |
/// Determine length of file, if possible |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
107 |
pub fn len(&self) -> Result<u64> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
108 |
let len = unsafe { PHYSFS_fileLength(self.raw) }; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
109 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
110 |
if len >= 0 { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
111 |
Ok(len as u64) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
112 |
} else { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
113 |
Err(physfs_error_as_io_error()) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
114 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
115 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
116 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
117 |
/// Determines current position within a file |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
118 |
pub fn tell(&self) -> Result<u64> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
119 |
let ret = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
120 |
PHYSFS_tell(self.raw) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
121 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
122 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
123 |
match ret { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
124 |
-1 => Err(physfs_error_as_io_error()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
125 |
_ => Ok(ret as u64) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
126 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
127 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
128 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
129 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
130 |
impl<'f> Read for File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
131 |
/// Reads from a file |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
132 |
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
133 |
let ret = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
134 |
PHYSFS_read( |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
135 |
self.raw, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
136 |
buf.as_ptr() as *mut c_void, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
137 |
mem::size_of::<u8>() as PHYSFS_uint32, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
138 |
buf.len() as PHYSFS_uint32 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
139 |
) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
140 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
141 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
142 |
match ret { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
143 |
-1 => Err(physfs_error_as_io_error()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
144 |
_ => Ok(ret as usize) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
145 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
146 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
147 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
148 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
149 |
impl<'f> Write for File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
150 |
/// Writes to a file. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
151 |
/// This code performs no safety checks to ensure |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
152 |
/// that the buffer is the correct length. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
153 |
fn write(&mut self, buf: &[u8]) -> Result<usize> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
154 |
let ret = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
155 |
PHYSFS_write( |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
156 |
self.raw, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
157 |
buf.as_ptr() as *const c_void, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
158 |
mem::size_of::<u8>() as PHYSFS_uint32, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
159 |
buf.len() as PHYSFS_uint32 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
160 |
) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
161 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
162 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
163 |
match ret { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
164 |
-1 => Err(physfs_error_as_io_error()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
165 |
_ => Ok(ret as usize) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
166 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
167 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
168 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
169 |
/// Flushes a file if buffered; no-op if unbuffered. |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
170 |
fn flush(&mut self) -> Result<()> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
171 |
let ret = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
172 |
PHYSFS_flush(self.raw) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
173 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
174 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
175 |
match ret { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
176 |
0 => Err(physfs_error_as_io_error()), |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
177 |
_ => Ok(()) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
178 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
179 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
180 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
181 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
182 |
impl<'f> Seek for File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
183 |
/// Seek to a new position within a file |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
184 |
fn seek(&mut self, pos: SeekFrom) -> Result<u64> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
185 |
let seek_pos = match pos { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
186 |
SeekFrom::Start(n) => n as i64, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
187 |
SeekFrom::End(n) => { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
188 |
let len = try!(self.len()); |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
189 |
n + len as i64 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
190 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
191 |
SeekFrom::Current(n) => { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
192 |
let curr_pos = try!(self.tell()); |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
193 |
n + curr_pos as i64 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
194 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
195 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
196 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
197 |
let result = unsafe { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
198 |
PHYSFS_seek( |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
199 |
self.raw, |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
200 |
seek_pos as PHYSFS_uint64 |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
201 |
) |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
202 |
}; |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
203 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
204 |
if result == -1 { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
205 |
return Err(physfs_error_as_io_error()); |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
206 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
207 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
208 |
self.tell() |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
209 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
210 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
211 |
|
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
212 |
impl<'f> Drop for File<'f> { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
213 |
fn drop(&mut self) { |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
214 |
let _ = self.close(); |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
215 |
} |
a1613788130d
Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff
changeset
|
216 |
} |