
/****************************************************************************/

/*  Module PNLCVALC.C

    Copyright 1986 Roundhill Computer Systems Limited
    All rights reserved

    Use and disclosure of this code are prohibited unless in accordance
    with the terms of the PANEL software licence.

    ************************************************************************
    *** THE OBJECT CODE VERSION OF THIS MODULE IS INCLUDED IN THE PANEL  ***
    *** LIBRARY.  IF YOU AMEND AND COMPILE THIS MODULE AND LINK IT WITH  ***
    *** YOUR APPLICATION IT WILL SUPERSEDE THE LIBRARY VERSION.          ***
    ************************************************************************

    PANEL:  Type 4          : Character Validation
            Source Language : C

    Modification History (Push-Down List):  

            27.12.85        : Convert to table-driven operation

******************************************************************************/

#define VALCARG 1
#include <pnl4.h>

/*  Working fields for character validation functions */

#define point '.'       /* change decimal point to comma if you wish */

int PANXBD = NXABD;

uchar PAVAL[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
uchar PAVLN[13] = {'0','1','2','3','4','5','6','7','8','9',point,'-'};
uchar PAXTRA[17] = "";  /* extra characters (pairs xX) for upc conversion */

uchar *PVINCH, *PVDINCH, *PVNINCH;  /* args for character validation */

extern int (*PACVTB[])();   /* character validation function table */
extern int PACVPM;          /* max index into function table */

/*****************************************************************************/

int PACVAL(inch,dinch,ninch)

/*  Character Validation Base Module.  

    Calling conventions:
    
      On entry, 
            - PACRF is the FCB of the field being input
            - *INCH is the character just input
            - *DINCH contains a copy if INCH
            - *NINCH contains a null character
        
      On exit, 
            - no change should be made to PACRF
            - the field's data area may be altered with care
            - *INCH will normally be stored in the data area by PANEL
            - *DINCH will normally be echoed to the user
            - *NINCH will be forced to be the next i/p char
            - the PACVAL return code will be interpreted as follows:
    
               0:  Character is OK, store and display it (if ECHO set);
               1:  Character is bad, beep it
               2:  Character is to be thrown away
               3:  Character is to be ignored, but test for field end

    If the validation number is higher than the table maximum, use the default

******************************************************************************/

uchar *inch,*dinch,*ninch;

{          
register int i,j;
int cv;
    
PVINCH = inch;                  /* save static values */
PVDINCH = dinch;
PVNINCH = ninch;

if ( (PACRF.sa & upper) ||      /* carry out upper case conversion */
     (PACRF.charvaln == 5 && PACRF.fi == 0) )   /* see CV 5 below */
    {                           /* first do standard ASCII a..z */
    if ( (*inch >= 'a' ) && (*inch <= 'z') )
        *inch += 'A'-'a';
      else
        {                       /* then handle any extra pairs in PAXTRA */
        if (PAXTRA[0])          /* some extra characters to test */
            {                   /* string should be "aAbB...." etc */
            for (i = 0; PAXTRA[i]; i++)     /* scan to end of string */
                {                           /* string should have 2n chars */
                if ( (*inch == PAXTRA[i])   /* matched char in string */
                   && ( !(i & 0x1) )        /* in pos 0,2,4... */
                   && ( PAXTRA[i+1] ) )     /* ignore if string end (odd no) */
                     *inch = PAXTRA[i+1];   /* replace with neighbour char */
                }
            }
        }
    *dinch = *inch;             /* change display char also */
    }

if (!(*inch)) return (1);       /* invalid key-function always bad char */

cv = UCI(PACRF.charvaln);
return (*PACVTB[(cv>PACVPM)?0:cv])();  
}
