summaryrefslogtreecommitdiff
blob: c035edeb9de87eb6a6fe320ceb4b30328877b4e4 (plain)
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
#include "text.h"

#include "astring.h"
#include "outf.h"

#include <assert.h>
#include <errno.h>
#include <string.h>


int extract_content_insert(
        extract_alloc_t*    alloc,
        const char*         original,
        const char*         single_name,
        const char*         mid_begin_name,
        const char*         mid_end_name,
        extract_astring_t*  contentss,
        int                 contentss_num,
        char**              o_out
        )
{
    int e = -1;
    const char* mid_begin = NULL;
    const char* mid_end = NULL;
    const char* single = NULL;
    extract_astring_t   out;
    extract_astring_init(&out);

    assert(single_name || mid_begin_name || mid_end_name);

    if (single_name) single = strstr(original, single_name);

    if (single)
    {
        outf("Have found single_name='%s', using in preference to mid_begin_name=%s mid_end_name=%s",
                single_name,
                mid_begin_name,
                mid_end_name
                );
        mid_begin = single;
        mid_end = single + strlen(single_name);
    }
    else
    {
        if (mid_begin_name) {
            mid_begin = strstr(original, mid_begin_name);
            if (!mid_begin) {
                outf("error: could not find '%s' in odt content", mid_begin_name);
                errno = ESRCH;
                goto end;
            }
            mid_begin += strlen(mid_begin_name);
        }
        if (mid_end_name) {
            mid_end = strstr(mid_begin ? mid_begin : original, mid_end_name);
            if (!mid_end) {
                outf("error: could not find '%s' in odt content", mid_end_name);
                e = -1;
                errno = ESRCH;
                goto end;
            }
        }
        if (!mid_begin) {
            mid_begin = mid_end;
        }
        if (!mid_end) {
            mid_end = mid_begin;
        }
    }

    if (extract_astring_catl(alloc, &out, original, mid_begin - original)) goto end;
    {
        int i;
        for (i=0; i<contentss_num; ++i) {
            if (extract_astring_catl(alloc, &out, contentss[i].chars, contentss[i].chars_num)) goto end;
        }
    }
    assert( mid_end);
    /* As per docs, at least one of <single_name>, <mid_begin_name> and
    <mid_end_name> is non-null, and this ensures that mid_end must not be null.
    */
    /* coverity[var_deref_model] */
    if (extract_astring_cat(alloc, &out, mid_end)) goto end;

    *o_out = out.chars;
    out.chars = NULL;
    e = 0;

    end:
    if (e) {
        extract_astring_free(alloc, &out);
        *o_out = NULL;
    }
    return e;
}