Last change
on this file since 604 was
49,
checked in by Sam Hocevar, 15 years ago
|
- Imported original public domain release, for future reference.
|
File size:
1.4 KB
|
Line | |
---|
1 | #ifndef MEMORY_FILE |
---|
2 | #define MEMORY_FILE |
---|
3 | |
---|
4 | #include "jmalloc.hpp" |
---|
5 | #include "system.h" |
---|
6 | |
---|
7 | class memory_file |
---|
8 | { |
---|
9 | unsigned char *data; |
---|
10 | |
---|
11 | public : |
---|
12 | long wp,rp,size; |
---|
13 | |
---|
14 | memory_file(int size) : size(size) |
---|
15 | //{{ |
---|
16 | { |
---|
17 | data=(unsigned char *)jmalloc(size,"memory file"); |
---|
18 | wp=rp=0; |
---|
19 | } |
---|
20 | //}} |
---|
21 | |
---|
22 | ~memory_file() |
---|
23 | //{{ |
---|
24 | { |
---|
25 | jfree(data); |
---|
26 | } |
---|
27 | //}} |
---|
28 | |
---|
29 | void write_short(unsigned short x) |
---|
30 | //{{{ |
---|
31 | { |
---|
32 | x=int_to_local(x); |
---|
33 | write(&x,2); |
---|
34 | } |
---|
35 | //}} |
---|
36 | |
---|
37 | void write_long(unsigned long x) |
---|
38 | //{{{ |
---|
39 | { |
---|
40 | x=long_to_local(x); |
---|
41 | write(&x,4); |
---|
42 | } |
---|
43 | //}}} |
---|
44 | |
---|
45 | unsigned short read_short() |
---|
46 | //{{{ |
---|
47 | { |
---|
48 | unsigned short x; |
---|
49 | read(&x,2); |
---|
50 | return int_to_local(x); |
---|
51 | } |
---|
52 | //}}} |
---|
53 | |
---|
54 | unsigned long read_long() |
---|
55 | //{{{ |
---|
56 | { |
---|
57 | unsigned long x; |
---|
58 | read(&x,4); |
---|
59 | return long_to_local(x); |
---|
60 | } |
---|
61 | //}} |
---|
62 | |
---|
63 | long read(void *buf, int rsize) |
---|
64 | //{{{ |
---|
65 | { |
---|
66 | if (rp+rsize>wp) |
---|
67 | rsize=wp-rp; |
---|
68 | memcpy(buf,data+rp,rsize); |
---|
69 | rp+=rsize; |
---|
70 | return rsize; |
---|
71 | } |
---|
72 | //}} |
---|
73 | |
---|
74 | long write(void *buf, int wsize) |
---|
75 | //{{ |
---|
76 | { |
---|
77 | if (wp+wsize>size) |
---|
78 | wsize=size-wp; |
---|
79 | memcpy(data+wp,buf,wsize); |
---|
80 | wp+=wsize; |
---|
81 | return wsize; |
---|
82 | } |
---|
83 | //}} |
---|
84 | |
---|
85 | void write_byte(unsigned char c) |
---|
86 | //{{{ |
---|
87 | { |
---|
88 | if (wp+1<=size) |
---|
89 | data[wp++]=c; |
---|
90 | } |
---|
91 | //}} |
---|
92 | |
---|
93 | unsigned char read_byte() |
---|
94 | //{{ |
---|
95 | { |
---|
96 | if (rp+1<=wp) |
---|
97 | return data[rp++]; |
---|
98 | return 0; |
---|
99 | } |
---|
100 | //}} |
---|
101 | |
---|
102 | } ; |
---|
103 | |
---|
104 | |
---|
105 | #endif |
---|
106 | //{{{ Emacs Locals |
---|
107 | // Local Variables: |
---|
108 | // folded-file: t |
---|
109 | // End: |
---|
110 | //}}} |
---|
Note: See
TracBrowser
for help on using the repository browser.