~ubuntu-branches/ubuntu/trusty/argyll/trusty-proposed

« back to all changes in this revision

Viewing changes to cgats/pars.c

  • Committer: Package Import Robot
  • Author(s): Artur Rona
  • Date: 2014-02-12 00:35:39 UTC
  • mfrom: (13.1.24 sid)
  • Revision ID: package-import@ubuntu.com-20140212003539-24tautzlitsiz61w
Tags: 1.5.1-5ubuntu1
* Merge from Debian unstable. (LP: #1275572) Remaining changes:
  - debian/control:
    + Build-depend on libtiff-dev rather than libtiff4-dev.
  - debian/control, debian/patches/06_fix_udev_rule.patch:
    + Fix udev rules to actually work; ENV{ACL_MANAGE} has
      stopped working ages ago, and with logind it's now the
      "uaccess" tag. Dropping also consolekit from Recommends.
  - debian/patches/drop-usb-db.patch:
    + Use hwdb builtin, instead of the obsolete usb-db
      in the udev rules.
* debian/patches/05_ftbfs-underlinkage.diff:
  - Dropped change, no needed anymore.
* Refresh the patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
121
121
 
122
122
/* --------------------------------------------- */
123
123
/* Memory image cgatsFile compatible class */
124
 
/* Buffer is assumed to be a fixed size, and externally allocated */
125
 
/* Writes therefore don't expand the buffer. */
 
124
/* Buffer is assumed to have been allocated by the given allocator, */
 
125
/* and will be expanded on write. */
126
126
 
127
127
/* Get the size of the file (Only valid for memory file). */
128
128
static size_t cgatsFileMem_get_size(cgatsFile *pp) {
129
 
        return pp->size;
 
129
        cgatsFileMem *p = (cgatsFileMem *)pp;
 
130
 
 
131
        return p->end - p->start;
130
132
}
131
133
 
132
134
/* Set current position to offset. Return 0 on success, nz on failure. */
184
186
        return c;
185
187
}
186
188
 
 
189
/* Expand the memory buffer file to hold up to pointer ep */
 
190
/* Don't expand if realloc fails */
 
191
static void cgatsFileMem_filemem_resize(cgatsFileMem *p, unsigned char *ep) {
 
192
        size_t na, co, ce;
 
193
        unsigned char *nstart;
 
194
        
 
195
        /* No need to realloc */
 
196
        if (ep <= p->aend) {
 
197
                return;
 
198
        }
 
199
 
 
200
        co = p->cur - p->start;         /* Current offset */
 
201
        ce = p->end - p->start;         /* Current end */
 
202
        na = ep - p->start;                     /* new allocatd size */
 
203
 
 
204
        /* Round new allocation up */
 
205
        if (na <= 1024)
 
206
                na += 1024;
 
207
        else
 
208
                na += 4096;
 
209
 
 
210
        if ((nstart = p->al->realloc(p->al, p->start, na)) != NULL) {
 
211
                p->start = nstart;
 
212
                p->cur = nstart + co;
 
213
                p->end = nstart + ce;
 
214
                p->aend = nstart + na;
 
215
        }
 
216
}
 
217
 
187
218
/* write count items of size length. Return number of items successfully written. */
188
219
static size_t cgatsFileMem_write(
189
220
cgatsFile *pp,
195
226
        size_t len;
196
227
 
197
228
        len = ssat_mul(size, count);
198
 
        if (len > (size_t)(p->end - p->cur)) { /* Too much */
 
229
        if (len > (size_t)(p->end - p->cur))  /* Try and expand buffer */
 
230
                cgatsFileMem_filemem_resize(p, p->start + len);
 
231
 
 
232
        if (len > (size_t)(p->end - p->cur)) {
199
233
                if (size > 0)
200
234
                        count = (p->end - p->cur)/size;
201
235
                else
205
239
        if (len > 0)
206
240
                memmove(p->cur, buffer, len);
207
241
        p->cur += len;
 
242
        if (p->end < p->cur)
 
243
                p->end = p->cur;
208
244
        return count;
209
245
}
210
246
 
217
253
        int rv;
218
254
        va_list args;
219
255
        cgatsFileMem *p = (cgatsFileMem *)pp;
 
256
        int len;
220
257
 
221
258
        va_start(args, format);
222
259
 
223
 
#if ((defined(__IBMC__) || defined(__BORLANDC__)) && defined(_M_IX86))
224
 
        rv = vsprintf((char *)p->cur, format, args);    /* This could overwrite the buffer !!! */
225
 
#else
226
 
        rv = vsnprintf((char *)p->cur, (p->end - p->cur), format, args);
227
 
#endif
228
 
 
 
260
        rv = 1;
 
261
        len = 100;                                      /* Initial allocation for printf */
 
262
        cgatsFileMem_filemem_resize(p, p->cur + len);
 
263
 
 
264
        /* We have to use the available printf functions to resize the buffer if needed. */
 
265
        for (;rv != 0;) {
 
266
                /* vsnprintf() either returns -1 if it doesn't fit, or */
 
267
                /* returns the size-1 needed in order to fit. */
 
268
                len = vsnprintf((char *)p->cur, (p->aend - p->cur), format, args);
 
269
 
 
270
                if (len > -1 && ((p->cur + len +1) <= p->aend)) /* Fitted in current allocation */
 
271
                        break;
 
272
 
 
273
                if (len > -1)                           /* vsnprintf returned needed size-1 */
 
274
                        len = len+2;                    /* (In case vsnprintf returned 1 less than it needs) */
 
275
                else
 
276
                        len *= 2;                               /* We just have to guess */
 
277
 
 
278
                /* Attempt to resize */
 
279
                cgatsFileMem_filemem_resize(p, p->cur + len);
 
280
 
 
281
                /* If resize failed */
 
282
                if ((p->aend - p->cur) < len) {
 
283
                        rv = 0;
 
284
                        break;                  
 
285
                }
 
286
        }
 
287
        if (rv != 0) {
 
288
                /* Figure out where end of printf is */
 
289
                len = strlen((char *)p->cur);   /* Length excluding nul */
 
290
                p->cur += len;
 
291
                if (p->cur > p->end)
 
292
                        p->end = p->cur;
 
293
                rv = len;
 
294
        }
229
295
        va_end(args);
230
296
        return rv;
231
297
}
232
298
 
233
 
 
234
299
/* flush all write data out to secondary storage. Return nz on failure. */
235
300
static int cgatsFileMem_flush(
236
301
cgatsFile *pp
238
303
        return 0;
239
304
}
240
305
 
 
306
/* Return the memory buffer. Error if not cgatsFileMem */
 
307
static int cgatsFileMem_get_buf(
 
308
cgatsFile *pp,
 
309
unsigned char **buf,
 
310
size_t *len
 
311
) {
 
312
        cgatsFileMem *p = (cgatsFileMem *)pp;
 
313
        if (buf != NULL)
 
314
                *buf = p->start;
 
315
        if (len != NULL)
 
316
                *len = p->end - p->start;
 
317
        return 0;
 
318
}
 
319
 
241
320
/* return the filename */
242
321
static char *cgatsFileMem_fname(
243
322
cgatsFile *pp
284
363
        p->write    = cgatsFileMem_write;
285
364
        p->gprintf  = cgatsFileMem_printf;
286
365
        p->flush    = cgatsFileMem_flush;
 
366
        p->get_buf  = cgatsFileMem_get_buf;
287
367
        p->fname    = cgatsFileMem_fname;
288
368
        p->del      = cgatsFileMem_delete;
289
369
 
290
370
        p->start = (unsigned char *)base;
291
371
        p->cur = p->start;
292
 
        p->end = p->start + length;
293
 
 
294
 
        p->size = length;
 
372
        p->aend = p->end = p->start + length;
295
373
 
296
374
        return (cgatsFile *)p;
297
375
}
333
411
/* and the error message in parse will be valid. */
334
412
static int
335
413
read_line(parse *p) {
336
 
        char c;
 
414
        int c;
337
415
        p->bo = 0;                      /* Reset pointer to the start of the line buffer */
338
416
        p->q = 0;                       /* Reset quoted flag */
339
417
        p->errc = 0;            /* Reset error status */