File: unittest_alloc.c

package info (click to toggle)
libinih 53-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 424 kB
  • sloc: ansic: 625; cpp: 139; makefile: 68; sh: 53
file content (49 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (5)
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
/* inih -- unit tests for custom memory allocator */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ini.h"

void* ini_malloc(size_t size) {
    printf("ini_malloc(%d)\n", (int)size);
    return malloc(size);
}

void ini_free(void* ptr) {
    printf("ini_free()\n");
    free(ptr);
}

void* ini_realloc(void* ptr, size_t size) {
    printf("ini_realloc(%d)\n", (int)size);
    return realloc(ptr, size);
}

char Prev_section[50];

int dumper(void* user, const char* section, const char* name,
           const char* value)
{
    if (strcmp(section, Prev_section)) {
        printf("... [%s]\n", section);
        strncpy(Prev_section, section, sizeof(Prev_section));
        Prev_section[sizeof(Prev_section) - 1] = '\0';
    }
    printf("... %s=%s;\n", name, value);
    return 1;
}

void parse(const char* name, const char* string) {
    int e;

    *Prev_section = '\0';
    e = ini_parse_string(string, dumper, NULL);
    printf("%s: e=%d\n", name, e);
}

int main(void)
{
    parse("basic", "[section]\nfoo = bar\nbazz = buzz quxx");
    return 0;
}