home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / ENGINE / MAP_OUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-29  |  11.4 KB  |  272 lines

  1. /*
  2. *    map_out.c
  3. *
  4. *   Output mapping functions for the real screen code
  5. *
  6. *   Quincey Koziol
  7. *
  8. *    Date        Notes
  9. *    --------------------------------------------
  10. *    11/90        Started
  11. */
  12.  
  13. /*
  14. * Includes
  15. */
  16.  
  17. #define OUTPUTMASTER
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <io.h>
  23. #include <ctype.h>
  24. #ifdef MSC
  25. #ifdef __TURBOC__
  26. #include <alloc.h>
  27. #else
  28. #include <malloc.h>
  29. #endif
  30. #endif
  31. #ifdef MEMORY_DEBUG
  32. #include "memdebug.h"
  33. #endif
  34. #ifdef __TURBOC__
  35. #include "turboc.h"
  36. #endif
  37. #include "vskeys.h"
  38. #include "externs.h"
  39. #include "map_out.h"
  40.  
  41. #define MAX_LINE_LENGTH    160        /* the maximum length of a line in the output mapping file */
  42.  
  43. /* Local functions */
  44. static unsigned int parse_str(char *file_str);
  45. static void add_output(unsigned int search_key,unsigned int key_map_code);
  46. static void del_output(unsigned int search_key);
  47.  
  48. /* Local variables */
  49. static char white_sp[]="\x009\x00a\x00b\x00c\x00d\x020";    /* string which contains all the white space characters */
  50. static char end_token[]="\x000\x009\x00a\x00b\x00c\x00d\x020};";    /* string which contains all the white space characters, and the right curley brace & the semi-colon */
  51.  
  52. /**********************************************************************
  53. *  Function :    del_output
  54. *  Purpose    :    delete a node from the list of mapped keys,
  55. *                basicly just replaces the key code in the array with the
  56. *                array index.
  57. *  Parameters    :
  58. *            search_key - the keycode to delete from the list
  59. *  Returns    :    none
  60. *  Calls    :    none
  61. *  Called by    :    read_keyboard_file()
  62. **********************************************************************/
  63. static void del_output(unsigned int search_key)
  64. {
  65.     if(search_key<256)
  66.         outputtable[search_key]=(unsigned char)search_key;
  67. }    /* end del_output() */
  68.  
  69. /**********************************************************************
  70. *  Function :    add_output
  71. *  Purpose    :    add a node to the list of mapped output characters
  72. *  Parameters    :
  73. *            search_key - the character code to add to the list
  74. *            key_map_code - the character code to map the key to
  75. *  Returns    :    none
  76. *  Calls    :    none
  77. *  Called by    :    read_keyboard_file()
  78. **********************************************************************/
  79. static void add_output(unsigned int search_key,unsigned int key_map_code)
  80. {
  81.     if(search_key<256)
  82.         outputtable[search_key]=(unsigned char)key_map_code;
  83. }    /* end add_output() */
  84.  
  85. /**********************************************************************
  86. *  Function    :    parse_str()
  87. *  Purpose    :    parse the string to map a key to.
  88. *  Parameters    :
  89. *            file_str - pointer to a string from the file to parse
  90. *  Returns    :    0xFFFF for error, otherwise, the vt100 code for telbin
  91. *  Calls    :    none
  92. *  Called by    :    read_keyboard_file()
  93. **********************************************************************/
  94. static unsigned int parse_str(char *file_str)
  95. {
  96.     unsigned int ret_code;    /* the code to return from this function */
  97.     int done=0;             /* flag for dropping out of the loop */
  98.     byte *temp_str;         /* pointer to the current place in the string */
  99.  
  100.     ret_code=0xFFFF;       /* mark return code to indicate nothing special to return */
  101.     temp_str=file_str;        /* start at the beginning of the string to parse */
  102.     while((*temp_str)!='\0' && !done) {     /* parse until the end of the string or until the buffer is full */
  103.         if((*temp_str) && (*temp_str)!=';' && (*temp_str)!='\\' && (*temp_str)!='{' && (*temp_str)>' ') {    /* copy regular characters until a special one is hit */
  104.             ret_code=(*temp_str);    /* copy the character */
  105.             done=1;                 /* stop parsing the string */
  106.           }    /* end while */
  107.         if(*temp_str) {        /* check on the various special cases for dropping out of the loop */
  108.             switch(*temp_str) {        /* switch for the special case */
  109.                 case '\\':    /* backslash for escape coding a character */
  110.                     temp_str++;        /* get the character after the backslash */
  111.                     if((*temp_str)=='{') {    /* check for curly brace around numbers */
  112.                         temp_str++;    /* increment to the next character */
  113.                         if((*temp_str)=='o') /* check for octal number */
  114.                             ret_code=octal_to_int(temp_str);   /* get the acii number after the escape code */
  115.                         else if((*temp_str)=='x') /* check for hexadecimal number */
  116.                             ret_code=hex_to_int(temp_str); /* get the acii number after the escape code */
  117.                         else {        /* must be an escape integer */
  118.                             if((*temp_str)=='d')    /* check for redundant decimal number specification */
  119.                                 temp_str++;
  120.                             ret_code=atoi(temp_str);   /* get the ascii number after the escape code */
  121.                           }    /* end else */
  122.                       }    /* end if */
  123.                     else if((*temp_str)=='o') /* check for octal number */
  124.                         ret_code=octal_to_int(temp_str);   /* get the acii number after the escape code */
  125.                     else if((*temp_str)=='x') /* check for hexadecimal number */
  126.                         ret_code=hex_to_int(temp_str); /* get the acii number after the escape code */
  127.                     else {        /* must be an escape integer */
  128.                         if((*temp_str)=='d')    /* check for redundant decimal number specification */
  129.                             temp_str++;
  130.                         ret_code=atoi(temp_str);   /* get the ascii number after the escape flag */
  131.                       }    /* end else */
  132.                     done=1;
  133.                     break;
  134.  
  135.                 case '{':    /* curly brace opens a quoted string */
  136.                     temp_str++;        /* jump over the brace itself */
  137.                     if((*temp_str) && (*temp_str)!='}')    /* copy regular characters until a special one is hit */
  138.                         ret_code=(*temp_str);        /* copy the character */
  139.                     done=1;         /* drop out of the loop */
  140.                     break;
  141.  
  142.                 default:    /* ctrl characters, space, and semi-colon terminate a string */
  143.                     done=1;
  144.                     break;
  145.  
  146.               }    /* end switch */
  147.           }    /* end if */
  148.         else
  149.             done=1;
  150.       }    /* end while */
  151.     return(ret_code);
  152. }    /* end parse_str() */
  153.  
  154. /**********************************************************************
  155. *  Function    :    read_output_file
  156. *  Purpose    :    read in a output mapping file, parse the input and 
  157. *                map the output characters
  158. *  Parameters    :
  159. *            output_file - string containing the name of the output mapping file
  160. *  Returns    :    0 for no error, -1 for any errors which occur
  161. *  Calls    :    parse_str(), & lots of library string functions
  162. *  Called by    :    initoutputfile(), Sconfile()
  163. **********************************************************************/
  164. int read_output_file(char *output_file)
  165. {
  166.     FILE *output_fp;        /* pointer to the keyboard file */
  167.     char output_line[MAX_LINE_LENGTH],    /* static array to store lines read from keyboard file */
  168.         *temp_str;            /* temporary pointer to a string */
  169.     uint line_no=0,            /* what line in the file we are on */
  170.         map_code,            /* the output character to re-map to */
  171.         token_num,            /* the current token we are parsing */
  172.         where,                /* pointer to the beginning of text */
  173.         re_map_key,            /* the key to re-map */
  174.         error=0;            /* error from the file reading */
  175.  
  176.     if((output_fp=fopen(output_file,"rt"))!=NULL) {
  177.         while((temp_str=fgets(output_line,MAX_LINE_LENGTH,output_fp))!=NULL && !error) {    /* get a line of input */
  178.             token_num=0;            /* initialize the token we are on */
  179.             if((temp_str=strtok(output_line,white_sp))!=NULL) {    /* get the first token from the string */
  180.                 if((*temp_str)!=';') {        /* check for a comment line */
  181.                     do {
  182.                         switch(token_num) {        /* switch on which token we are processing */
  183.                             case 0:        /* the 'SET' token (we already know it is not a comment) */
  184.                                 if(stricmp(temp_str,"SET")) {    /* make certain the first token is a SET token */
  185.                                     printf("invalid token #%d:'%s' on line %d\n",token_num,temp_str,line_no);
  186.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  187.                                   }    /* end if */
  188.                                 break;
  189.  
  190.                             case 1:        /* the 'KEY' token */
  191.                                 if(stricmp(temp_str,"KEY")) {    /* make certain the first token is a KEY token */
  192.                                     printf("invalid token #%d:'%s' on line %d\n",token_num,temp_str,line_no);
  193.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  194.                                   }    /* end if */
  195.                                 break;
  196.  
  197.                             case 2:        /* the key to be re-mapped */
  198.                                 if(!stricmp(temp_str,"CLEAR") || !stricmp(temp_str,"OFF") || !stricmp(temp_str,"ON") || (*temp_str)==';') {    /* ignore the rest of the line if the 'key' is one of the tokens we don't support */
  199.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  200.                                   }    /* end if */
  201.                                 else {        /* the 'key' field is not a special command or a comment, must be a valid character */
  202.                                     if(*(temp_str)!='\\') {    /* the key to re-map is not an escape code */
  203.                                         re_map_key=*temp_str;    /* set the re-mapping key */
  204.                                       }    /* end if */
  205.                                     else {
  206.                                         re_map_key=atoi(temp_str+1);    /* get the re-mapping key value from the string */
  207.                                         if(re_map_key==0) {        /* invalid key code */
  208.                                             printf("Error, invalid key code:%s, on line %d\n",temp_str,line_no);
  209.                                             token_num=4;    /* bump the token count up to drop out of the loop */
  210.                                           }    /* end if */
  211.                                       }    /* end else */
  212.                                   }    /* end else */
  213.                                 break;
  214.  
  215.                             case 3:        /* the string to re-map the key to */
  216.                                 if((*temp_str)==';') {    /* ignore the rest of the line if the 'key' is one of the tokens we don't support */
  217.                                     outputtable[re_map_key]=(unsigned char)re_map_key;       /* reset the output mapping definition */
  218.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  219.                                   }    /* end if */
  220.                                 else {
  221.                                     if((map_code=parse_str(temp_str))!=0xFFFF) {      /* parse the re-mapping string */
  222.                                         add_output(re_map_key,map_code);  /* add a regular key string to the key mapping list */
  223.                                       }    /* end if */
  224.                                     else {
  225.                                         del_output(re_map_key);
  226.                                         printf("Error, re-mapping string:%s invalid on line %d\n",temp_str,line_no);
  227.                                         token_num=4;    /* bump the token count up to drop out of the loop */
  228.                                       } /* end else */
  229.                                   }    /* end else */
  230.                                 break;
  231.                           }    /* end switch */
  232.                         token_num++;
  233.                         if(token_num<3)        /* if the next token is not the last one, then grab it */
  234.                             temp_str=strtok(NULL,white_sp);        /* get the next token */
  235.                         else if(token_num==3) {        /* for the last 'token' (after the remapping key to the end of the line), just get the next character in the line */
  236.                             temp_str+=(strlen(temp_str)+1);        /* jump over the previous token */
  237.                             where=strspn(temp_str,white_sp);    /* look for the first non-white space in the line */
  238.                             temp_str+=where;    /* jump to the first position with a character */
  239.                             if(!isgraph(*temp_str)) {        /* not more characters in the line */
  240.                                 del_output(re_map_key);
  241.                                 token_num=4;    /* bump the token count to drop out of the loop */
  242.                               }    /* end if */
  243.                           }    /* end if */
  244.                       }    while(temp_str!=NULL && token_num<4);
  245.                   }    /* end if */
  246.               }    /* end if */
  247.             line_no++;            /* increment current line */
  248.           }    /* end while */
  249.         fclose(output_fp);
  250.       }    /* end if */
  251.     else
  252.         error=(-1);        /* indicate an error */
  253.     return(error);
  254. }    /* end read_keyboard_file() */
  255.  
  256. /**********************************************************************
  257. *  Function    :    initoutputfile()
  258. *  Purpose    :    initialize the default output mappings
  259. *  Parameters    :    none
  260. *  Returns    :    none
  261. *  Calls    :    none
  262. *  Called by    :    main()
  263. **********************************************************************/
  264. void initoutputfile(void )
  265. {
  266.     int i;                /* local counting variable */
  267.  
  268.     for(i=0; i<256; i++)    /* just set the mappings in the table to their one code */
  269.         outputtable[i]=(unsigned char)i;
  270. }    /* end initoutputfile() */
  271.  
  272.