home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNDATE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-11
|
4KB
|
228 lines
/*
fndate.c jdc 3/10/87
% date_funcs
Date editing functions.
The field variable should be a struct tm *.
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
4/06/88 jmd added call to sed_DoSpecial
5/12/88 jmd added calls to sed_GetScratchPad()
8/20/88 jmd removed some unused variables
9/17/88 jmd added std_ funcs, errmsg
10/09/88 jmd added SED_ABORT support
10/14/88 jdc added var_size element to field_funcs_struct
12/22/88 jmd added SED_FIRST support
6/01/89 gam added ocountry stuff
6/07/89 jmd added test for mouse code (later removed)
6/05/89 gam fixed reference to COUNTRY
1/25/90 jmd converted kb_Check to kb_CheckWait
2/26/90 pmcm/mla changed month and day init in senter
3/14/90 jmd moved formatting before validation
3/16/90 jmd added support for KEY_INVALID in fexit
3/28/90 jmd ansi-fied
8/27/90 jmd removed errmsg global variable
9/11/90 pmcm changed sed_SetMouseCode to kb_Stuff
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#include "ostdlib.h" /* for atoi() */
#include "scancode.h"
#include "fnfunc.h" /* for field functions */
#include <time.h>
#include "cstime.h"
OGLOBAL field_funcs_struct date_funcs = {
std_fenter,
date_fexit,
date_fkey,
date_senter,
date_sexit,
sizeof(struct tm)
};
void date_fkey(sed_type sed)
{
int scancode, key;
scancode = kb_Read();
if (sed_DoSpecial(sed, scancode))
return;
if (special_key(sed, scancode))
return;
if (inter_field(sed, scancode))
return;
if (inter_page(sed, scancode))
return;
switch(scancode) {
case RIGHT:
sed_IncChar(sed);
break;
case LEFT:
sed_DecChar(sed);
break;
case HOME:
sed_GoHome(sed);
break;
case END:
sed_GoEnd(sed);
break;
case INS:
break;
case BACKSPACE:
sed_Overwrite(sed,'0');
sed_DecChar(sed);
break;
case DEL:
sed_Overwrite(sed,'0');
break;
default:
key = ascii(scancode);
if (isdigit(key)) {
if (sed_GetBaton(sed) == SED_FIRST) {
/* clear record on first keystroke */
sed_SetCurrRecord(sed, "000000");
sed_UpdateCurrField(sed);
}
sed_Overwrite(sed, key);
sed_IncChar(sed);
}
break;
}
/* reset baton */
sed_SetBaton(sed, -1);
}
boolean date_fexit(sed_type sed)
{
struct tm *t;
if (sed_GetBaton(sed) != SED_ABORT) {
/* format the field's record */
std_format(sed);
t = (struct tm *) sed_GetCurrVar(sed);
date_sexit(sed, sed_GetFieldNo(sed));
if (!tm_IsDateValid(t)) {
/* Invalid entry: notify fkey function via mouse code */
kb_Stuff(KEY_INVALID);
return(FALSE);
}
}
return(std_fexit(sed));
}
void date_senter(sed_type sed, int fieldno)
/*
Convert native type to string for record.
*/
{
struct tm *t;
int year, mon, day;
t = (struct tm *) sed_GetVar(sed,fieldno);
if(t->tm_year > 0) {
year = t->tm_year - 1900;
mon = t->tm_mon + 1;
day = (t->tm_mday <= 0) ? 1 : t->tm_mday;
}
else {
day = 1;
mon = 1;
year = 0;
}
switch (ocountry.date_fmt) {
case DDMMYY:
sprintf(sed_GetScratchPad(sed), "%02d%02d%02d", day,mon,year);
break;
case YYMMDD:
sprintf(sed_GetScratchPad(sed), "%02d%02d%02d", year,mon,day);
break;
default:
sprintf(sed_GetScratchPad(sed), "%02d%02d%02d", mon,day,year);
break;
}
sed_SetRecord(sed, sed_GetScratchPad(sed), fieldno);
std_senter(sed, fieldno);
}
void date_sexit(sed_type sed, int fieldno)
/*
Converts record back to native type.
*/
{
char *buf;
int f1, f2, f3;
struct tm *t;
if (sed_GetBaton(sed) == SED_ABORT) {
return;
}
t = (struct tm *) sed_GetVar(sed,fieldno);
buf = sed_GetScratchPad(sed);
strcpy(buf,sed_GetRecord(sed, fieldno));
f1 = atoi(buf+4);
*(buf+4) = '\0';
f2 = atoi(buf+2);
*(buf+2) = '\0';
f3 = atoi(buf);
if (f1 != 0 || f2 != 0 || f3 !=0) {
switch (ocountry.date_fmt){
case DDMMYY:
t->tm_year = f1 + 1900;
t->tm_mon = f2 - 1;
t->tm_mday = f3;
break;
case YYMMDD:
t->tm_mday = f1;
t->tm_mon = f2 - 1;
t->tm_year = f3 + 1900;
break;
default:
t->tm_year = f1 + 1900;
t->tm_mday = f2;
t->tm_mon = f3 - 1;
break;
}
}
else {
t->tm_year = t->tm_mon = t->tm_mday = 0;
}
}