summaryrefslogtreecommitdiff
path: root/src/inp_str.c
diff options
context:
space:
mode:
authorthevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2009-03-23 10:36:47 +0000
committerthevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2009-03-23 10:36:47 +0000
commit243a2bd11df5ac0fe907fcfe4b8a6ebe39e05735 (patch)
treeac4348b4e5d95c83d1bf3de6c761a0c0b8e20eb0 /src/inp_str.c
parent5d5659ecb95f76a69964bbd240b783b560b576cc (diff)
downloadmpc-243a2bd11df5ac0fe907fcfe4b8a6ebe39e05735.tar.gz
src/inp_str.c: Accept just two levels of parentheses
tests/inp_str.dat: tests with number of read characters tests/tio_str.c: exit when error, test number of read characters. git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@495 211d60ee-9f03-0410-a15a-8952a2c7a4e4
Diffstat (limited to 'src/inp_str.c')
-rw-r--r--src/inp_str.c42
1 files changed, 23 insertions, 19 deletions
diff --git a/src/inp_str.c b/src/inp_str.c
index 7910416..c748352 100644
--- a/src/inp_str.c
+++ b/src/inp_str.c
@@ -68,35 +68,39 @@ mpc_inp_str (mpc_ptr rop, FILE *stream, size_t *read, int base, mpc_rnd_t rnd_mo
white = skip_whitespace (stream);
c = getc (stream);
if (c != EOF) {
- /* If c=='(', set par=true and read everything up to ')'; otherwise,
- par=false already and read everything up to white space.
- Then have mpc_setstr do the work. */
+ /* If c=='(', set par=1 and read everything up to ')' skipping one level
+ of nested pair of parentheses; otherwise, par=0 already and read
+ everything up to white space. Then have mpc_setstr do the work. */
size_t strsize = 100;
char *str = mpc_alloc_str (strsize);
- if (c == '(')
- par = 1;
- while (c != EOF &&
- ((par && c != ')') || (!par && !isspace ((unsigned char) c)))) {
+ while (c != EOF && c != '\n'
+ && (par != 0 || !isspace ((unsigned char) c))) {
str [nread] = (char) c;
nread++;
if (nread == strsize) {
str = mpc_realloc_str (str, strsize, 2 * strsize);
strsize *= 2;
}
- c = getc (stream);
- }
- if (c != EOF) {
- if (par) {
- str [nread] = ')';
- nread++;
+ if (c == '(') {
+ par++;
+ if (par > 2)
+ break;
}
- else /* put whitespace back into stream */
- ungetc (c, stream);
- str = mpc_realloc_str (str, strsize, nread + 1);
- strsize = nread + 1;
- str [nread] = '\0';
- inex = mpc_set_str (rop, str, base, rnd_mode);
+ else if (c == ')') {
+ par--;
+ if (par < 0)
+ break;
+ }
+ c = getc (stream);
}
+ if (isspace ((unsigned char) c))
+ ungetc (c, stream);
+
+ str = mpc_realloc_str (str, strsize, nread + 1);
+ strsize = nread + 1;
+ str [nread] = '\0';
+ inex = mpc_set_str (rop, str, base, rnd_mode);
+
mpc_free_str (str);
}