00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "keymap.h"
00011 #ifdef __APPLE__
00012 #include <sys/param.h>
00013 #endif
00014
00015 #include "drms_types.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 const char *KeyMapClassTables[] =
00026 {
00027 "",
00028 "",
00029 "",
00030 "BUNIT\tbunit_ssw\n" \
00031 "BMAJ Bmajor_ssw\n" \
00032 "noexist,intKey3_ssw\n",
00033 "LONPOLE\tlongitude_pole_gng\n" \
00034 "BUNIT\tbunit_gng\n" \
00035 "EQUINOX, intKey_gng\n",
00036 ""
00037 };
00038
00039
00040 const char *KeyMapClassIDMap[] =
00041 {
00042 "",
00043 "dsds",
00044 "local",
00045 "ssw",
00046 "gng",
00047 ""
00048 };
00049
00050 #define MAXCLKEY 128
00051 #define MAXMAPPINGKEY 64
00052
00053
00054 static HContainer_t *gClassTables = NULL;
00055
00056
00057 static void KMFree(const void *val);
00058 static int exputl_keymap_initcltables();
00059 static void exputl_keymap_termcltables();
00060
00061
00062
00063
00064
00065 static void KMFree(const void *val)
00066 {
00067 Exputl_KeyMap_t *km = (Exputl_KeyMap_t *)val;
00068
00069 if (km)
00070 {
00071 hcon_free(&(km->int2ext));
00072 hcon_free(&(km->ext2int));
00073 }
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 static int exputl_keymap_initcltables()
00086 {
00087 int ret = 0;
00088
00089 if (!gClassTables)
00090 {
00091 gClassTables = hcon_create(sizeof(Exputl_KeyMap_t), MAXCLKEY, KMFree, NULL, NULL, NULL, 0);
00092 }
00093
00094 if (gClassTables)
00095 {
00096 int ok = 1;
00097 int i = kKEYMAPCLASS_DEFAULT + 1;
00098 for (; ok && i < kKEYMAPCLASS_NUMTABLESPLUSONE; i++)
00099 {
00100 Exputl_KeyMap_t *km = exputl_keymap_create();
00101 if (km)
00102 {
00103
00104 ok = exputl_keymap_parsetable(km, KeyMapClassTables[i]);
00105
00106 if (ok)
00107 {
00108 ok = !hcon_insert(gClassTables, KeyMapClassIDMap[i], km);
00109 free(km);
00110 }
00111 else
00112 {
00113
00114 exputl_keymap_destroy(&km);
00115 }
00116 }
00117 else
00118 {
00119 ok = 0;
00120 }
00121 }
00122
00123 ret = ok;
00124 }
00125
00126 if (!ret && gClassTables)
00127 {
00128 hcon_destroy(&gClassTables);
00129 }
00130
00131 return ret;
00132 }
00133
00134
00135
00136
00137 static void exputl_keymap_termcltables()
00138 {
00139 if (gClassTables)
00140 {
00141 hcon_destroy(&gClassTables);
00142 }
00143 }
00144
00145
00146
00147
00148 Exputl_KeyMap_t *exputl_keymap_create()
00149 {
00150 Exputl_KeyMap_t *ret = NULL;
00151
00152 Exputl_KeyMap_t *km = (Exputl_KeyMap_t *)malloc(sizeof(Exputl_KeyMap_t));
00153 if (km)
00154 {
00155 hcon_init(&(km->int2ext), MAXMAPPINGKEY, DRMS_MAXKEYNAMELEN, NULL, NULL);
00156 hcon_init(&(km->ext2int), DRMS_MAXKEYNAMELEN, MAXMAPPINGKEY, NULL, NULL);
00157 ret = km;
00158 }
00159
00160 return ret;
00161 }
00162
00163
00164
00165
00166 void exputl_keymap_destroy(Exputl_KeyMap_t **km)
00167 {
00168 if (*km)
00169 {
00170 hcon_free(&((*km)->int2ext));
00171 hcon_free(&((*km)->ext2int));
00172 free(*km);
00173 *km = NULL;
00174 }
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184 int exputl_keymap_parsetable(Exputl_KeyMap_t *keymap, const char *text)
00185 {
00186 int success = 1;
00187
00188 if (keymap)
00189 {
00190
00191 char token[MAX(MAXMAPPINGKEY, DRMS_MAXKEYNAMELEN)];
00192 char *pCh = NULL;
00193 char *fits = NULL;
00194 char *drms = NULL;
00195 char *textC = strdup(text);
00196 char *lasts = NULL;
00197
00198 pCh = strtok_r(textC, "\n", &lasts);
00199 for (; success && pCh != NULL; pCh = strtok_r(NULL, "\n", &lasts))
00200 {
00201 if (strlen(pCh) == 0)
00202 {
00203
00204 continue;
00205 }
00206
00207 if (pCh[0] == '#')
00208 {
00209
00210 continue;
00211 }
00212
00213 snprintf(token, sizeof(token), "%s", pCh);
00214 pCh = strtok(token, " \t,");
00215 if (pCh)
00216 {
00217 fits = pCh;
00218 pCh = strtok(NULL, " \t,");
00219 if (pCh)
00220 {
00221 drms = pCh;
00222 }
00223 else
00224 {
00225 fprintf(stderr, "Skipping bad line in map file:\n\t%s", textC);
00226 continue;
00227 }
00228 }
00229 else
00230 {
00231
00232 continue;
00233 }
00234
00235 if (fits && drms)
00236 {
00237 char drmsKeyName[DRMS_MAXKEYNAMELEN];
00238 snprintf(drmsKeyName, sizeof(drmsKeyName), "%s", drms);
00239 hcon_insert(&(keymap->ext2int), fits, drmsKeyName);
00240 hcon_insert(&(keymap->int2ext), drmsKeyName, fits);
00241 }
00242 }
00243
00244 if (textC)
00245 {
00246 free(textC);
00247 }
00248 }
00249 else
00250 {
00251 success = 0;
00252 }
00253
00254 return success;
00255 }
00256
00257
00258
00259
00260
00261
00262 int exputl_keymap_parsefile(Exputl_KeyMap_t *keymap, FILE *fPtr)
00263 {
00264 int success = 1;
00265
00266 char buf[8192];
00267 char lineBuf[LINE_MAX];
00268 long nRead = 0;
00269 int done = 0;
00270
00271
00272 while (!done && success)
00273 {
00274 while (success && !(done = (fgets(lineBuf, LINE_MAX, fPtr) == NULL)))
00275 {
00276 if (strlen(lineBuf) + nRead < sizeof(buf))
00277 {
00278 nRead += strlen(lineBuf);
00279 strcat(buf, lineBuf);
00280 }
00281 else
00282 {
00283 break;
00284 }
00285 }
00286
00287
00288 success = exputl_keymap_parsetable(keymap, buf);
00289 buf[0] = '\0';
00290 nRead = 0;
00291 }
00292
00293 return success;
00294 }
00295
00296
00297
00298
00299 int exputl_keymap_init()
00300 {
00301 BASE_Cleanup_t cu;
00302 cu.item = NULL;
00303 cu.free = exputl_keymap_term;
00304 base_cleanup_register("keymapclasstables", &cu);
00305
00306 return exputl_keymap_initcltables();
00307 }
00308
00309
00310
00311
00312 void exputl_keymap_term(void *data)
00313 {
00314 exputl_keymap_termcltables();
00315 }
00316
00317 const char *exputl_keymap_extname(Exputl_KeyMap_t *keymap, const char *intName)
00318 {
00319 return (const char*)hcon_lookup(&(keymap->int2ext), intName);
00320 }
00321
00322 const char *exputl_keymap_classidextname(Exputl_KeyMapClass_t classid, const char *intName)
00323 {
00324 const char *ret = NULL;
00325
00326 if (gClassTables || exputl_keymap_init())
00327 {
00328
00329 const char *strid = KeyMapClassIDMap[classid];
00330
00331
00332 Exputl_KeyMap_t *km = hcon_lookup(gClassTables, strid);
00333 if (km)
00334 {
00335 ret = (const char *)hcon_lookup(&(km->int2ext), intName);
00336 }
00337 }
00338
00339 return ret;
00340 }
00341
00342 const char *exputl_keymap_classextname(const char *class, const char *intName)
00343 {
00344 const char *ret = NULL;
00345
00346 if (gClassTables || exputl_keymap_init())
00347 {
00348
00349 Exputl_KeyMap_t *km = hcon_lookup(gClassTables, class);
00350 if (km)
00351 {
00352 ret = (const char *)hcon_lookup(&(km->int2ext), intName);
00353 }
00354 }
00355
00356 return ret;
00357 }
00358
00359 const char *exputl_keymap_intname(Exputl_KeyMap_t *keymap, const char *extName)
00360 {
00361 return (const char*)hcon_lookup(&(keymap->ext2int), extName);
00362 }
00363
00364 const char *exputl_keymap_classidintname(Exputl_KeyMapClass_t classid, const char *extName)
00365 {
00366 const char *ret = NULL;
00367
00368 if (gClassTables || exputl_keymap_init())
00369 {
00370
00371 const char *strid = KeyMapClassIDMap[classid];
00372
00373
00374 Exputl_KeyMap_t *km = hcon_lookup(gClassTables, strid);
00375 if (km)
00376 {
00377 ret = (const char *)hcon_lookup(&(km->ext2int), extName);
00378 }
00379 }
00380
00381 return ret;
00382 }
00383
00384 const char *exputl_keymap_classintname(const char *class, const char *extName)
00385 {
00386 const char *ret = NULL;
00387
00388 if (gClassTables || exputl_keymap_init())
00389 {
00390
00391 Exputl_KeyMap_t *km = hcon_lookup(gClassTables, class);
00392 if (km)
00393 {
00394 ret = (const char *)hcon_lookup(&(km->ext2int), extName);
00395 }
00396 }
00397
00398 return ret;
00399 }
00400
00401 const char *exputl_keymap_getclname(Exputl_KeyMapClass_t clid)
00402 {
00403 return KeyMapClassIDMap[clid];
00404 }