File: extracts.c

package info (click to toggle)
sip5 5.5.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,380 kB
  • sloc: ansic: 37,043; yacc: 6,670; python: 2,107; lex: 823; makefile: 18; cpp: 4
file content (109 lines) | stat: -rw-r--r-- 2,868 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
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
/*
 * The extracts generator for SIP.
 *
 * Copyright (c) 2015 Riverbank Computing Limited <info@riverbankcomputing.com>
 *
 * This file is part of SIP.
 *
 * This copy of SIP is licensed for use under the terms of the SIP License
 * Agreement.  See the file LICENSE for more details.
 *
 * This copy of SIP may also used under the terms of the GNU General Public
 * License v2 or v3 as published by the Free Software Foundation which can be
 * found in the files LICENSE-GPL2 and LICENSE-GPL3 included in this package.
 *
 * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */


#include <stdio.h>
#include <string.h>

#include "sip.h"


/*
 * Generate the extracts.
 */
void generateExtracts(sipSpec *pt, const stringList *extracts)
{
    while (extracts != NULL)
    {
        const char *cp, *id, *fname;
        size_t id_len;
        extractDef *ed;
        extractPartDef *epd;
        FILE *fp;

        /* Extract the id and file name. */
        cp = strchr(extracts->s, ':');

        if (cp == NULL || cp == extracts->s || cp[1] == '\0')
            fatal("An extract must be in the form 'id:file', not '%s'\n",
                    extracts->s);

        id = extracts->s;
        id_len = cp - extracts->s;
        fname = &cp[1];

        for (ed = pt->extracts; ed != NULL; ed = ed->next)
            if (strlen(ed->id) == id_len && strncmp(ed->id, id, id_len) == 0)
                break;

        if (ed == NULL)
            fatal("There is no extract defined with the identifier \"%.*s\"\n",
                    id_len, id);

        if ((fp = fopen(fname, "w")) == NULL)
            fatal("Unable to create file '%s'\n", fname);

        for (epd = ed->parts; epd != NULL; epd = epd->next)
            fprintf(fp, "%s", epd->part->frag);

        fclose(fp);

        extracts = extracts->next;
    }
}


/*
 * Add a new part to a (possibly new) extract.
 */
void addExtractPart(sipSpec *pt, const char *id, int order, codeBlock *part)
{
    extractDef *ed;
    extractPartDef *epd, **at;

    /* Find the extract if it exists. */
    for (ed = pt->extracts; ed != NULL; ed = ed->next)
        if (strcmp(ed->id, id) == 0)
            break;

    /* Create the extract if it doesn't already exist. */
    if (ed == NULL)
    {
        ed = sipMalloc(sizeof (extractDef));

        ed->id = id;
        ed->parts = NULL;
        ed->next = pt->extracts;

        pt->extracts = ed;
    }

    /* Find the position where the new part should be inserted. */
    for (at = &ed->parts; *at != NULL; at = &(*at)->next)
        if (order >= 0 && ((*at)->order < 0 || order < (*at)->order))
            break;

    /* Create the new part. */
    epd = sipMalloc(sizeof (extractPartDef));

    epd->order = order;
    epd->part = part;
    epd->next = *at;

    *at = epd;
}