Пример #1
0
static void list_top(char *fn)
{
  int status,done;
#define BUFLEN 256
  char buf[BUFLEN];
  gmx_cpp_t handle;
  char *cppopts[] = { NULL };

  status = cpp_open_file(fn,&handle,cppopts);
  if (status != 0) 
    gmx_fatal(FARGS,cpp_error(&handle,status));
  do {
    status = cpp_read_line(&handle,BUFLEN,buf);
    done = (status == eCPP_EOF);
    if (!done) {
      if (status != eCPP_OK)
	gmx_fatal(FARGS,cpp_error(&handle,status));
      else 
	printf("%s\n",buf);
    }
  } while (!done);
  status = cpp_close_file(&handle);
  if (status != eCPP_OK) 
    gmx_fatal(FARGS,cpp_error(&handle,status));
}
Пример #2
0
/* Return one whole line from the file into buf which holds at most n
   characters, for subsequent processing. Returns integer status. This
   routine also does all the "intelligent" work like processing cpp
   directives and so on. Note that often the routine is called
   recursively and no cpp directives are printed. */
int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
{
    gmx_cpp_t   handle = (gmx_cpp_t)*handlep;
    int         i, nn, len, status;
    const char *ptr, *ptr2;
    char       *name;
    char       *dname, *dval;
    gmx_bool    bEOF;

    if (!handle)
    {
        return eCPP_INVALID_HANDLE;
    }
    if (!handle->fp)
    {
        return eCPP_FILE_NOT_OPEN;
    }

    bEOF = feof(handle->fp);
    if (!bEOF)
    {
        /* Read the actual line now. */
        if (fgets2(buf, n-1, handle->fp) == NULL)
        {
            /* Recheck EOF, since we could have been at the end before
             * the fgets2 call, but we need to read past the end to know.
             */
            bEOF = feof(handle->fp);
            if (!bEOF)
            {
                /* Something strange happened, fgets returned NULL,
                 * but we are not at EOF.
                 */
                return eCPP_UNKNOWN;
            }
        }
    }

    if (bEOF)
    {
        if (handle->parent == NULL)
        {
            return eCPP_EOF;
        }
        cpp_close_file(handlep);
        *handlep      = handle->parent;
        handle->child = NULL;
        return cpp_read_line(handlep, n, buf);
    }
    else
    {
        if (n > handle->line_len)
        {
            handle->line_len = n;
            srenew(handle->line, n);
        }
        strcpy(handle->line, buf);
        handle->line_nr++;
    }
    /* Now we've read a line! */
    if (debug)
    {
        fprintf(debug, "%s : %4d : %s\n", handle->fn, handle->line_nr, buf);
    }

    /* Process directives if this line contains one */
    if (find_directive(buf, &dname, &dval))
    {
        status = process_directive(handlep, dname, dval);
        if (status != eCPP_OK)
        {
            return status;
        }
        /* Don't print lines with directives, go on to the next */
        return cpp_read_line(handlep, n, buf);
    }

    /* Check whether we're not ifdeffed out. The order of this statement
       is important. It has to come after #ifdef, #else and #endif, but
       anything else should be ignored. */
    if (is_ifdeffed_out(handle))
    {
        return cpp_read_line(handlep, n, buf);
    }

    /* Check whether we have any defines that need to be replaced. Note
       that we have to use a best fit algorithm, rather than first come
       first go. We do this by sorting the defines on length first, and
       then on alphabetical order. */
    for (i = 0; (i < ndef); i++)
    {
        if (defs[i].def)
        {
            nn  = 0;
            ptr = buf;
            while ((ptr = strstrw(ptr, defs[i].name)) != NULL)
            {
                nn++;
                ptr += strlen(defs[i].name);
            }
            if (nn > 0)
            {
                len = strlen(buf) + nn*max(4, 4+strlen(defs[i].def)-strlen(defs[i].name));
                snew(name, len);
                ptr = buf;
                while ((ptr2 = strstrw(ptr, defs[i].name)) != NULL)
                {
                    strncat(name, ptr, (int)(ptr2-ptr));
                    strcat(name, defs[i].def);
                    ptr = ptr2 + strlen(defs[i].name);
                }
                strcat(name, ptr);
                strcpy(buf, name);
                sfree(name);
            }
        }
    }

    return eCPP_OK;
}