|
|
Let's start by looking at a simple G2 program. In the next section, ``Introduction to G2++'', we'll look at the equivalent G2++ program and discuss the differences. The G2 program uses the following record definition, written in the G2 record definition language:
usr.g
usr
login 6
id
usr LONG
grp SHORT
name 20
proj
4 LONG
First, we compile usr.g using the G2 compiler, g2comp:
$ g2comp usr.g
to which g2comp responds
usr.g:
=>usr.[hc]
indicating that two files have been created: usr.h and usr.c. You need not be concerned with usr.c; just remember to compile and link it together with your application (see below) usr.h contains a structure definition for type struct USR:
usr.h -- generated by g2comp
typedef struct USR{
char login[6+1];
struct{
long usr;
short grp;
}id;
char name[20+1];
long proj[4];
}USR;
Client programs which include this file can declare variables of type struct USR to serve as the source or destination of I/O operations. The following client program reads a file of G2 records, updating usr records and passes other records through unchanged.
main.c - C program
#include <g2.h>
#include "usr.h"
main(){
char name[G2MAXNAME];
while( getname(name,stdin) ){
if( strcmp(name,"usr")==0 ){
USR u;
getbody(&u,usr,stdin);
u.id.grp =+ 100;
putrec(&u,usr,stdout);
}else{
G2BUF b;
getbuf1(&b,name,stdin);
putbuf(&b,stdout);
}
}
}
Finally, we compile and link this program together with usr.c and I/O routines from the G2 library:
$ cc main.c usr.c -lg2