Back to index

im-sdk  12.3.91
ime_buffer.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "ime_buffer.h"
00003 
00004 
00005 void free_ime_buffer(ImeBufferRec *ime_buffer)
00006 {
00007     int i;
00008 
00009     if (ime_buffer) {
00010         if (ime_buffer->candidates.candidates) {
00011             for (i = 0; i < ime_buffer->max_cand_num; ++i) {
00012                 if (ime_buffer->candidates.candidates[i].text)
00013                     free(ime_buffer->candidates.candidates[i].text);
00014             }
00015             free(ime_buffer->candidates.candidates);
00016         }
00017         if (ime_buffer->candidates.numbers)
00018             free(ime_buffer->candidates.numbers);
00019         if (ime_buffer->preedit.preedit.text)
00020             free(ime_buffer->preedit.preedit.text);
00021         if (ime_buffer->commit_buf)
00022             free(ime_buffer->commit_buf);
00023         free(ime_buffer);
00024     }
00025 }
00026 
00027 ImeBufferRec * alloc_ime_buffer(
00028     int max_preedit_bytes,
00029     int max_candidate_num,
00030     int max_candidate_byte,
00031     int max_numberring_byte,
00032     int max_commit_byte)
00033 {
00034     int i, suc;
00035 
00036     ImeBufferRec *ime_buffer = (ImeBufferRec *)calloc(1, sizeof(ImeBufferRec));
00037     if (ime_buffer) {
00038 
00039         suc = ((ime_buffer->commit_buf = (unsigned char*)malloc(max_commit_byte)) != NULL);
00040         suc = suc && ((ime_buffer->preedit.preedit.text = (unsigned char*)calloc(1, max_preedit_bytes)) != NULL);
00041         suc = suc && ((ime_buffer->candidates.numbers = (unsigned char*)calloc(1, max_numberring_byte*max_candidate_num+1)) != NULL);
00042         suc = suc && ((ime_buffer->candidates.candidates = (ImeText)calloc(max_candidate_num, sizeof(ImeTextRec))) != NULL);
00043         if (suc) {
00044             memset(ime_buffer->candidates.candidates, 0, sizeof(ImeTextRec)*max_candidate_num);
00045             ime_buffer->max_cand_num = max_candidate_num;
00046         }
00047         for (i = 0; suc && i < max_candidate_num; ++i) {
00048             suc = suc && ((ime_buffer->candidates.candidates[i].text = (unsigned char *)calloc(1, max_candidate_byte)) != NULL);
00049         }
00050 
00051         if (!suc) {
00052             free_ime_buffer(ime_buffer);
00053             ime_buffer = NULL;
00054         }
00055     }
00056     clear_ime_buffer(ime_buffer);
00057     return ime_buffer;
00058 }
00059 
00060 void clear_ime_buffer(ImeBufferRec *ime_buffer)
00061 {
00062     if (ime_buffer) {
00063         if (ime_buffer->preedit.preedit.text)
00064             ime_buffer->preedit.preedit.text[0] = '\0';
00065         ime_buffer->preedit.caret = 0;
00066         ime_buffer->candidates.count = 0;
00067         ime_buffer->return_status = 0;
00068     }
00069 }