home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / dvorak.zip / DVORAK.C next >
C/C++ Source or Header  |  1994-01-17  |  5KB  |  193 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "dvorak.h"
  6.  
  7. static VOID PrintText(CHAR **ppcText);
  8. static SHORT ProcessFile(FILE *fp, ULONG ulIndex);
  9. static SHORT ProcessEntry(FILE *fp, IndexEntry *oEntry);
  10. static SHORT Assignments(KeyDef *oOrgTable, KeyDef *oNewTable, INT iEntryCnt);
  11.  
  12. static CHAR *pcStartUp[] =
  13. {
  14.     "DVORAK -- This program will change your OS/2 DCP file to use a Dvorak keyboard",
  15.     "configuration as your default. To change back from the Dvorak keyboard",
  16.     "please type 'keyb XXX' where XXX is another country that is not 'US'.",
  17.     "",
  18.     "Note: This program was written by Andrew Stern and tested on the file",
  19.     "keyboard.dcp 4/26/93.  I wrote this program because I was told that IBM had no",
  20.     "support for a Dvorak keyboard in their operating system. Hopefully they will",
  21.     "correct this oversight in a future release.",
  22.     "(C) 1994 By Andrew Stern",
  23.     "Additional credit goes to Ned Konz who wrote SWAPDCP.",
  24.     "Please feel free to distribute and modify anything you want as long as you",
  25.     "include this source file unmodified with your distribution.",
  26.     "Andrew Stern at CS: 71022,3257",
  27.     0
  28. };
  29.  
  30. /*    "DEVINFO=KBD,US,C:\\OS2\\KEYBOARD.DCP */
  31.  
  32. static CHAR *cpHelp[] =
  33. {
  34.     "Usage: DVORAK filename.DCP",
  35.     0
  36. };
  37.  
  38. static SHORT sAssignKey[MAX_ASSIGN][2] = {12, 26, 13, 27, 16, 40, 17, 51, 18, 52, 19, 25, 20, 21, 21, 33, 22, 34,
  39.  23, 46, 24, 19, 25, 38, 26, 53, 27, 13, 31, 24, 32, 18, 33, 22, 34, 23,
  40.  35, 32, 36, 35, 37, 20, 38, 49, 39, 31, 40, 12, 44, 39, 45, 16, 46, 36,
  41.  47, 37, 48, 45, 49, 48, 51, 17, 52, 47, 53, 44};
  42.  
  43. INT main(INT argc, CHAR *argv[])
  44. {
  45.     CHAR    *pcFileName;
  46.     FILE    *fp;
  47.     ULONG    ulIndex;
  48.     SHORT    sRtn;
  49.  
  50.     PrintText(pcStartUp);
  51.  
  52.     if (argc != 2) {
  53.         PrintText(cpHelp);
  54.         return(1);
  55.     }
  56.  
  57.     pcFileName = *++argv;
  58.     if ((fp = fopen(pcFileName, "r+b")) == NULL) {
  59.         printf("ERROR: Could not open file %s.\n", pcFileName);
  60.         return(1);
  61.     }
  62.  
  63.     /* Read location of index */
  64.     ulIndex = 0L;
  65.     fread(&ulIndex, sizeof(ulIndex), 1, fp);
  66.     sRtn = ProcessFile(fp, ulIndex);
  67.  
  68.     fclose(fp);
  69.     return(sRtn);
  70. }
  71.  
  72. static VOID PrintText(CHAR **ppcText)
  73. {
  74.     CHAR **ppcStr;
  75.  
  76.     for (ppcStr = ppcText; *ppcStr; ppcStr++) {
  77.         printf("%s\n", *ppcStr);
  78.     }
  79. }
  80.  
  81. static SHORT ProcessFile(FILE *fp, ULONG ulIndex)
  82. {
  83.     WORD            wNumEntries;
  84.     WORD            wEntryIndex;
  85.     IndexEntry    *oEntry;
  86.     INT            wNumRead;
  87.     SHORT            sRtn;
  88.  
  89.     fseek(fp, (ULONG) ulIndex, SEEK_SET);
  90.     wNumEntries = 0;
  91.     fread(&wNumEntries, sizeof(wNumEntries), 1, fp);
  92.  
  93.     oEntry = calloc(wNumEntries, sizeof(IndexEntry));
  94.     if (oEntry == NULL) {
  95.         printf("ERROR: Unable to create memory for index of entries.\n");
  96.         return(1);
  97.     }
  98.  
  99.     if ((wNumRead = fread(oEntry, sizeof(IndexEntry), wNumEntries, fp)) != wNumEntries) {
  100.         printf("ERROR: Unable to read all the entry tables.\n");
  101.         return(1);
  102.     }
  103.  
  104.     sRtn = 0;
  105.     for (wEntryIndex = 0; wEntryIndex < wNumEntries; wEntryIndex++) {
  106.         if (oEntry[wEntryIndex].HeaderLocation) {
  107.             sRtn |= ProcessEntry(fp, oEntry + wEntryIndex);
  108.         }
  109.     }
  110.  
  111.     free(oEntry);
  112.     return(sRtn);
  113. }
  114.  
  115. static SHORT ProcessEntry(FILE *fp, IndexEntry *oEntry)
  116. {
  117.     LONG        lTablePosition;
  118.     XHeader    *oHeader;
  119.     VOID        *oOrgKeys;
  120.     VOID        *oNewKeys;
  121.     SHORT        sRtn;
  122.  
  123.     oHeader = calloc(sizeof(XHeader), 1);
  124.     if (oHeader == NULL) {
  125.         printf("ERROR: Unable to create memory for entry's header.\n");
  126.         return(1);
  127.     }
  128.  
  129.     fseek(fp, (LONG) oEntry->HeaderLocation, SEEK_SET);
  130.     if (fread(oHeader, sizeof(XHeader), 1, fp) != 1)  {
  131.         printf("ERROR: can't read table header.\n" );
  132.         return(1);
  133.     }
  134.  
  135.     if ((oHeader->Country[0] == 'S') && (oHeader->Country[1] == 'U')) {
  136.         lTablePosition = ftell(fp);
  137.  
  138.         oOrgKeys = calloc(oHeader->XtableLen, 1);
  139.         oNewKeys = calloc(oHeader->XtableLen, 1);
  140.  
  141.         if ((oOrgKeys == NULL) || (oNewKeys == NULL)) {
  142.             printf("ERROR: Unable to create memory for entry's keys.\n");
  143.             return(1);
  144.         }
  145.  
  146.         if (fread(oOrgKeys, oHeader->XtableLen, 1, fp) != 1) {
  147.             printf("ERROR: Can't read key table.\n" );
  148.             return(1);
  149.         }
  150.         memcpy(oNewKeys, oOrgKeys, oHeader->XtableLen);
  151.  
  152.         sRtn = Assignments((KeyDef *) oOrgKeys, (KeyDef *) oNewKeys, oHeader->EntryCount);
  153.  
  154.         fseek(fp, lTablePosition, SEEK_SET);
  155.         if (fwrite(oNewKeys, oHeader->XtableLen, 1, fp) != 1) {
  156.             printf("ERROR: Can't write key table.\n" );
  157.             return(1);
  158.         }
  159.  
  160.         free(oHeader);
  161.         free(oOrgKeys);
  162.         free(oNewKeys);
  163.  
  164.         return(sRtn);
  165.     } else {
  166.         return(0);
  167.     }
  168. }
  169.  
  170. static SHORT Assignments(KeyDef *oOrgKeys, KeyDef *oNewKeys, INT iEntryCnt)
  171. {
  172.     SHORT sNumberOfAssignments;
  173.     SHORT sKeyAssignIndex;
  174.     SHORT sKeyOrg;
  175.     SHORT sKeyNew;
  176.     SHORT    sRtn;
  177.  
  178.     sRtn = 0;
  179.     sNumberOfAssignments = (sizeof(sAssignKey) / sizeof(SHORT)) >> 1;
  180.  
  181.     for (sKeyAssignIndex = 0; sKeyAssignIndex < sNumberOfAssignments; sKeyAssignIndex++) {
  182.         sKeyNew = sAssignKey[sKeyAssignIndex][0] - 1;
  183.         sKeyOrg = sAssignKey[sKeyAssignIndex][1] - 1;
  184.  
  185.         if ((sKeyOrg < iEntryCnt) && (sKeyNew < iEntryCnt) && (sKeyOrg > 0) && (sKeyNew > 0)) {
  186.             oNewKeys[sKeyNew] = oOrgKeys[sKeyOrg];
  187.         }
  188.     }
  189.     
  190.     return(sRtn);
  191. }
  192.  
  193.