Back to index

im-sdk  12.3.91
zhuyin_keymap.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #include <stdio.h>
00044 #include "zhuyin_standard.h"
00045 #include "zhuyin_keymap.h"
00046 
00047 int ZhuyinKeymapList_Init(TZhuyinKeymapList *pZhuyinKeymapList)
00048 {
00049        pZhuyinKeymapList->nNum_Keymaps = 0;
00050        pZhuyinKeymapList->nNum_Keymaps_Alloced = 0;
00051        pZhuyinKeymapList->pKeymaps = NULL;
00052 
00053        return(ZHUYIN_KEYMAP_OK);
00054 }
00055 
00056 int ZhuyinKeymapList_Free(TZhuyinKeymapList *pZhuyinKeymapList)
00057 {
00058        int i;
00059 
00060        if (pZhuyinKeymapList->pKeymaps == NULL) {
00061               return(0);
00062        }
00063 
00064        for(i=0; i<pZhuyinKeymapList->nNum_Keymaps_Alloced; i++) {
00065               if (pZhuyinKeymapList->pKeymaps[i]== NULL)
00066                      continue;
00067 
00068               if (pZhuyinKeymapList->pKeymaps[i]->pName) {
00069                      free((char *)pZhuyinKeymapList->pKeymaps[i]->pName);
00070               }
00071 
00072               if (pZhuyinKeymapList->pKeymaps[i]->pKeymap) {
00073                      free((char *)pZhuyinKeymapList->pKeymaps[i]->pKeymap);
00074               }
00075 
00076               free((char *)pZhuyinKeymapList->pKeymaps[i]);
00077        }
00078 
00079        free((char *)pZhuyinKeymapList->pKeymaps);
00080 
00081        pZhuyinKeymapList->nNum_Keymaps = 0;
00082        pZhuyinKeymapList->nNum_Keymaps_Alloced = 0;
00083        pZhuyinKeymapList->pKeymaps = NULL;
00084 
00085        return(ZHUYIN_KEYMAP_OK);
00086 }
00087 
00088 int ZhuyinKeymapList_Alloc(TZhuyinKeymapList *pZhuyinKeymapList, int nNum_Alloced)
00089 {
00090        int i;
00091 
00092        pZhuyinKeymapList->nNum_Keymaps_Alloced = 0;
00093        pZhuyinKeymapList->pKeymaps = (PZhuyinKeymap*)malloc(nNum_Alloced*sizeof(PZhuyinKeymap));
00094        if (pZhuyinKeymapList->pKeymaps == NULL) {
00095               return(ZHUYIN_KEYMAP_ERROR);
00096        }
00097 
00098        for (i=0; i<nNum_Alloced; i++)
00099               pZhuyinKeymapList->pKeymaps[i] = NULL;
00100 
00101        pZhuyinKeymapList->nNum_Keymaps_Alloced = nNum_Alloced;
00102 
00103        return(ZHUYIN_KEYMAP_OK);
00104 }
00105 
00106 int ZhuyinKeymapList_ReAlloc(TZhuyinKeymapList *pZhuyinKeymapList, int nNum_ReAlloced)
00107 {
00108        int i;
00109 
00110        pZhuyinKeymapList->pKeymaps = (PZhuyinKeymap*)realloc(pZhuyinKeymapList->pKeymaps,
00111                                    nNum_ReAlloced * sizeof(PZhuyinKeymap));
00112        if (pZhuyinKeymapList->pKeymaps == NULL) {
00113               pZhuyinKeymapList->nNum_Keymaps = 0;
00114               pZhuyinKeymapList->nNum_Keymaps_Alloced = 0;
00115               return(ZHUYIN_KEYMAP_ERROR);
00116        }
00117 
00118        for (i=pZhuyinKeymapList->nNum_Keymaps_Alloced; i<nNum_ReAlloced; i++)
00119               pZhuyinKeymapList->pKeymaps[i] = NULL;
00120 
00121        pZhuyinKeymapList->nNum_Keymaps_Alloced = nNum_ReAlloced;
00122 
00123        return(ZHUYIN_KEYMAP_OK);
00124 }
00125 
00126 int ZhuyinKeymapList_Print(TZhuyinKeymapList *pZhuyinKeymapList)
00127 {
00128        int nKeymap_ID, idx, key;
00129 
00130        printf("nNum_Keymaps: %d\n", pZhuyinKeymapList->nNum_Keymaps);
00131        for(nKeymap_ID = 0; nKeymap_ID < pZhuyinKeymapList->nNum_Keymaps; nKeymap_ID ++) {
00132               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00133                      return(ZHUYIN_KEYMAP_ERROR);
00134               }
00135 
00136               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName == NULL) {
00137                      return(ZHUYIN_KEYMAP_ERROR);
00138               }
00139 
00140               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00141                      return(ZHUYIN_KEYMAP_ERROR);
00142               }
00143 
00144               printf("Name: %s\n", pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName);
00145 
00146               printf("Keymap: \n");
00147               for (idx = 1; idx <= NUM_OF_ZHUYIN_SYMBOL; idx++) {
00148                      key = pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx];
00149                      if (key != 0) {
00150                             printf("%s:  %c  0x%x\n", (char *)ZhuyinIndexToZhuyinSymbol(idx), key, key);
00151                      }
00152               }
00153        }
00154 
00155        return(ZHUYIN_KEYMAP_OK);
00156 }
00157 
00158 int ZhuyinKeymapList_Item_Prepare(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID)
00159 {
00160        int ret;
00161 
00162        if (nKeymap_ID < 0 || nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps_Alloced + NUM_KEYMAPS_ALLOC)
00163               return(ZHUYIN_KEYMAP_ERROR);
00164 
00165        if (pZhuyinKeymapList->nNum_Keymaps_Alloced == 0) {
00166               ret = ZhuyinKeymapList_Alloc(pZhuyinKeymapList, NUM_KEYMAPS_ALLOC);
00167               if (ret == ZHUYIN_KEYMAP_ERROR)
00168                      return(ZHUYIN_KEYMAP_ERROR);
00169        }
00170 
00171        if (nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps_Alloced) {
00172               int nNum_Alloc = pZhuyinKeymapList->nNum_Keymaps_Alloced + NUM_KEYMAPS_ALLOC;
00173               ret = ZhuyinKeymapList_ReAlloc(pZhuyinKeymapList, nNum_Alloc);
00174               if (ret == ZHUYIN_KEYMAP_ERROR)
00175                      return(ZHUYIN_KEYMAP_ERROR);
00176        }
00177 
00178        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00179               pZhuyinKeymapList->pKeymaps[nKeymap_ID] = (TZhuyinKeymap *)calloc(1, sizeof(TZhuyinKeymap));
00180               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL)
00181                      return(ZHUYIN_KEYMAP_ERROR);
00182        }
00183 
00184        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00185               free((char *)pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName);
00186               pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName = NULL;
00187        }
00188 
00189        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap != NULL) {
00190               free((char *)pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap);
00191               pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap = NULL;
00192        }
00193               
00194        return(ZHUYIN_KEYMAP_OK);
00195 }
00196 
00197 int ZhuyinKeymapList_Item_Set_Name(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, unsigned char *pName)
00198 {
00199        if (!pName || !*pName) 
00200               return(ZHUYIN_KEYMAP_ERROR);
00201 
00202        if (nKeymap_ID < 0 || nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps_Alloced)
00203               return(ZHUYIN_KEYMAP_ERROR);
00204 
00205        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00206               return(ZHUYIN_KEYMAP_ERROR);
00207        }
00208 
00209        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00210               free((char *)pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName);
00211        }
00212 
00213        pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName = (unsigned char *)strdup(pName);
00214        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName == NULL)
00215               return(ZHUYIN_KEYMAP_ERROR);
00216 
00217        return(ZHUYIN_KEYMAP_OK);
00218 }
00219 
00220 int ZhuyinKeymapList_Item_Set_KeymapValue(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, unsigned char *pZhuyinSymbol, char key)
00221 {
00222        int idx;
00223 
00224        if (!pZhuyinSymbol || !*pZhuyinSymbol)
00225               return(ZHUYIN_KEYMAP_ERROR);
00226 
00227        if (nKeymap_ID < 0 || nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps_Alloced)
00228               return(ZHUYIN_KEYMAP_ERROR);
00229 
00230        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00231               return(ZHUYIN_KEYMAP_ERROR);
00232        }
00233 
00234        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00235               pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap = (char *)calloc(NUM_OF_ZHUYIN_SYMBOL + 2, sizeof(char));
00236               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL)
00237                      return(ZHUYIN_KEYMAP_ERROR);
00238        }
00239               
00240        idx = ZhuyinSymbolToZhuyinIndex(pZhuyinSymbol);
00241        if (idx == 0) return(ZHUYIN_KEYMAP_OK);
00242 
00243        pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx] = tolower(key);
00244        return(ZHUYIN_KEYMAP_OK);
00245 }
00246 
00247 int ZhuyinKeymapList_Item_Add_StandardKeymap(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID)
00248 {
00249        int idx;
00250        char key;
00251 
00252        if (nKeymap_ID < 0 || nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps_Alloced)
00253               return(ZHUYIN_KEYMAP_ERROR);
00254 
00255        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00256               return(ZHUYIN_KEYMAP_ERROR);
00257        }
00258 
00259        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00260               free((char *)pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName);
00261        }
00262 
00263        pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName = (unsigned char *)strdup(NAME_STANDARD_ZHUYIN_KEYMAP);
00264        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName == NULL)
00265               return(ZHUYIN_KEYMAP_ERROR);
00266 
00267        if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00268               pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap = (char *)calloc(NUM_OF_ZHUYIN_SYMBOL + 2, sizeof(char));
00269               if (pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL)
00270                      return(ZHUYIN_KEYMAP_ERROR);
00271        }
00272               
00273        for (idx = 1; idx <= NUM_OF_ZHUYIN_SYMBOL; idx++) {
00274               pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx] = ZhuyinIndexToStandardKey(idx);
00275        }
00276        return(ZHUYIN_KEYMAP_OK);
00277 }
00278 
00279 int ZhuyinKeymapList_Item_KeyToIndex(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, int key)
00280 {
00281        int idx;
00282        char *pKeymap;
00283 
00284        if (nKeymap_ID < 0 || nKeymap_ID >= pZhuyinKeymapList->nNum_Keymaps)
00285               return(0);
00286 
00287        pKeymap = pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pKeymap;
00288        if (pKeymap == NULL)
00289               return(0);
00290 
00291        key = tolower(key);
00292        for (idx = 1; idx <= NUM_OF_ZHUYIN_SYMBOL; idx++) {
00293               if (key == pKeymap[idx]) {
00294                      return(idx);
00295               }
00296        }
00297        return(0);
00298 }
00299 
00300 unsigned char *ZhuyinKeymapList_Item_KeyToSymbol(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, int key)
00301 {
00302        int idx = ZhuyinKeymapList_Item_KeyToIndex(pZhuyinKeymapList, nKeymap_ID, key);
00303        return((unsigned char *)ZhuyinIndexToZhuyinSymbol(idx));
00304 }
00305 
00306 int ZhuyinKeymapList_Item_KeyToType(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, int key)
00307 {
00308        int idx = ZhuyinKeymapList_Item_KeyToIndex(pZhuyinKeymapList, nKeymap_ID, key);
00309        return(ZhuyinIndexToZhuyinType(idx));
00310 }
00311 
00312 int ZhuyinKeymapList_Item_KeyToStandardKey(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, int key)
00313 {
00314        int idx = ZhuyinKeymapList_Item_KeyToIndex(pZhuyinKeymapList, nKeymap_ID, key);
00315        return(ZhuyinIndexToStandardKey(idx));
00316 }
00317 
00318 int ZhuyinKeymapList_Item_CheckIfValidKey(TZhuyinKeymapList *pZhuyinKeymapList, int nKeymap_ID, int key)
00319 {
00320        int idx = ZhuyinKeymapList_Item_KeyToIndex(pZhuyinKeymapList, nKeymap_ID, key);
00321        return(idx ? 1 : 0);
00322 }
00323