-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmemfile.h
179 lines (142 loc) · 2.76 KB
/
nmemfile.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "nstream.h"
class nmemfile : public nstream
{
public:
nmemfile(size_t initsize=0, size_t growsize=-1)
{
pos=leng=0;
alloc=initsize;
grow=growsize;
if(alloc)
ptr=(unsigned char *)malloc(alloc);
else
ptr=NULL;
del=true;
}
nmemfile(void *p, size_t l, bool delp=false)
{
attach(p,l,delp);
}
virtual ~nmemfile()
{
if(del)
free(ptr);
}
operator unsigned char *(){return ptr;}
operator char *(){return (char *)ptr;}
unsigned char &operator[](int i)
{
return ptr[i];
}
size_t len(void)const{return leng;}
unsigned char *buf(void){return ptr;}
void setlen(size_t l){leng=l;if(leng<pos)pos=leng;}
void attach(void *p, size_t l, bool delp=false)
{
pos=0;
alloc=leng=l;
ptr=(unsigned char *)p;
grow=-1;
del=delp;
}
void *detach(void)
{
del=false;
return ptr;
}
size_t write(const void *p, size_t l) override
{
if(pos+l>alloc)
{
if(!del)
{
//Buffer full, cant write all of it
memcpy(ptr+pos,p,l=alloc-pos);
return l;
}
size_t needed=(pos+l)-alloc;
size_t nalloc=grow!=-1?grow:(alloc?alloc:256);
alloc+=needed>nalloc?needed:nalloc;
ptr=(unsigned char *)realloc(ptr,alloc);
//unsigned char *n=new unsigned char[alloc];
//memcpy(n,ptr,leng); //Move old buffer
//delete [] ptr;
//ptr=n;
}
memcpy(ptr+pos,p,l);
pos+=l;
if(pos>leng)leng=pos;
return l;
}
size_t read(void *p, size_t l) override
{
if(pos+l>leng)
l=leng-pos;
memcpy(p,ptr+pos,l);
pos+=l;
return l;
}
void *read(size_t l)
{
if(pos+l>leng)
l=leng-pos;
void *r=(void *)(ptr+pos);
pos+=l;
return r;
}
size_t sizeleft(void)const
{
return leng-pos;
}
int64_t setpos(int64_t delta, pos_type how=pos_begin)
{
int64_t newpos;
switch(how)
{
case nstream::pos_begin:
newpos=delta;
break;
case nstream::pos_current:
newpos=pos+delta;
break;
case nstream::pos_end:
newpos=leng+delta;
break;
default:
return -1;
}
if(newpos<0 || newpos > int64_t(leng))return -1;
pos=(size_t)newpos;
return newpos;
}
bool load(const char *file)
{
FILE *fp=fopen(file, "rb");
if(!fp)
return false;
if(del)free(ptr);
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);
ptr=(unsigned char *)malloc(size);
fread(ptr, 1, size, fp);
fclose(fp);
pos=0;
alloc=leng=(size_t)size;
grow=-1;
del=true;
return true;
}
bool save(FILE *fp)
{
return fwrite(ptr,1,leng,fp)==leng?true:false;
}
private:
unsigned char *ptr;
size_t leng,alloc,pos,grow;
bool del;
};