APPENDIX C Sample C Routine to read ASCII files


Andy Dibbins (dibbinaj@boat.bt.co.uk) offers:

Here's a little contribution to the section about ascii access from 4gl, I found these code examples in a magazine called "The C USERS JOURNAL".


/***********************************************************************
*                                                                      *
*    File Name : stdio4gl.c                                             *
*                                                                      *
*    Description : Source code for the functions listed in fgiusr.c     *
*                                                                      *
*    Author : Andy Dibbins                                              *
*                                                                      *
*    Date written : 13/7/90                                             *
*                                                                      *
*    Date last modified : 13/05/92 L davies increase buffer size        *
*                                                                      *
***********************************************************************/
#include <string.h>
#include <stdio.h>
#include <signal.h>

#define FGL_BUFSIZE 2064

extern int int_flag;
extern int readpend;

/***********************************************************************
    4GL syntax is "CALL fgl_fopen(filename,mode) returning filehandle"
***********************************************************************/

fgl_fopen(nargs)
int nargs;
{
    char filename[80];
    char mode[10];
    FILE *ascfile;

    if (nargs != 2)  {
     fprintf (stderr,
            "fgl_fopen: wrong number of arguments");
     exit(1);
    }

    popquote(mode,sizeof(mode)-1);      
    popquote(filename,sizeof(filename)-1);
    clipped(mode);            
    clipped(filename);
    if(!(ascfile = fopen(filename,mode)))  {
     fprintf (stderr,
         "fgl_fopen: unable to open file %s",filename);
     exit(1);
    }

    retlong((long)ascfile);   
    return 1;  
}

/***********************************************************************
    4GL syntax is "CALL fgl_getkey()"
***********************************************************************/

fgl_getkey(nargs)
int nargs;
{
     short ret_code = 0;

     if (nargs != 0)  {
          fprintf(stderr,
               "fgl_getkey : wrong number of arguments");
          exit(1);
     }

     ret_code = rgetkey();
     retshort(ret_code);
     return 1;
}

/***********************************************************************
    4GL syntax is "CALL fgl_fclose(filehandle)"
***********************************************************************/

fgl_fclose(nargs)
int nargs;
{
    long filehandle;

    if (nargs != 1)  {
          fprintf(stderr,
            "fgl_fclose: wrong number of arguments");
          exit(1);
    }

    poplong(&filehandle);               
    fclose((FILE *)filehandle);
    return 0;
}

/*********************************************************************
    4GL syntax is "CALL fgl_fgets(filehandle) returning err_code, buffer"
************************************************************************/

fgl_fgets(nargs)
int nargs;
{
    long filehandle;
    char buffer[FGL_BUFSIZE];
    short err_code = 0;
    int len;

    if (nargs != 1)  {
     fprintf(stderr,
            "fgl_fclose: wrong number of arguments");
     exit(1);
    }

    poplong(&filehandle);
    memset(buffer,0,sizeof(buffer));
    if (fgets(buffer,sizeof(buffer)-1,(FILE *)filehandle) != NULL)  {
          len = strlen(buffer);
          buffer[len-1] = '\0';
    }
    else  {
          err_code = -1;
    }
    retshort(err_code);
    retquote(buffer);
    return 2;
}

/***********************************************************************
    4GL syntax is "CALL fgl_fputs(buffer,filehandle) returning err_code"
***********************************************************************/

fgl_fputs(nargs)
int nargs;
{
    long filehandle;
    char buffer[FGL_BUFSIZE];
    short err_code=0;
    int len;

    if (nargs != 2)  {
        fprintf (stderr,
           "fgl_fopen: wrong number of arguments");
        exit(1);
    }

    poplong(&filehandle);
    popquote(buffer,sizeof(buffer)-1);
    clipped(buffer,(char)' ');     
    err_code = fputs(buffer,(FILE *)filehandle);
    fputc('\n',(FILE *)filehandle);
    retshort(err_code);
    return 1;
}

/********************************************************************
    4GL syntax is "CALL fgl_max(arg1,arg2,...) returning max_val"
*********************************************************************/

fgl_max(nargs)
int nargs;
{
    int i;
    short test_val;
    short max_val = -32767;

    for (i=0;i<nargs; i++)  {
        popshort(&test_val);
          if (test_val > max_val)  {
            max_val = test_val;
          }
    }
    retshort(max_val);
    return 1;
}


/********************************************************************
     strip trailing spaces from string
*********************************************************************/

clipped(str)
char *str;
{
     char *blank;

     blank = str + strlen(str) - 1;
     while (*blank == ' ')  {
          *blank-- = '\0';
     }
}

/*********************************************************************
     End of STDIO4GL.C
**********************************************************************/