Back to index

im-sdk  12.3.91
Classes | Defines | Typedefs | Enumerations | Functions
converter_win.c File Reference
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  TTextItemStruct
struct  TTextItemListStruct

Defines

#define LEN_BUFFER_ALLOC   5000

Typedefs

typedef struct TTextItemStructPTextItemStruct

Enumerations

enum  { OK = 0, ERROR = -1, ERROR_ZHICODE = -2, ERROR_ZHUYINCODE = -3 }

Functions

char * skip_space (char *s)
char * to_space (char *s)
char * trim_string (char *s)
int parse_line_for_zhuyin (char *line_buf, TTextItemStruct *item)
int TextItemList_Alloc (TTextItemListStruct *ItemList, int num_alloced)
int TextItemList_ReAlloc (TTextItemListStruct *ItemList, int num_alloced)
int TextItemList_Init (TTextItemListStruct *ItemList)
int TextItemList_Free (TTextItemListStruct *ItemList)
int TextItem_Compare_By_YinCode (const void *p1, const void *p2)
int TextItemList_Sort (TTextItemListStruct *ItemList, int(*compare)(const void *, const void *))
int TextItemList_Print_For_Single (TTextItemListStruct *ItemList)
int Read_TextItemList_From_File (char *file_name, TTextItemListStruct *ItemList, int(*parse_line)(char *, TTextItemStruct *))
int main (int argc, char **argv)

Class Documentation

struct TTextItemStruct

Definition at line 56 of file converter.c.

Class Members
char cangjie_code
unsigned char * hz_str
unsigned char * Yin
unsigned char * Zhi
unsigned char * ZhiList
struct TTextItemListStruct

Definition at line 61 of file converter.c.

Collaboration diagram for TTextItemListStruct:
Class Members
PTextItemStruct * items
long num_items

Define Documentation

#define LEN_BUFFER_ALLOC   5000

Definition at line 66 of file converter_win.c.


Typedef Documentation

typedef struct TTextItemStruct * PTextItemStruct

Enumeration Type Documentation

anonymous enum
Enumerator:
OK 
ERROR 
ERROR_ZHICODE 
ERROR_ZHUYINCODE 

Definition at line 48 of file converter_win.c.

     {
       OK = 0,
       ERROR = -1,
       ERROR_ZHICODE = -2,
       ERROR_ZHUYINCODE = -3,
};

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 311 of file converter_win.c.

{
       int ret = OK;
       char *zhuyin_file;

       TTextItemListStruct Zhuyin_ItemList;

       if (argc != 2) {
              printf("Usage:  %s zhuyin_file\n", argv[0]);
              exit(-1);
       }

       zhuyin_file = argv[1];

       ret = Read_TextItemList_From_File(zhuyin_file, 
                                     &Zhuyin_ItemList, 
                                     parse_line_for_zhuyin);
       if (ret == ERROR) {
              TextItemList_Free(&Zhuyin_ItemList);
              exit(-1);
       }

/*
       TextItemList_Sort(&Zhuyin_ItemList, TextItem_Compare_By_ZhuyinCode);
*/
       TextItemList_Print_For_Single(&Zhuyin_ItemList);
       TextItemList_Free(&Zhuyin_ItemList);

       return(OK);
}

Here is the call graph for this function:

int parse_line_for_zhuyin ( char *  line_buf,
TTextItemStruct item 
)

Definition at line 113 of file converter_win.c.

{
       char key_sequence[5];
       char *ptr, *yin_ptr, *zhi_ptr;
       int  yin_len;

       ptr = trim_string(line_buf);

       if (*ptr == '\n' || *ptr == '#') 
              return(ERROR);

       yin_ptr = ptr;
       ptr = to_space(ptr);
       *ptr = '\0';
       
       ZhuyinSymbolSequenceToStandardKeySequence(yin_ptr, key_sequence, &yin_len);

       if (yin_len == 0) return(ERROR_ZHUYINCODE);

       if (item->Yin != NULL) free((char *)item->Yin);
       item->Yin = (unsigned char *)strdup(key_sequence);

       zhi_ptr = skip_space(ptr+1);
       ptr = to_space(ptr);
       *ptr = '\0';

       if (*zhi_ptr == 0) return(ERROR_ZHICODE);

       if (item->ZhiList != NULL) free((char *)item->ZhiList);
       item->ZhiList = (unsigned char *)strdup(zhi_ptr);

       return(OK);
}

Here is the call graph for this function:

int Read_TextItemList_From_File ( char *  file_name,
TTextItemListStruct ItemList,
int(*)(char *, TTextItemStruct *)  parse_line 
)

Definition at line 254 of file converter_win.c.

