printf, data presentation

Tony Field tony at xenlink.UUCP
Fri Dec 30 14:41:08 AEST 1988



(I am posting this on comp.lang.c and comp.unix.wizards.  If this is
inappropriate for either group, my mistake.)

I wonder why C i/o functions - including curses - do not provide a simple
method of accepting 'fixed format' screen input?  The following example
illustrates the problem and also shows a simple solution.

Often, a programmer must construct complex data acquisition screens that
have a number of user-enterable fields. For example, a Human Resources
system might need a screen that looks like:

+-----------------------------------------------------------------------------+
|                    H U M A N     R E S O U R C E S                          |
|                                                                             |
|   NAME: ______________________________________       DEPARTMENT: _ __       |
|ADDRESS: __________________________________________                          |
|         __________________________________________                          |
|         __________________________________________                          |
|                                                                             |
|        S.I.N.: ___ ___ ___         PHONE: ___ _______          (RES)        |
|                                                                             |
|MONTHLY BASE SALARY: __________              YEARLY BASE SALARY: __________  |
+-----------------------------------------------------------------------------+

The normal way of solving this problem is to develop special code to
allow the user to traverse the screen.

What the programmer really wants to do is say, "Here is a screen image.
Place the user's data field responses into these variables.". I have seen
many "data windows" packages - mostly in MSDOS - but none of them seems
to follow the spirit of C. In spirit, all formatted display in C should
follow the general conventions of "printf/sscanf".

For example, the Human Resources screen could be presented to the user with
the following type of code:

      wstart (editsub,BLUE,GREEN,GREY,BLUE,BAR);

      wdefine (20,0," H U M A N    R E S O U R C E S ");
      wdefine (0,4,"   NAME: %40s   DEPARTMENT: %1c %3d",hr.name,&hr.dept,&hr.dnumb);
      wdefine (0,5,"ADDRESS: %40s",hr.addr1);
      wdefine (9,6,"%40s",hr.addr2);
      wdefine (9,7,"%40s",hr.addr3);
      wdefine (9,9,"S.I.N.: %-10ld       PHONE: %20s  (res)",&hr.sin,hr.phone);
      wdefine (0,11,"MONTHY BASE SALARY $%10.2lf",&hr.salary);
      wdefine (40,11,"YEARLY BASE SALARY $%10.2plf",&yearly);

      do
      {  wfetch_screen ();
         error_code = edit_user_data (&hr);
      } while (error_code);
      
The function "wstart" prepares the software to accept a new screen
definition. In addition it provides colour (or other) attributes for
prompt and data fields.

The "wdefine" function calls set up the various data fields and places
the various prompts and current contents of the data fields on the
screen. The parameters first specify the x-y screen coordinates, the
formatting string for the prompts and data fields, and the the ADDRESS of
the various data items (like sscanf).

Simple analysis of the format string can easily segregate the "prompt"
information from the formatting description of the data items. In
addition, sufficient information is provided to locate the x-y
coordinate, size, and type of every data field.

After the screen has been defined with "wdefine", a single call to
function "wfetch_screen" can walk the user through the entire screen
structure (even in a user-defined random pattern). When wfetch_screen
returns, the programmer can assume all of the variables have been filled
in by the user. The programmer's error checking routine can be called
until the user's data is error-free.

The mechanism presented above illustrates a screen presentation style
that is familiar to IBM-3270 programmers (and dBase programmers) : i.e.
present a screen to the user, fetch all his responses, then edit the
data.

Simple extensions could be made to allow field-by-field data editing.
This would be more consistent with programming styles on *nix system.
The field-by-field editing also can allow 'dynamic data help' to provide
user with help information.

I feel that C really needs a cleaner method of accepting screen input
than currently provided by printf/curses.  There should be a 'standard
C library' set of functions, consistent with the style of C, that
allow full-screen data presentation.

If any one is interested in this general problem, I would appreciate
their comments.  In addition, I could provide a bunch of C code (that
currently runs under Xenix or QNX) that implements all of the above.



More information about the Comp.unix.wizards mailing list