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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*
The MIT License (MIT)
Copyright (c) 2017 Charles Gunyon
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.
*/
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmp.h"
static bool read_bytes(void *data, size_t sz, FILE *fh) {
return fread(data, sizeof(uint8_t), sz, fh) == (sz * sizeof(uint8_t));
}
static bool file_reader(cmp_ctx_t *ctx, void *data, size_t limit) {
return read_bytes(data, limit, (FILE *)ctx->buf);
}
static bool file_skipper(cmp_ctx_t *ctx, size_t count) {
return fseek((FILE *)ctx->buf, count, SEEK_CUR);
}
static size_t file_writer(cmp_ctx_t *ctx, const void *data, size_t count) {
return fwrite(data, sizeof(uint8_t), count, (FILE *)ctx->buf);
}
void error_and_exit(const char *msg) {
fprintf(stderr, "%s\n\n", msg);
exit(EXIT_FAILURE);
}
int main(void) {
FILE *fh = NULL;
cmp_ctx_t cmp;
uint16_t year = 1983;
uint8_t month = 5;
uint8_t day = 28;
int64_t sint = 0;
uint64_t uint = 0;
float flt = 0.0f;
double dbl = 0.0;
bool boolean = false;
uint8_t fake_bool = 0;
uint32_t string_size = 0;
uint32_t array_size = 0;
uint32_t binary_size = 0;
uint32_t map_size = 0;
int8_t ext_type = 0;
uint32_t ext_size = 0;
char sbuf[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
fh = fopen("cmp_data.dat", "w+b");
if (fh == NULL)
error_and_exit("Error opening data.dat");
cmp_init(&cmp, fh, file_reader, file_skipper, file_writer);
/*
* When you write an array, you first specify the number of array
* elements, then you write that many elements.
*/
if (!cmp_write_array(&cmp, 9))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_sint(&cmp, -14))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_uint(&cmp, 38))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_float(&cmp, 1.8f))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_double(&cmp, 300.4))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_nil(&cmp))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_true(&cmp))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_false(&cmp))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_bool(&cmp, false))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_u8_as_bool(&cmp, 1))
error_and_exit(cmp_strerror(&cmp));
/* Array full */
/*
* Maps work similar to arrays, but the length is in "pairs", so this
* writes 2 pairs to the map. Subsequently, pairs are written in key,
* value order.
*/
if (!cmp_write_map(&cmp, 2))
error_and_exit(cmp_strerror(&cmp));
/* You can write string data all at once... */
if (!cmp_write_str(&cmp, "Greeting", 8))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_str(&cmp, "Hello", 5))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_str(&cmp, "Name", 4))
error_and_exit(cmp_strerror(&cmp));
/* ...or in chunks */
if (!cmp_write_str_marker(&cmp, 5))
error_and_exit(cmp_strerror(&cmp));
if (file_writer(&cmp, "Li", 2) != 2)
error_and_exit(strerror(errno));
if (file_writer(&cmp, "nus", 3) != 3)
error_and_exit(strerror(errno));
/* Map full */
/* Binary data functions the same as string data */
if (!cmp_write_bin(&cmp, "MessagePack", 11))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_write_bin_marker(&cmp, 8))
error_and_exit(cmp_strerror(&cmp));
if (file_writer(&cmp, "is ", 3) != 3)
error_and_exit(strerror(errno));
if (file_writer(&cmp, "great", 5) != 5)
error_and_exit(strerror(errno));
/*
* With extended types, you can create your own custom types. Here we
* create a simple date type.
*/
/* cmp_write_ext_marker(type, size) */
if (!cmp_write_ext_marker(&cmp, 1, 4))
error_and_exit(cmp_strerror(&cmp));
file_writer(&cmp, &year, sizeof(uint16_t));
file_writer(&cmp, &month, sizeof(uint8_t));
file_writer(&cmp, &day, sizeof(uint8_t));
/* Now we can read the data back just as easily */
rewind(fh);
if (!cmp_read_array(&cmp, &array_size))
error_and_exit(cmp_strerror(&cmp));
if (array_size != 9)
error_and_exit("Array size was not 9");
if (!cmp_read_sinteger(&cmp, &sint))
error_and_exit(cmp_strerror(&cmp));
if (sint != -14)
error_and_exit("Signed int was not -14");
if (!cmp_read_uinteger(&cmp, &uint))
error_and_exit(cmp_strerror(&cmp));
if (uint != 38)
error_and_exit("Unsigned int was not 38");
if (!cmp_read_float(&cmp, &flt))
error_and_exit(cmp_strerror(&cmp));
if (flt != 1.8f)
error_and_exit("Float was not 1.8f");
if (!cmp_read_double(&cmp, &dbl))
error_and_exit(cmp_strerror(&cmp));
if (dbl != 300.4)
error_and_exit("Double was not 300.f");
if (!cmp_read_nil(&cmp))
error_and_exit(cmp_strerror(&cmp));
if (!cmp_read_bool(&cmp, &boolean))
error_and_exit(cmp_strerror(&cmp));
if (boolean != true)
error_and_exit("First boolean was not true");
if (!cmp_read_bool(&cmp, &boolean))
error_and_exit(cmp_strerror(&cmp));
if (boolean != false)
error_and_exit("Second boolean was not false");
if (!cmp_read_bool(&cmp, &boolean))
error_and_exit(cmp_strerror(&cmp));
if (boolean != false)
error_and_exit("Third boolean was not false");
if (!cmp_read_bool_as_u8(&cmp, &fake_bool))
error_and_exit(cmp_strerror(&cmp));
if (fake_bool != 1) {
fprintf(stderr, "%u.\n", fake_bool);
error_and_exit("Third boolean (u8) was not 1");
}
if (!cmp_read_map(&cmp, &map_size))
error_and_exit(cmp_strerror(&cmp));
if (map_size != 2)
error_and_exit("Map size was not 2");
/*
* String reading here. Note that normally strings are encoded using
* UTF-8. I have cleverly restricted this example to ASCII, which overlaps
* UTF-8 encoding, but this must not be assumed in real-world code.
*
* You can read strings in two ways. Either you can read the string's size
* in bytes and then read the bytes manually...
*/
if (!cmp_read_str_size(&cmp, &string_size))
error_and_exit(cmp_strerror(&cmp));
if (string_size != 8)
error_and_exit("Greeting string key size was not 8");
if (!read_bytes(sbuf, 8, fh))
error_and_exit(strerror(errno));
sbuf[string_size] = 0;
if (strncmp(sbuf, "Greeting", 8) != 0)
error_and_exit("Greeting string key name was not 'Greeting'");
/*
* ...or you can set the maximum number of bytes to read and do it all in
* one call. cmp_read_str will write no more than "size" bytes, including
* the terminating NULL, to the passed buffer. If the string's size
* exceeds the passed buffer size, the "size" input is set to the number of
* bytes necessary, not including the terminating NULL. Otherwise, the
* "size" input is set to the number of bytes written, not including the
* terminating NULL.
*/
string_size = sizeof(sbuf);
if (!cmp_read_str(&cmp, sbuf, &string_size))
error_and_exit(cmp_strerror(&cmp));
if (strncmp(sbuf, "Hello", 5) != 0)
error_and_exit("Greeting string value was not 'Hello'");
string_size = sizeof(sbuf);
if (!cmp_read_str(&cmp, sbuf, &string_size))
error_and_exit(cmp_strerror(&cmp));
if (strncmp(sbuf, "Name", 4) != 0)
error_and_exit("Name key name was not 'Name'");
string_size = sizeof(sbuf);
if (!cmp_read_str(&cmp, sbuf, &string_size))
error_and_exit(cmp_strerror(&cmp));
if (strncmp(sbuf, "Linus", 5) != 0)
error_and_exit("Name key value was not 'Linus'");
memset(sbuf, 0, sizeof(sbuf));
binary_size = sizeof(sbuf);
if (!cmp_read_bin(&cmp, &sbuf, &binary_size))
error_and_exit(cmp_strerror(&cmp));
if (memcmp(sbuf, "MessagePack", 11) != 0)
error_and_exit("1st binary value was not 'MessagePack'");
memset(sbuf, 0, sizeof(sbuf));
binary_size = sizeof(sbuf);
if (!cmp_read_bin(&cmp, &sbuf, &binary_size))
error_and_exit(cmp_strerror(&cmp));
if (memcmp(sbuf, "is great", 8) != 0)
error_and_exit("2nd binary value was not 'is great'");
if (!cmp_read_ext_marker(&cmp, &ext_type, &ext_size))
error_and_exit(cmp_strerror(&cmp));
if (!read_bytes(&year, sizeof(uint16_t), fh))
error_and_exit(strerror(errno));
if (!read_bytes(&month, sizeof(uint8_t), fh))
error_and_exit(strerror(errno));
if (!read_bytes(&day, sizeof(uint8_t), fh))
error_and_exit(strerror(errno));
if (year != 1983)
error_and_exit("Year was not 1983");
if (month != 5)
error_and_exit("Month was not 5");
if (day != 28)
error_and_exit("Day was not 28");
rewind(fh);
/* Alternately, you can read objects until the stream is empty */
while (1) {
cmp_object_t obj;
if (!cmp_read_object(&cmp, &obj)) {
if (feof(fh))
break;
error_and_exit(cmp_strerror(&cmp));
}
switch (obj.type) {
case CMP_TYPE_POSITIVE_FIXNUM:
case CMP_TYPE_UINT8:
printf("Unsigned Integer: %u\n", obj.as.u8);
break;
case CMP_TYPE_FIXMAP:
case CMP_TYPE_MAP16:
case CMP_TYPE_MAP32:
printf("Map: %u\n", obj.as.map_size);
break;
case CMP_TYPE_FIXARRAY:
case CMP_TYPE_ARRAY16:
case CMP_TYPE_ARRAY32:
printf("Array: %u\n", obj.as.array_size);
break;
case CMP_TYPE_FIXSTR:
case CMP_TYPE_STR8:
case CMP_TYPE_STR16:
case CMP_TYPE_STR32:
if (!read_bytes(sbuf, obj.as.str_size, fh))
error_and_exit(strerror(errno));
sbuf[obj.as.str_size] = 0;
printf("String: %s\n", sbuf);
break;
case CMP_TYPE_BIN8:
case CMP_TYPE_BIN16:
case CMP_TYPE_BIN32:
memset(sbuf, 0, sizeof(sbuf));
if (!read_bytes(sbuf, obj.as.bin_size, fh))
error_and_exit(strerror(errno));
printf("Binary: %s\n", sbuf);
break;
case CMP_TYPE_NIL:
printf("NULL\n");
break;
case CMP_TYPE_BOOLEAN:
if (obj.as.boolean)
printf("Boolean: true\n");
else
printf("Boolean: false\n");
break;
case CMP_TYPE_EXT8:
case CMP_TYPE_EXT16:
case CMP_TYPE_EXT32:
case CMP_TYPE_FIXEXT1:
case CMP_TYPE_FIXEXT2:
case CMP_TYPE_FIXEXT4:
case CMP_TYPE_FIXEXT8:
case CMP_TYPE_FIXEXT16:
if (obj.as.ext.type == 1) { /* Date object */
if (!read_bytes(&year, sizeof(uint16_t), fh))
error_and_exit(strerror(errno));
if (!read_bytes(&month, sizeof(uint8_t), fh))
error_and_exit(strerror(errno));
if (!read_bytes(&day, sizeof(uint8_t), fh))
error_and_exit(strerror(errno));
printf("Date: %u/%u/%u\n", year, month, day);
}
else {
printf("Extended type {%d, %u}: ",
obj.as.ext.type, obj.as.ext.size
);
while (obj.as.ext.size--) {
read_bytes(sbuf, sizeof(uint8_t), fh);
printf("%02x ", sbuf[0]);
}
printf("\n");
}
break;
case CMP_TYPE_FLOAT:
printf("Float: %f\n", obj.as.flt);
break;
case CMP_TYPE_DOUBLE:
printf("Double: %f\n", obj.as.dbl);
break;
case CMP_TYPE_UINT16:
printf("Unsigned Integer: %u\n", obj.as.u16);
break;
case CMP_TYPE_UINT32:
printf("Unsigned Integer: %u\n", obj.as.u32);
break;
case CMP_TYPE_UINT64:
printf("Unsigned Integer: %" PRIu64 "\n", obj.as.u64);
break;
case CMP_TYPE_NEGATIVE_FIXNUM:
case CMP_TYPE_SINT8:
printf("Signed Integer: %d\n", obj.as.s8);
break;
case CMP_TYPE_SINT16:
printf("Signed Integer: %d\n", obj.as.s16);
break;
case CMP_TYPE_SINT32:
printf("Signed Integer: %d\n", obj.as.s32);
break;
case CMP_TYPE_SINT64:
printf("Signed Integer: %" PRId64 "\n", obj.as.s64);
break;
default:
printf("Unrecognized object type %u\n", obj.type);
break;
}
}
fclose(fh);
return EXIT_SUCCESS;
}
|