{
       FILE *ifile;
       char line_buf[256];

       int num_malloc_items = LEN_BUFFER_ALLOC;
       int num_items = 0;
       int line_no = 0;
       int ret;

       ifile = fopen(file_name, "r");
       if (ifile == NULL) {
              fprintf(stderr, "Error: open file %s\n", file_name);
              return(ERROR);
       }

       TextItemList_Alloc(ItemList, num_malloc_items);

       while(fgets(line_buf, 256, ifile) != NULL) {

              line_no++;

              /* realloc buffer for items */
              if (num_items >= num_malloc_items) {
                     num_malloc_items += LEN_BUFFER_ALLOC;
                     ret = TextItemList_ReAlloc(ItemList, num_malloc_items);
                     if (ret == ERROR) 
                            break;
              }
              
              if (ItemList->items[num_items] == NULL) {
                     ItemList->items[num_items] = (TTextItemStruct *)calloc(1, sizeof(TTextItemStruct));
                     if (ItemList->items[num_items] == NULL) {
                            fprintf(stderr, "Error:  No Memory for TextItemList\n");
                            break;
                     }
              }

              ret = parse_line(line_buf, ItemList->items[num_items]);
              if (ret != 0) {
                     if (ret == ERROR_ZHICODE) {
                            fprintf(stderr, "Error: hz_code error in line %d\n", line_no);
                     } else if (ret == ERROR_ZHUYINCODE) {
                            fprintf(stderr, "Error: Zhi error in line %d\n", line_no);
                     }
                     continue;
              } 

              num_items++;
       }

       TextItemList_ReAlloc(ItemList, num_items);

       fclose(ifile);
       return(OK);
}

Here is the call graph for this function:

char* skip_space ( char *  s)

Definition at line 68 of file converter_win.c.

{
       while (*s && (*s==' ' || *s=='\t')) s++;
       return s;
}
int TextItem_Compare_By_YinCode ( const void *  p1,
const void *  p2 
)

Definition at line 216 of file converter_win.c.

{
       TTextItemStruct *item1 = *((TTextItemStruct **)p1);
       TTextItemStruct *item2 = *((TTextItemStruct **)p2);
       int ret;

       ret = strcmp(item1->Yin, item2->Yin);

       return(ret);
}
int TextItemList_Alloc ( TTextItemListStruct ItemList,
int  num_alloced 
)

Definition at line 147 of file converter_win.c.

{
       int i;

       ItemList->num_items = 0;
       ItemList->items = malloc(num_alloced * sizeof(PTextItemStruct*));

       if (ItemList->items == NULL) {
              fprintf(stderr, "Error: TextItemList malloc\n");
              return(ERROR);
       }

       for (i=0; i<num_alloced; i++)
              ItemList->items[i] = NULL;

       ItemList->num_items = num_alloced;
       
       return(OK);
}

Definition at line 193 of file converter_win.c.

{
       int i, j;

       for (i=0; i<ItemList->num_items; i++) {
              if (ItemList->items[i]->Yin != NULL) 
                     free((char *)ItemList->items[i]->Yin);

              if (ItemList->items[i]->ZhiList != NULL) 
                     free((char *)ItemList->items[i]->ZhiList);

              if (ItemList->items[i] != NULL)
                     free((char *)ItemList->items[i]);
       }

       if (ItemList->items != NULL)
              free((char *)ItemList->items);

       ItemList->num_items = 0;

       return(OK);
}

Definition at line 187 of file converter_win.c.

{
       ItemList->num_items = 0;
       ItemList->items = NULL;
}

Definition at line 232 of file converter_win.c.

{
       int i, j;
       unsigned char *Yin;
       unsigned char *ZhiList;
       long frequence;

       for (i=0; i<ItemList->num_items; i++) {
              if (ItemList->items[i] == NULL) {
                     continue;
              }

              Yin = ItemList->items[i]->Yin;
              if (!Yin || !*Yin) break;

              ZhiList = ItemList->items[i]->ZhiList;
              if (!ZhiList || !*ZhiList) break;

              printf("%s\t%s\n", (char *)Yin,  ZhiList);
       }
}
int TextItemList_ReAlloc ( TTextItemListStruct ItemList,
int  num_alloced 
)

Definition at line 167 of file converter_win.c.

{
       int i;

       ItemList->items = realloc((void *)ItemList->items, num_alloced * sizeof(PTextItemStruct*));

       if (ItemList->items == NULL) {
              fprintf(stderr, "Error: TextItemList realloc\n");
              ItemList->num_items = 0;
              return(ERROR);
       }

       for (i=ItemList->num_items; i<num_alloced; i++)
              ItemList->items[i] = NULL;

       ItemList->num_items = num_alloced;
       
       return(OK);
}
int TextItemList_Sort ( TTextItemListStruct ItemList,
int(*)(const void *, const void *)  compare 
)

Definition at line 227 of file converter_win.c.

{
       qsort((void *)ItemList->items, ItemList->num_items, sizeof(PTextItemStruct), compare);
}
char* to_space ( char *  s)

Definition at line 74 of file converter_win.c.

{
       while (*s && !(isspace(*s) || *s == '\n')) s++;
       return s;
}
char* trim_string ( char *  s)

Definition at line 80 of file converter_win.c.

{
       char *head_ptr, *tail_ptr;

       if (!s || !*s) return(s);

       head_ptr = s;

       /* trim left space */
       while(*head_ptr && (*head_ptr == ' ' || *head_ptr == '\t' || *head_ptr == '\n'))
              head_ptr ++;

       if (!*head_ptr) return(head_ptr);

       /* trim right space */
       tail_ptr = head_ptr + strlen(head_ptr) - 1;
       
       while(*tail_ptr && (*tail_ptr == ' ' || *tail_ptr == '\t' || *tail_ptr == '\n')) {
              *tail_ptr = '\0';
              tail_ptr --;
       }

       return(head_ptr);
}

Here is the caller graph for this function: