Rev 154 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 154 | Rev 186 | ||
---|---|---|---|
Line 40... | Line 40... | ||
40 | // Returns 1 on success, 0 on error. |
40 | // Returns 1 on success, 0 on error. |
41 | 41 | ||
42 | char *new_data; |
42 | char *new_data; |
43 | 43 | ||
44 | // first, reallocate space to hold data_size bytes more |
44 | // first, reallocate space to hold data_size bytes more |
45 | new_data = (char *) realloc (buffer->data, buffer->size + data_size); |
45 | new_data = (char *) realloc (buffer->data, buffer->size + data_size + 2); |
46 | if (new_data == NULL) |
46 | if (new_data == NULL) |
47 | return (0); // on failure, return an error value |
47 | return (0); // on failure, return an error value |
48 | 48 | ||
49 | buffer->data = new_data; // save pointer to reallocated data |
49 | buffer->data = new_data; // save pointer to reallocated data |
50 | memcpy (&buffer->data[buffer->size], data, data_size); // write new data at the end |
50 | memcpy (&buffer->data[buffer->size], data, data_size); // write new data at the end |
- | 51 | buffer->data[buffer->size + data_size + 0] = 0; // always null-terminate buffers, silently |
|
- | 52 | buffer->data[buffer->size + data_size + 1] = 0; // always null-terminate buffers, silently |
|
51 | buffer->size += data_size; // and increment buffer size |
53 | buffer->size += data_size; // and increment buffer size |
52 | 54 | ||
53 | return (1); // buffer appended successfully, return SUCCESS |
55 | return (1); // buffer appended successfully, return SUCCESS |
54 | } |
56 | } |
55 | 57 | ||
Line 60... | Line 62... | ||
60 | // Returns 1 on success, 0 on error. |
62 | // Returns 1 on success, 0 on error. |
61 | 63 | ||
62 | char *new_data; |
64 | char *new_data; |
63 | 65 | ||
64 | // first, reallocate space to hold data_size bytes more |
66 | // first, reallocate space to hold data_size bytes more |
65 | new_data = (char *) realloc (buffer->data, data_size + buffer->size); |
67 | new_data = (char *) realloc (buffer->data, data_size + buffer->size + 2); |
66 | if (new_data == NULL) |
68 | if (new_data == NULL) |
67 | return (0); // on failure, return an error value |
69 | return (0); // on failure, return an error value |
68 | 70 | ||
69 | buffer->data = new_data; // save pointer to reallocated data |
71 | buffer->data = new_data; // save pointer to reallocated data |
70 | if (buffer->size > 0) |
72 | if (buffer->size > 0) |
71 | memmove (&buffer->data[data_size], buffer->data, buffer->size); // move existing data to the end |
73 | memmove (&buffer->data[data_size], buffer->data, buffer->size); // move existing data to the end |
72 | memcpy (buffer->data, data, data_size); // write new data at the beginning |
74 | memcpy (buffer->data, data, data_size); // write new data at the beginning |
- | 75 | buffer->data[buffer->size + data_size + 0] = 0; // always null-terminate buffers, silently |
|
- | 76 | buffer->data[buffer->size + data_size + 1] = 0; // always null-terminate buffers, silently |
|
73 | buffer->size += data_size; // and increment buffer size |
77 | buffer->size += data_size; // and increment buffer size |
74 | 78 | ||
75 | return (1); // buffer appended successfully, return SUCCESS |
79 | return (1); // buffer appended successfully, return SUCCESS |
76 | } |
80 | } |
77 | 81 | ||
Line 86... | Line 90... | ||
86 | 90 | ||
87 | // see if we need to grow the data |
91 | // see if we need to grow the data |
88 | if (write_index + data_size > buffer->size) |
92 | if (write_index + data_size > buffer->size) |
89 | { |
93 | { |
90 | // if so, reallocate data buffer at the right size |
94 | // if so, reallocate data buffer at the right size |
91 | new_data = (char *) realloc (buffer->data, write_index + data_size); |
95 | new_data = (char *) realloc (buffer->data, write_index + data_size + 2); |
92 | if (new_data == NULL) |
96 | if (new_data == NULL) |
93 | return (0); // on failure, return an error value |
97 | return (0); // on failure, return an error value |
94 | 98 | ||
95 | buffer->data = new_data; // save pointer to reallocated data |
99 | buffer->data = new_data; // save pointer to reallocated data |
96 | } |
100 | } |
Line 100... | Line 104... | ||
100 | if (((data <= dest) && (data + data_size > dest)) || ((dest <= data) && (dest + data_size > data))) |
104 | if (((data <= dest) && (data + data_size > dest)) || ((dest <= data) && (dest + data_size > data))) |
101 | memmove (&buffer->data[write_index], data, data_size); // if so, move data in place |
105 | memmove (&buffer->data[write_index], data, data_size); // if so, move data in place |
102 | else |
106 | else |
103 | memcpy (&buffer->data[write_index], data, data_size); // else write data in place (faster) |
107 | memcpy (&buffer->data[write_index], data, data_size); // else write data in place (faster) |
104 | if (write_index + data_size > buffer->size) |
108 | if (write_index + data_size > buffer->size) |
- | 109 | { |
|
- | 110 | buffer->data[write_index + data_size + 0] = 0; // always null-terminate buffers, silently |
|
- | 111 | buffer->data[write_index + data_size + 1] = 0; // always null-terminate buffers, silently |
|
105 | buffer->size = write_index + data_size; // update data size only if it growed |
112 | buffer->size = write_index + data_size; // update data size only if it growed |
- | 113 | } |
|
106 | 114 | ||
107 | return (1); // buffer written successfully, return SUCCESS |
115 | return (1); // buffer written successfully, return SUCCESS |
108 | } |
116 | } |
109 | 117 | ||
110 | 118 | ||
111 | int Buffer_ReadFromFile (buffer_t *buffer, const char *file_pathname) |
119 | int Buffer_ReadFromFile (buffer_t *buffer, const char *file_pathname) |
112 | { |
120 | { |
113 | // this function copies the contents of file_pathname in a newly allocated data buffer |
121 | // this function copies the contents of file_pathname in a newly allocated data buffer |
114 | // and fills in the buffer size accordingly. It is up to the caller to free that buffer. |
122 | // and fills in the buffer size accordingly. It is up to the caller to free that buffer. |
115 | // Returns 1 on success, 0 on error. |
123 | // Returns 1 on success, 0 on error. |
116 | 124 | ||
117 | struct stat stat_buffer; |
125 | struct stat stat_buffer; |
118 | FILE *fp; |
126 | FILE *fp; |
119 | 127 | ||
120 | // start by reporting a zero length |
128 | // start by reporting a zero length |
121 | buffer->size = 0; |
129 | buffer->size = 0; |
122 | 130 | ||
123 | // figure out file existence and size |
131 | // figure out file existence and size |
124 | if (stat (file_pathname, &stat_buffer) != 0) |
132 | if (stat (file_pathname, &stat_buffer) != 0) |
125 | return (0); // on failure, return an error value |
133 | return (0); // on failure, return an error value |
126 | 134 | ||
127 | // allocate enough space for it |
135 | // allocate enough space for it |
128 | if ((buffer->data = (char *) malloc (stat_buffer.st_size)) == NULL) |
136 | if ((buffer->data = (char *) malloc (stat_buffer.st_size + 2)) == NULL) |
129 | return (0); // on failure, return an error value |
137 | return (0); // on failure, return an error value |
130 | 138 | ||
131 | // open it for binary reading |
139 | // open it for binary reading |
132 | fopen_s (&fp, file_pathname, "rb"); |
140 | fopen_s (&fp, file_pathname, "rb"); |
133 | if (fp == NULL) |
141 | if (fp == NULL) |
134 | { |
142 | { |
135 | free (buffer->data); |
143 | free (buffer->data); |
136 | return (0); // on failure, return an error value |
144 | return (0); // on failure, return an error value |
137 | } |
145 | } |
138 | 146 | ||
139 | // and read it at once |
147 | // and read it at once |
140 | if (fread (buffer->data, 1, stat_buffer.st_size, fp) != (size_t) stat_buffer.st_size) |
148 | if (fread (buffer->data, 1, stat_buffer.st_size, fp) != (size_t) stat_buffer.st_size) |
141 | { |
149 | { |
142 | free (buffer->data); |
150 | free (buffer->data); |
143 | return (0); // on failure, return an error value |
151 | return (0); // on failure, return an error value |
144 | } |
152 | } |
145 | 153 | ||
146 | fclose (fp); // finished, close file |
154 | fclose (fp); // finished, close file |
- | 155 | buffer->data[stat_buffer.st_size + 0] = 0; // always null-terminate buffers, silently |
|
- | 156 | buffer->data[stat_buffer.st_size + 1] = 0; // always null-terminate buffers, silently |
|
147 | buffer->size = stat_buffer.st_size; // report its size |
157 | buffer->size = stat_buffer.st_size; // report its size |
148 | 158 | ||
149 | return (1); // and return SUCCESS |
159 | return (1); // and return SUCCESS |
150 | } |
160 | } |
151 | 161 | ||
152 | 162 | ||
Line 165... | Line 175... | ||
165 | // figure out file existence and size |
175 | // figure out file existence and size |
166 | if (_wstat (file_pathname, &stat_buffer) != 0) |
176 | if (_wstat (file_pathname, &stat_buffer) != 0) |
167 | return (0); // on failure, return an error value |
177 | return (0); // on failure, return an error value |
168 | 178 | ||
169 | // allocate enough space for it |
179 | // allocate enough space for it |
170 | if ((buffer->data = (char *) malloc (stat_buffer.st_size)) == NULL) |
180 | if ((buffer->data = (char *) malloc (stat_buffer.st_size + 2)) == NULL) |
171 | return (0); // on failure, return an error value |
181 | return (0); // on failure, return an error value |
172 | 182 | ||
173 |
|
183 | // open it for binary reading |
174 | _wfopen_s (&fp, file_pathname, L"rb"); |
184 | _wfopen_s (&fp, file_pathname, L"rb"); |
175 | if (fp == NULL) |
185 | if (fp == NULL) |
176 | { |
186 | { |
177 | free (buffer->data); |
187 | free (buffer->data); |
178 | return (0); // on failure, return an error value |
188 | return (0); // on failure, return an error value |
Line 184... | Line 194... | ||
184 | free (buffer->data); |
194 | free (buffer->data); |
185 | return (0); // on failure, return an error value |
195 | return (0); // on failure, return an error value |
186 | } |
196 | } |
187 | 197 | ||
188 | fclose (fp); // finished, close file |
198 | fclose (fp); // finished, close file |
- | 199 | buffer->data[stat_buffer.st_size + 0] = 0; // always null-terminate buffers, silently |
|
- | 200 | buffer->data[stat_buffer.st_size + 1] = 0; // always null-terminate buffers, silently |
|
189 | buffer->size = stat_buffer.st_size; // report its size |
201 | buffer->size = stat_buffer.st_size; // report its size |
190 | 202 | ||
191 | return (1); // and return SUCCESS |
203 | return (1); // and return SUCCESS |
192 | } |
204 | } |
193 | 205 |