home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / FUNCS.EXE / CSCAPE / SOURCE / FNTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  3.2 KB  |  176 lines

  1. /*
  2.     fntime.c    
  3.  
  4.     % time_funcs
  5.  
  6.     Time editting functions.
  7.     The field variable should be a struct tm *.
  8.  
  9.     C-scape 3.1
  10.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  11.     ALL RIGHTS RESERVED.
  12.  
  13.     Revision History:
  14.     -----------------
  15.      3/12/87 jdc    created
  16.      4/06/88 jmd     added call to sed_DoSpecial
  17.      5/12/88 jmd    added calls to sed_GetScratchPad()
  18.      9/17/88 jmd     added std_ funcs, errmsg
  19.     10/09/88 jmd     added SED_ABORT support
  20.     10/14/88 jdc    added var_size element to field_funcs_struct
  21.     12/22/88 jmd     added SED_FIRST support
  22.  
  23.      6/07/89 jmd    added test for mouse code (later removed)
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30.  
  31. #include "cscape.h"
  32. #include "ostdlib.h"        /* for atoi() */
  33.  
  34. #include "scancode.h"
  35.  
  36. #include <time.h>
  37. #include "cstime.h"
  38.  
  39. /* error message (global variable can be changed by programmer) */
  40. char *fntime_errmsg = "Invalid time";
  41.  
  42. OGLOBAL field_funcs_struct time_funcs = {
  43.     std_fenter,
  44.     time_fexit,
  45.     time_fkey,
  46.     time_senter,
  47.     time_sexit,
  48.     sizeof(struct tm)
  49. };
  50.  
  51. void time_fkey(sed)
  52.     sed_type sed;
  53. {
  54.     int   scancode, key;
  55.  
  56.     scancode = kb_Read();
  57.  
  58.     if (sed_DoSpecial(sed, scancode))
  59.         return;
  60.     if (special_key(sed, scancode))
  61.         return;
  62.     if (inter_field(sed, scancode))
  63.         return;
  64.     if (inter_page(sed, scancode))
  65.         return;
  66.  
  67.     switch(scancode) {
  68.     case ESC:
  69.         sed_SetBaton(sed, 0);
  70.         sed_ToggleExit(sed);
  71.         break;
  72.     case RIGHT:
  73.         sed_IncChar(sed);
  74.         break;
  75.     case LEFT:
  76.         sed_DecChar(sed);
  77.         break;
  78.     case HOME:
  79.         sed_GoHome(sed);
  80.         break;
  81.     case END:
  82.         sed_GoEnd(sed);
  83.         break;
  84.     case INS:
  85.         break;
  86.     case BACKSPACE:
  87.         sed_Overwrite(sed,'0');
  88.         sed_DecChar(sed);
  89.         break;
  90.     case DEL:
  91.         sed_Overwrite(sed,'0');
  92.         break;
  93.     default:
  94.         key = ascii(scancode);
  95.         if (isdigit(key)) {
  96.             if (sed_GetBaton(sed) == SED_FIRST) {
  97.                 /* clear record on first keystroke */
  98.                 sed_SetCurrRecord(sed, "000000");
  99.                 sed_UpdateCurrField(sed);
  100.             }
  101.             sed_Overwrite(sed, key);
  102.             sed_IncChar(sed);
  103.         }
  104.         break;
  105.     }
  106.  
  107.     /* reset baton */
  108.     sed_SetBaton(sed, -1);
  109. }
  110.  
  111. boolean time_fexit(sed)
  112.     sed_type sed;
  113. {
  114.     if (sed_GetBaton(sed) != SED_ABORT) {
  115.         time_sexit(sed, sed_GetFieldNo(sed));
  116.  
  117.         if (!tm_IsTimeValid((struct tm *) sed_GetCurrVar(sed))) {
  118.             tone();
  119.             sed_BorderPrompt(sed, fntime_errmsg);
  120.             /* wait for a keystroke */
  121.             while (!kb_Check())
  122.                 ;
  123.             sed_BorderPrompt(sed, sed_GetCurrFieldData(sed, 0));
  124.             return(FALSE);
  125.         }
  126.     }
  127.  
  128.     return(std_fexit(sed));
  129. }
  130.  
  131. void time_senter(sed, fieldno)
  132.     sed_type sed;
  133.     int fieldno;
  134. /*
  135.     Convert native type to string for record.
  136. */
  137. {
  138.     struct tm *t;
  139.  
  140.     t = (struct tm *) sed_GetVar(sed,fieldno);
  141.  
  142.     sprintf(sed_GetScratchPad(sed), "%02d%02d%02d", (t->tm_hour),t->tm_min,(t->tm_sec));
  143.  
  144.     sed_SetRecord(sed, sed_GetScratchPad(sed), fieldno);
  145.  
  146.     std_senter(sed, fieldno);
  147. }
  148.  
  149. void time_sexit(sed, fieldno)
  150.     sed_type sed;
  151.     int fieldno;
  152. /*
  153.     Converts record back to native type.
  154. */
  155. {
  156.     char  *buf;
  157.     struct tm *t;
  158.  
  159.     if (sed_GetBaton(sed) != SED_ABORT) {
  160.         t = (struct tm *) sed_GetVar(sed,fieldno);
  161.  
  162.         buf = sed_GetScratchPad(sed);
  163.  
  164.         strcpy(buf,sed_GetRecord(sed, fieldno));
  165.  
  166.         t->tm_sec = atoi(buf+4);
  167.         *(buf+4) = '\0';
  168.         t->tm_min = atoi(buf+2);
  169.         *(buf+2) = '\0';
  170.         t->tm_hour = atoi(buf);
  171.     }
  172. }
  173.  
  174.  
  175.  
  176.