DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
G2++ Tutorial - G2++(3C++)

A G2 Example

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:


NOTE: Neither the G2 compiler nor the G2 library are part of this release.

           $ cc main.c usr.c -lg2

Next topic: Introduction to G2++
Previous topic: The rationale for G2 (and G2++)

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004