home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / keycoll.cpp < prev    next >
C/C++ Source or Header  |  2000-12-13  |  2KB  |  103 lines

  1. /*
  2. ** Module   :KEYCOLL.CPP
  3. ** Abstract :Keyboard definition collection
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Fri  27/03/1998     Created
  8. **
  9. */
  10.  
  11. #include <string.h>
  12.  
  13. #include <boxcoll.h>
  14. #include <version.h>
  15.  
  16. int KeyDefCollection::Compare(Ptr p1, Ptr p2)
  17. {
  18.     return __cstrcmp((char*)p1, (char*)p2);
  19. }
  20.  
  21. void KeyDefCollection::InsKey(char *buf, int namelen)
  22. {
  23.     char *str;
  24.  
  25.     if(!buf || namelen >= KEY_NAME_LEN)
  26.         return;
  27.  
  28.     str = buf+namelen;
  29.  
  30.     while(__issp(*str))
  31.         str++;
  32.  
  33.     if(!*str)
  34.         return;
  35.  
  36.     if(*str != ':' && *str != '=')
  37.         return;
  38.  
  39.     str++;
  40.  
  41.     int nlen = compile_keydef(str, 0);
  42.  
  43.     if(nlen <= 0)
  44.         return;
  45.  
  46.     struct keydef_pair* def = (struct keydef_pair*)
  47.                               new char[sizeof(keydef_pair)+nlen];
  48.     def->prog[nlen] = 0;
  49.  
  50.     compile_keydef(str, def->prog);
  51.  
  52.     memcpy(def->key, buf, namelen);
  53.     def->key[namelen] = 0;
  54.  
  55.     //printf("Compiled definition for %s\n", def->key);
  56.  
  57.     unsigned long index = Look(def->key);
  58.  
  59.     if(index < Count() && !Compare(def->key, Get(index)))
  60.     {
  61.         //Replace existing keydef
  62.         Free(Remove(index));
  63.         At(def, index);
  64.     }
  65.     else
  66.         Add(def);
  67. }
  68.  
  69. char *KeyDefCollection::GetDef(char *key)
  70. {
  71.     unsigned long index = Look(key);
  72.  
  73.     if(index < Count() && !Compare(key, Get(index)))
  74.         return ((struct keydef_pair*)Get(index))->prog;
  75.     return 0;
  76. }
  77.  
  78. void KeyDefCollection::AddAssignment(char *keyname, char *prog)
  79. {
  80.     if(!keyname || !prog)
  81.         return;
  82.  
  83.     int nlen = strlen(prog);
  84.  
  85.     struct keydef_pair* def = (struct keydef_pair*)
  86.                               new char[sizeof(keydef_pair)+nlen];
  87.  
  88.     strncpy(def->key, keyname, KEY_NAME_LEN);
  89.     memcpy(def->prog, prog, nlen);
  90.     def->prog[nlen] = 0;
  91.  
  92.     unsigned long index = Look(def->key);
  93.  
  94.     if(index < Count() && !Compare(def->key, Get(index)))
  95.     {
  96.         Free(Remove(index));
  97.         At(def, index);
  98.     }
  99.     else
  100.         Add(def);
  101. }
  102.  
  103.