| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup cgraph_core |
| 4 |
*/ |
| 5 |
/************************************************************************* |
| 6 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 7 |
* All rights reserved. This program and the accompanying materials |
| 8 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 9 |
* which accompanies this distribution, and is available at |
| 10 |
* https://www.eclipse.org/legal/epl-v10.html |
| 11 |
* |
| 12 |
* Contributors: Details at https://graphviz.org |
| 13 |
*************************************************************************/ |
| 14 |
|
| 15 |
#include "config.h" |
| 16 |
|
| 17 |
#include <stddef.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <cgraph/cghdr.h> |
| 20 |
#include <cgraph/rdr.h> |
| 21 |
|
| 22 |
static int iofread(void *chan, char *buf, int bufsize) |
| 23 |
{ |
| 24 |
if (fgets(buf, bufsize, chan)) |
| 25 |
return (int)strlen(buf); |
| 26 |
else |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
/* default IO methods */ |
| 31 |
static int ioputstr(void *chan, const char *str) |
| 32 |
{ |
| 33 |
return fputs(str, chan); |
| 34 |
} |
| 35 |
|
| 36 |
static int ioflush(void *chan) |
| 37 |
{ |
| 38 |
return fflush(chan); |
| 39 |
} |
| 40 |
|
| 41 |
Agiodisc_t AgIoDisc = { iofread, ioputstr, ioflush }; |
| 42 |
|
| 43 |
static int |
| 44 |
memiofread(void *chan, char *buf, int bufsize) |
| 45 |
{ |
| 46 |
const char *ptr; |
| 47 |
char *optr; |
| 48 |
char c; |
| 49 |
int l; |
| 50 |
rdr_t *s; |
| 51 |
|
| 52 |
if (bufsize == 0) return 0; |
| 53 |
s = chan; |
| 54 |
if (s->cur >= s->len) |
| 55 |
return 0; |
| 56 |
l = 0; |
| 57 |
ptr = s->data + s->cur; |
| 58 |
optr = buf; |
| 59 |
/* We know we have at least one character */ |
| 60 |
c = *ptr++; |
| 61 |
do { |
| 62 |
*optr++ = c; |
| 63 |
l++; |
| 64 |
/* continue if c is not newline, we have space in buffer, |
| 65 |
* and next character is non-null (we are working with |
| 66 |
* null-terminated strings. |
| 67 |
*/ |
| 68 |
} while (c != '\n' && l < bufsize && (c = *ptr++)); |
| 69 |
s->cur += (size_t)l; |
| 70 |
return l; |
| 71 |
} |
| 72 |
|
| 73 |
static Agiodisc_t memIoDisc = {memiofread, 0, 0}; |
| 74 |
|
| 75 |
static Agraph_t *agmemread0(Agraph_t *arg_g, const char *cp) |
| 76 |
{ |
| 77 |
rdr_t rdr; |
| 78 |
Agdisc_t disc; |
| 79 |
|
| 80 |
memIoDisc.putstr = AgIoDisc.putstr; |
| 81 |
memIoDisc.flush = AgIoDisc.flush; |
| 82 |
rdr.data = cp; |
| 83 |
rdr.len = strlen(cp); |
| 84 |
rdr.cur = 0; |
| 85 |
|
| 86 |
disc.id = &AgIdDisc; |
| 87 |
disc.io = &memIoDisc; |
| 88 |
if (arg_g) return agconcat(arg_g, NULL, &rdr, &disc); |
| 89 |
return agread(&rdr, &disc); |
| 90 |
} |
| 91 |
|
| 92 |
Agraph_t *agmemread(const char *cp) |
| 93 |
{ |
| 94 |
return agmemread0(0, cp); |
| 95 |
} |
| 96 |
|
| 97 |
Agraph_t *agmemconcat(Agraph_t *g, const char *cp) |
| 98 |
{ |
| 99 |
return agmemread0(g, cp); |
| 100 |
} |