Back to index

im-sdk  12.3.91
converter_win.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 <ctype.h>
00044 #include <stdio.h>
00045 #include <string.h>
00046 #include <stdlib.h>
00047 
00048 enum {
00049        OK = 0,
00050        ERROR = -1,
00051        ERROR_ZHICODE = -2,
00052        ERROR_ZHUYINCODE = -3,
00053 };
00054 
00055 /* define struct for file with text format */
00056 typedef struct {
00057        unsigned char *Yin;
00058        unsigned char *ZhiList;
00059 } TTextItemStruct, *PTextItemStruct;
00060 
00061 typedef struct {
00062        long num_items;
00063        PTextItemStruct *items;
00064 } TTextItemListStruct;
00065 
00066 #define LEN_BUFFER_ALLOC  5000
00067 
00068 char *skip_space(char *s)
00069 {
00070        while (*s && (*s==' ' || *s=='\t')) s++;
00071        return s;
00072 }
00073 
00074 char *to_space(char *s)
00075 {
00076        while (*s && !(isspace(*s) || *s == '\n')) s++;
00077        return s;
00078 }
00079 
00080 char *trim_string(char *s)
00081 {
00082        char *head_ptr, *tail_ptr;
00083 
00084        if (!s || !*s) return(s);
00085 
00086        head_ptr = s;
00087 
00088        /* trim left space */
00089        while(*head_ptr && (*head_ptr == ' ' || *head_ptr == '\t' || *head_ptr == '\n'))
00090               head_ptr ++;
00091 
00092        if (!*head_ptr) return(head_ptr);
00093 
00094        /* trim right space */
00095        tail_ptr = head_ptr + strlen(head_ptr) - 1;
00096        
00097        while(*tail_ptr && (*tail_ptr == ' ' || *tail_ptr == '\t' || *tail_ptr == '\n')) {
00098               *tail_ptr = '\0';
00099               tail_ptr --;
00100        }
00101 
00102        return(head_ptr);
00103 }
00104 
00105 /*
00106 parse the line into gbk_code, Zhi and frequence:
00107 
00108 return OK if success;
00109 return ERROR_ZHICODE  if gbk_code error;
00110 return ERROR_ZHUYINCODE if Zhi error;
00111 */
00112 
00113 int parse_line_for_zhuyin(char *line_buf, TTextItemStruct *item)
00114 {
00115        char key_sequence[5];
00116        char *ptr, *yin_ptr, *zhi_ptr;
00117        int  yin_len;
00118 
00119        ptr = trim_string(line_buf);
00120 
00121        if (*ptr == '\n' || *ptr == '#') 
00122               return(ERROR);
00123 
00124        yin_ptr = ptr;
00125        ptr = to_space(ptr);
00126        *ptr = '\0';
00127        
00128        ZhuyinSymbolSequenceToStandardKeySequence(yin_ptr, key_sequence, &yin_len);
00129 
00130        if (yin_len == 0) return(ERROR_ZHUYINCODE);
00131 
00132        if (item->Yin != NULL) free((char *)item->Yin);
00133        item->Yin = (unsigned char *)strdup(key_sequence);
00134 
00135        zhi_ptr = skip_space(ptr+1);
00136        ptr = to_space(ptr);
00137        *ptr = '\0';
00138 
00139        if (*zhi_ptr == 0) return(ERROR_ZHICODE);
00140 
00141        if (item->ZhiList != NULL) free((char *)item->ZhiList);
00142        item->ZhiList = (unsigned char *)strdup(zhi_ptr);
00143 
00144        return(OK);
00145 }
00146 
00147 int TextItemList_Alloc(TTextItemListStruct *ItemList, int num_alloced)
00148 {
00149        int i;
00150 
00151        ItemList->num_items = 0;
00152        ItemList->items = malloc(num_alloced * sizeof(PTextItemStruct*));
00153 
00154        if (ItemList->items == NULL) {
00155               fprintf(stderr, "Error: TextItemList malloc\n");
00156               return(ERROR);
00157        }
00158 
00159        for (i=0; i<num_alloced; i++)
00160               ItemList->items[i] = NULL;
00161 
00162        ItemList->num_items = num_alloced;
00163        
00164        return(OK);
00165 }
00166 
00167 int TextItemList_ReAlloc(TTextItemListStruct *ItemList, int num_alloced)
00168 {
00169        int i;
00170 
00171        ItemList->items = realloc((void *)ItemList->items, num_alloced * sizeof(PTextItemStruct*));
00172 
00173        if (ItemList->items == NULL) {
00174               fprintf(stderr, "Error: TextItemList realloc\n");
00175               ItemList->num_items = 0;
00176               return(ERROR);
00177        }
00178 
00179        for (i=ItemList->num_items; i<num_alloced; i++)
00180               ItemList->items[i] = NULL;
00181 
00182        ItemList->num_items = num_alloced;
00183        
00184        return(OK);
00185 }
00186 
00187 int TextItemList_Init(TTextItemListStruct *ItemList)
00188 {
00189        ItemList->num_items = 0;
00190        ItemList->items = NULL;
00191 }
00192 
00193 int TextItemList_Free(TTextItemListStruct *ItemList)
00194 {
00195        int i, j;
00196 
00197        for (i=0; i<ItemList->num_items; i++) {
00198               if (ItemList->items[i]->Yin != NULL) 
00199                      free((char *)ItemList->items[i]->Yin);
00200 
00201               if (ItemList->items[i]->ZhiList != NULL) 
00202                      free((char *)ItemList->items[i]->ZhiList);
00203 
00204               if (ItemList->items[i] != NULL)
00205                      free((char *)ItemList->items[i]);
00206        }
00207 
00208        if (ItemList->items != NULL)
00209               free((char *)ItemList->items);
00210 
00211        ItemList->num_items = 0;
00212 
00213        return(OK);
00214 }
00215 
00216 int TextItem_Compare_By_YinCode(const void *p1, const void *p2)
00217 {
00218        TTextItemStruct *item1 = *((TTextItemStruct **)p1);
00219        TTextItemStruct *item2 = *((TTextItemStruct **)p2);
00220        int ret;
00221 
00222        ret = strcmp(item1->Yin, item2->Yin);
00223 
00224        return(ret);
00225 }
00226 
00227 int TextItemList_Sort(TTextItemListStruct *ItemList, int(*compare)(const void *, const void *))
00228 {
00229        qsort((void *)ItemList->items, ItemList->num_items, sizeof(PTextItemStruct), compare);
00230 }
00231 
00232 int TextItemList_Print_For_Single(TTextItemListStruct *ItemList)
00233 {
00234        int i, j;
00235        unsigned char *Yin;
00236        unsigned char *ZhiList;
00237        long frequence;
00238 
00239        for (i=0; i<ItemList->num_items; i++) {
00240               if (ItemList->items[i] == NULL) {
00241                      continue;
00242               }
00243 
00244               Yin = ItemList->items[i]->Yin;
00245               if (!Yin || !*Yin) break;
00246 
00247               ZhiList = ItemList->items[i]->ZhiList;
00248               if (!ZhiList || !*ZhiList) break;
00249 
00250               printf("%s\t%s\n", (char *)Yin,  ZhiList);
00251        }
00252 }
00253 
00254 int Read_TextItemList_From_File(char *file_name, TTextItemListStruct *ItemList, int(*parse_line)(char *, TTextItemStruct *))
00255 {
00256        FILE *ifile;
00257        char line_buf[256];
00258 
00259        int num_malloc_items = LEN_BUFFER_ALLOC;
00260        int num_items = 0;
00261        int line_no = 0;
00262        int ret;
00263 
00264        ifile = fopen(file_name, "r");
00265        if (ifile == NULL) {
00266               fprintf(stderr, "Error: open file %s\n", file_name);
00267               return(ERROR);
00268        }
00269 
00270        TextItemList_Alloc(ItemList, num_malloc_items);
00271 
00272        while(fgets(line_buf, 256, ifile) != NULL) {
00273 
00274               line_no++;
00275 
00276               /* realloc buffer for items */
00277               if (num_items >= num_malloc_items) {
00278                      num_malloc_items += LEN_BUFFER_ALLOC;
00279                      ret = TextItemList_ReAlloc(ItemList, num_malloc_items);
00280                      if (ret == ERROR) 
00281                             break;
00282               }
00283               
00284               if (ItemList->items[num_items] == NULL) {
00285                      ItemList->items[num_items] = (TTextItemStruct *)calloc(1, sizeof(TTextItemStruct));
00286                      if (ItemList->items[num_items] == NULL) {
00287                             fprintf(stderr, "Error:  No Memory for TextItemList\n");
00288                             break;
00289                      }
00290               }
00291 
00292               ret = parse_line(line_buf, ItemList->items[num_items]);
00293               if (ret != 0) {
00294                      if (ret == ERROR_ZHICODE) {
00295                             fprintf(stderr, "Error: hz_code error in line %d\n", line_no);
00296                      } else if (ret == ERROR_ZHUYINCODE) {
00297                             fprintf(stderr, "Error: Zhi error in line %d\n", line_no);
00298                      }
00299                      continue;
00300               } 
00301 
00302               num_items++;
00303        }
00304 
00305        TextItemList_ReAlloc(ItemList, num_items);
00306 
00307        fclose(ifile);
00308        return(OK);
00309 }
00310 
00311 int main(int argc, char **argv)
00312 {
00313        int ret = OK;
00314        char *zhuyin_file;
00315 
00316        TTextItemListStruct Zhuyin_ItemList;
00317 
00318        if (argc != 2) {
00319               printf("Usage:  %s zhuyin_file\n", argv[0]);
00320               exit(-1);
00321        }
00322 
00323        zhuyin_file = argv[1];
00324 
00325        ret = Read_TextItemList_From_File(zhuyin_file, 
00326                                      &Zhuyin_ItemList, 
00327                                      parse_line_for_zhuyin);
00328        if (ret == ERROR) {
00329               TextItemList_Free(&Zhuyin_ItemList);
00330               exit(-1);
00331        }
00332 
00333 /*
00334        TextItemList_Sort(&Zhuyin_ItemList, TextItem_Compare_By_ZhuyinCode);
00335 */
00336        TextItemList_Print_For_Single(&Zhuyin_ItemList);
00337        TextItemList_Free(&Zhuyin_ItemList);
00338 
00339        return(OK);
00340 }