/branches/supermon/Supermon/src/common/sexpr_0.2.2/io.c
# · C · 190 lines · 110 code · 29 blank · 51 comment · 44 complexity · 0224f1b49946161bbf24409dafbae555 MD5 · raw file
- /**
- This software and ancillary information (herein called "SOFTWARE")
- called Supermon is made available under the terms described
- here. The SOFTWARE has been approved for release with associated
- LA-CC Number LA-CC 99-51.
- Unless otherwise indicated, this SOFTWARE has been authored by an
- employee or employees of the University of California, operator of the
- Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
- the U.S. Department of Energy. The U.S. Government has rights to use,
- reproduce, and distribute this SOFTWARE, and to allow others to do so.
- The public may copy, distribute, prepare derivative works and publicly
- display this SOFTWARE without charge, provided that this Notice and
- any statement of authorship are reproduced on all copies. Neither the
- Government nor the University makes any warranty, express or implied,
- or assumes any liability or responsibility for the use of this
- SOFTWARE.
- If SOFTWARE is modified to produce derivative works, such modified
- SOFTWARE should be clearly marked, so as not to confuse it with the
- version available from LANL.
- **/
- /** NOTE: This library is part of the supermon project, hence the name
- supermon above. **/
- /***
- * Matt's smaller s-expression parsing library
- *
- * Written by Matt Sottile (matt@lanl.gov), January 2002.
- ***/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "sexpr.h"
- #include <assert.h>
- /*
- * read_sexp :: same as fread_sexp, except with read vs fgetch.
- */
- int
- read_sexp (char *buf, int size, int f)
- {
- signed char ch = 0;
- char *sexp;
- int i = 0, depth = 0;
- int numread;
-
- sexp = (char *) malloc (sizeof (char) * size);
- assert(sexp != NULL);
- /* read until a '(' */
- do
- {
- numread = read (f, &ch, 1);
- }
- while (ch != '(' && numread == 1);
- if (numread < 1)
- return 0; /* bail! - no sexp here. */
- sexp[i] = ch;
- i++;
- depth++; /* inc depth since we know we have a '(' */
- while (depth > 0)
- {
- numread = read (f, &ch, 1);
- if (ch == ')')
- depth--;
- else if (ch == '(')
- depth++;
- else if (numread < 1) {
- free(sexp); /* fix leak - thanks smj */
- return 0; /* bail! */
- }
- /* strip out CR and LF -- I hate them. */
- if (ch != '\n' && ch != '\r')
- {
- sexp[i] = ch;
- i++;
- }
- if (i == size) {
- fprintf(stderr,"read_sexp overran buffer. %d bytes is too small.\n",
- size);
- free(sexp);
- return -1;
- }
- }
- /* null terminate the string */
- sexp[i] = '\0';
- if (i > size)
- {
- strncpy (buf, sexp, size);
- fprintf (stderr, "Warning: read_sexp over buffer size - truncating.\n");
- }
- else
- strncpy (buf, sexp, i + 1);
- free (sexp);
- return 1;
- }
- /*
- * fread_sexp :: given a FD, read until ( and return the string including the
- * ( to the corresponding ). return 1 if OK, 0 otherwise. this is useful,
- * since now we work by sexps, not lines. So if someone asks for a variable
- * the expression might span multiple lines. Not hard to deal with.
- * Use this if you're reading from a file or socket and know that the
- * matchine parens for the s-expr might not be on the same line.
- *
- * Return value of 0 = fail, 1 = succeed.
- */
- int
- fread_sexp (char *buf, int size, FILE * fd)
- {
- signed char ch = 0;
- char *sexp;
- int i = 0, depth = 0;
- sexp = (char *) malloc (sizeof (char) * size);
- assert(sexp != NULL);
- /* read until a '(' */
- do
- {
- ch = fgetc (fd);
- }
- while (ch != '(' && ch != EOF);
- if (ch == EOF)
- return 0; /* bail! - no sexp here. */
- sexp[i] = ch;
- i++;
- depth++; /* inc depth since we know we have a '(' */
- while (depth > 0)
- {
- ch = fgetc (fd);
- if (ch == ')')
- depth--;
- else if (ch == '(')
- depth++;
- else if (ch == EOF) {
- free(sexp); /* fix leak - thanks smj */
- return 0; /* bail! */
- }
- /* strip out CR and LF -- I hate them. */
- if (ch != '\n' && ch != '\r')
- {
- sexp[i] = ch;
- i++;
- }
- if (i == size) {
- fprintf(stderr,"fread_sexp overran buffer. %d bytes is too small.\n",
- size);
- free(sexp); /* fix leak - thanks smj */
- return -1;
- }
- }
- /* null terminate the string */
- sexp[i] = '\0';
- if (i > size)
- {
- strncpy (buf, sexp, size);
- fprintf (stderr, "Warning: read_sexp over buffer size - truncating.\n");
- }
- else
- strncpy (buf, sexp, i + 1);
- free (sexp);
- return 1;
- }
- /**
- * Function that can parse directly from a file descriptor
- */