File: blob.c

package info (click to toggle)
cook 2.33-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 8,640 kB
  • ctags: 4,140
  • sloc: ansic: 47,848; sh: 14,477; makefile: 4,681; yacc: 3,170; perl: 224; awk: 219
file content (214 lines) | stat: -rw-r--r-- 4,682 bytes parent folder | download
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
/*
 *      cook - file construction tool
 *      Copyright (C) 1994, 1997, 1998, 2006-2008 Peter Miller
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program. If not, see
 *      <http://www.gnu.org/licenses/>.
 */

#include <make2cook/blob.h>
#include <make2cook/emit.h>
#include <common/error_intl.h>
#include <common/mem.h>
#include <common/trace.h>

static blob_efunc notify;


blob_ty *
blob_alloc(string_ty *text, string_ty *file_name, long line_number)
{
    blob_ty         *this;

    this = mem_alloc(sizeof(blob_ty));
    this->reference_count = 1;
    this->text = text;
    this->file_name = str_copy(file_name);
    this->line_number = line_number;
    return this;
}


void
blob_free(blob_ty *this)
{
    this->reference_count--;
    if (this->reference_count > 0)
        return;
    assert(this->reference_count == 0);
    str_free(this->text);
    str_free(this->file_name);
    mem_free(this);
}


blob_ty *
blob_copy(blob_ty *this)
{
    this->reference_count++;
    return this;
}


void
blob_error_notify(blob_efunc func)
{
    notify = func;
}


void
blob_error(blob_ty *bp, sub_context_ty *scp, char *fmt)
{
    string_ty       *buffer;
    int             need_to_delete;

    if (scp)
        need_to_delete = 0;
    else
    {
        scp = sub_context_new();
        need_to_delete = 1;
    }

    buffer = subst_intl(scp, fmt);

    /* re-use substitution context */
    sub_var_set_string(scp, "File_Name", bp->file_name);
    sub_var_set_long(scp, "Number", bp->line_number);
    sub_var_set_string(scp, "MeSsaGe", buffer);
    error_intl(scp, i18n("$filename: $number: $message"));
    str_free(buffer);
    if (notify)
        notify();

    if (need_to_delete)
        sub_context_delete(scp);
}


void
blob_warning(blob_ty *bp, sub_context_ty *scp, char *fmt)
{
    string_ty       *buffer;
    int             need_to_delete;

    if (scp)
        need_to_delete = 0;
    else
    {
        scp = sub_context_new();
        need_to_delete = 1;
    }

    buffer = subst_intl(scp, fmt);

    /* re-use substitution context */
    sub_var_set_string(scp, "File_Name", bp->file_name);
    sub_var_set_long(scp, "Number", bp->line_number);
    sub_var_set_string(scp, "MeSsaGe", buffer);
    error_intl(scp, i18n("$filename: $number: warning: $message"));
    str_free(buffer);

    if (need_to_delete)
        sub_context_delete(scp);
}


blob_list_ty *
blob_list_alloc(void)
{
    blob_list_ty    *lllp;

    lllp = mem_alloc(sizeof(blob_list_ty));
    lllp->length = 0;
    lllp->maximum = 0;
    lllp->list = 0;
    return lllp;
}


void
blob_list_free(blob_list_ty *lllp)
{
    size_t          j;

    for (j = 0; j < lllp->length; ++j)
        blob_free(lllp->list[j]);
    if (lllp->list)
        mem_free(lllp->list);
    mem_free(lllp);
}


void
blob_list_append(blob_list_ty *lllp, blob_ty *llp)
{
    if (lllp->length >= lllp->maximum)
    {
        size_t          nbytes;

        lllp->maximum = lllp->maximum * 2 + 8;
        nbytes = lllp->maximum * sizeof(blob_ty *);
        lllp->list = mem_change_size(lllp->list, nbytes);
    }
    lllp->list[lllp->length++] = llp;
}


void
blob_list_prepend(blob_list_ty *lllp, blob_ty *llp)
{
    size_t          j;

    if (lllp->length >= lllp->maximum)
    {
        size_t          nbytes;

        lllp->maximum = lllp->maximum * 2 + 8;
        nbytes = lllp->maximum * sizeof(blob_ty *);
        lllp->list = mem_change_size(lllp->list, nbytes);
    }
    for (j = lllp->length; j > 0; --j)
        lllp->list[j] = lllp->list[j - 1];
    lllp->length++;
    lllp->list[0] = llp;
}


void
blob_emit(blob_ty *bp)
{
    trace(("emit\n"));
    emit_line_number(bp->line_number, bp->file_name);
    emit_string(bp->text);
}


void
blob_list_delete(blob_list_ty *blp, blob_ty *bp)
{
    size_t          j;
    size_t          k;

    for (j = 0; j < blp->length; ++j)
        if (blp->list[j] == bp)
            break;
    if (j >= blp->length)
        return;
    blob_free(blp->list[j]);
    for (k = j + 1; k < blp->length; ++k)
        blp->list[k - 1] = blp->list[k];
    blp->length--;
}