📄 hre_api.c
字号:
int recognizer_get_buffer(recognizer rec, u_int* nstrokes,pen_stroke** strokes)
{
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN,"Bad recognizer object.");
return(-1);
}
/*Do the function.*/
return(rec->recognizer_get_buffer(rec,nstrokes,strokes));
}
/*
* recognizer_set_buffer-Set stroke buffer to arg. Return 0 if success, else
* return -1.
*/
int recognizer_set_buffer(recognizer rec,u_int nstrokes,pen_stroke* strokes)
{
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN,"Bad recognizer object.");
return(-1);
}
/*Do the function.*/
return(rec->recognizer_set_buffer(rec,nstrokes,strokes));
}
/*
* recognizer_translate-Translate the strokes in the current context, including
* buffered strokes. If nstrokes == 0 or strokes == NULL, return
* translation of stroke buffer.
*/
int recognizer_translate(recognizer rec,
u_int nstrokes,
pen_stroke* strokes,
bool correlate_p,
int* nret,
rec_alternative** ret)
{
int retval;
char msg[80];
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN, msg);
return(-1);
}
/* ari */
/* {
* u_int i;
* pen_stroke ari_pstr;
* pen_point* ari_pts;
* int ari;
* for (i = 0; i < nstrokes; i++) {
* ari_pstr = strokes[i];
* ari_pts = ari_pstr.ps_pts;
* fprintf(stderr, "\nrecognizer_translate: ari_pts = %ld, sizeof(Time) = %d, sizeof(ari_pts[0] = %d, %d points are...\n", ari_pts, sizeof(Time), sizeof(ari_pts[0]), ari_pstr.ps_npts);
* for (ari = 0; ari < ari_pstr.ps_npts; ari++)
* fprintf(stderr, "%ld -- (%d, %d) ", ari_pts[ari], ari_pts[ari].x, ari_pts[ari].y);
* }
* }
*/
/*Do the function.*/
/* ari -- this is calling cmu_recognizer_translate */
retval = rec->recognizer_translate(rec,
nstrokes,
strokes,
correlate_p,
nret,
ret);
return (retval);
}
/*
* recognizer_get_extension_functions-Return a null terminated array
* of functions providing extended functionality. Their interfaces
* will change depending on the recognizer.
*/
rec_fn* recognizer_get_extension_functions(recognizer rec)
{
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN,"Bad recognizer object.");
return((rec_fn*)NULL);
}
/*Do the function.*/
return(rec->recognizer_get_extension_functions(rec));
}
/*
* recognizer_get_gesture_names - Return a null terminated array of
* gesture name strings.
*/
char**
recognizer_get_gesture_names(recognizer rec)
{
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN,"Bad recognizer object.");
return(NULL);
}
/*Do the function.*/
return(rec->recognizer_get_gesture_names(rec));
}
/*
* recognizer_set_gesture_action-Set the action function for the gesture.
*/
xgesture
recognizer_train_gestures(recognizer rec,char* name,xgesture fn,void* wsinfo)
{
/*Make sure magic numbers right.*/
if( !RI_CHECK_MAGIC(rec) ) {
the_last_error = dgettext(INTL_DOMAIN,"Bad recognizer object.");
return((xgesture)-1);
}
/*Do the function.*/
return(rec->recognizer_set_gesture_action(rec,name,fn,wsinfo));
}
/*
* Local functions.
*/
/*
* shared_library_name-Get the full pathname to the shared library,
* based on the recognizer name and the environment.
*/
#if 0
static char* shared_library_name(char* directory,char* locale,char* name)
{
char* ret = NULL;
int len = strlen(name);
/*If directory is there, it takes precedence.*/
if( directory != NULL ) {
ret = (char*)safe_malloc(strlen(directory) + len + 2);
strcpy(ret,directory);
strcat(ret,"/");
strcat(ret,name);
}
else {
char* dir = NULL;
/*First try the environment variable.*/
if( (dir = getenv(RECHOME)) == NULL ) {
dir = "REC_DEFAULT_HOME_DIR";
}
ret = (char*)safe_malloc(strlen(dir) + strlen(locale) + len + 3);
/*Form the pathname.*/
strcpy(ret,dir);
strcat(ret,"/");
strcat(ret,locale);
strcat(ret,"/");
strcat(ret,name);
}
return(ret);
}
#endif
/*
* intl_initialize-Initialize the internationaliztion of messages for
* the recognition manager.
*/
static void intl_initialize()
{
char* dirname;
/*Get recognizer home directory name from environment.*/
if( (dirname = getenv(RECHOME)) == NULL ) {
dirname = "REC_DEFAULT_HOME_DIR";
}
/*Bind the text domain.*/
bindtextdomain(dirname,INTL_DOMAIN);
}
/*make_rec_info-Create a rec_info structure*/
static rec_info* make_rec_info(char* directory,char* name,char** subset)
{
int i,len;
rec_info* ri;
char* locale;
ri = (rec_info*)safe_malloc(sizeof(rec_info));
ri->ri_locale = NULL;
ri->ri_name = NULL;
ri->ri_subset = NULL;
/*Get locale*/
if( (locale = getenv(LANG)) == NULL ) {
locale = strdup(REC_DEFAULT_LOCALE);
}
if( (ri->ri_locale = strdup(locale)) == NULL ) {
delete_rec_info(ri);
return(NULL);
}
/*Get shared library pathname.*/
/*
* if( (ri->ri_name = shared_library_name(directory,locale,name)) == NULL ) {
* delete_rec_info(ri);
* return(NULL);
* }
*/
/*Initialize the subset information.*/
if( subset != NULL ) {
/*Count the subset strings.*/
for( len = 1; subset[len] != NULL; len++ ) ;
/*Copy the subset strings.*/
ri->ri_subset = (char**)safe_malloc((len +1)*sizeof(char*));
for( i = 0; i < len; i++ ) {
if( subset[i] != NULL ) {
if( (ri->ri_subset[i] = strdup(subset[i])) == NULL ) {
delete_rec_info(ri);
return(NULL);
}
} else {
ri->ri_subset[i] = subset[i];
}
}
ri->ri_subset[i] = NULL;
} else {
ri->ri_subset = NULL;
}
return(ri);
}
static void delete_rec_info(rec_info* ri)
{
if( ri != NULL ) {
if( ri->ri_locale != NULL ) {
free(ri->ri_locale);
}
/*
* if( ri->ri_name != NULL ) {
* free(ri->ri_name);
* }
*/
if( ri->ri_subset != NULL ) {
int i;
for( i = 0; ri->ri_subset[i] != NULL; i++) {
free(ri->ri_subset[i]);
}
free(ri->ri_subset);
}
free(ri);
}
}
/*check_for_user_home-Check whether USERRECHOME has been created.*/
#if 0
static int check_for_user_home()
{
char* homedir = getenv(HOME);
char* rechome = NULL;
if( homedir == NULL ) {
the_last_error = "Home environment variable HOME not set.";
return(-1);
}
rechome = (char*)safe_malloc(strlen(homedir) + strlen(USERRECHOME) + 2);
/*Form name.*/
strcpy(rechome,homedir);
strcat(rechome,"/");
strcat(rechome,USERRECHOME);
/*Create directory.*/
if( mkdir(rechome,S_IRWXU | S_IRWXG | S_IRWXO) < 0 ) {
/*If errno is EEXIST, then OK.*/
if( errno != EEXIST ) {
the_last_error = "Error during creation of USERRECHOME.";
free(rechome);
return(-1);
}
}
free(rechome);
return(0);
}
#endif
/*
* Constructor functions for making structures.
*
* The general philosophy here is that we control all memory
* in connected data structures, *except* for pen_point arrays.
* There are likely to be lots and lots of points, they are likely
* to come from the window system; so if we wanted to control them,
* we would have to copy which would be slow. We require the client
* to deal with them directly, or the client can give us permission
* to delete them.
*/
/*
* recognizer
*/
recognizer make_recognizer(rec_info* rif)
{
recognizer rec;
/*Allocate it.*/
rec = (recognizer)safe_malloc(sizeof(*rec));
rec->recognizer_magic = REC_MAGIC;
rec->recognizer_version = REC_VERSION;
rec->recognizer_info = rif;
rec->recognizer_specific = NULL;
rec->recognizer_end_magic = REC_END_MAGIC;
rec->recognizer_load_state = NULL;
rec->recognizer_save_state = NULL;
rec->recognizer_load_dictionary = NULL;
rec->recognizer_save_dictionary = NULL;
rec->recognizer_free_dictionary = NULL;
rec->recognizer_add_to_dictionary = NULL;
rec->recognizer_delete_from_dictionary = NULL;
rec->recognizer_error = NULL;
rec->recognizer_set_context = NULL;
rec->recognizer_get_context = NULL;
rec->recognizer_clear = NULL;
rec->recognizer_get_buffer = NULL;
rec->recognizer_set_buffer = NULL;
rec->recognizer_translate = NULL;
rec->recognizer_get_extension_functions = NULL;
rec->recognizer_get_gesture_names = NULL;
rec->recognizer_set_gesture_action = NULL;
return(rec);
}
void delete_recognizer(recognizer rec)
{
if( rec != NULL ) {
if( rec->recognizer_info != NULL ) {
delete_rec_info(rec->recognizer_info);
}
free(rec);
}
}
/*
* rec_alternative
*/
rec_alternative* make_rec_alternative_array(u_int size)
{
int i;
rec_alternative* ri;
ri = (rec_alternative*) safe_malloc(size * sizeof(rec_alternative));
for( i = 0; i < size; i++ ) {
ri[i].ra_elem.re_type = REC_NONE;
ri[i].ra_elem.re_result.aval = NULL;
ri[i].ra_elem.re_conf = 0;
ri[i].ra_nalter = 0;
ri[i].ra_next = NULL;
}
return(ri);
}
rec_alternative*
initialize_rec_alternative(rec_alternative* ra,
u_int nelem)
{
if( ra != NULL ) {
if( (ra->ra_next = make_rec_alternative_array(nelem)) == NULL ) {
return(NULL);
}
ra->ra_nalter = nelem;
}
return(ra);
}
void delete_rec_alternative_array(u_int nalter,
rec_alternative* ra,
bool delete_points_p)
{
int i;
if( ra != NULL ) {
for( i = 0; i < nalter; i++ ) {
cleanup_rec_element(&ra[i].ra_elem,delete_points_p);
/*Now do the next one down the line.*/
if( ra[i].ra_nalter > 0 ) {
delete_rec_alternative_array(ra[i].ra_nalter,
ra[i].ra_next,
delete_points_p);
}
}
free(ra);
}
}
/*initialize_rec_element-Initialize a recognition element.*/
rec_element*
initialize_rec_element(rec_element* re,
char type,
u_int size,
void* trans,
rec_confidence conf)
{
if( re != NULL ) {
re->re_type = type;
re->re_conf = conf;
re->re_result.aval = NULL;
switch (type) {
case REC_GESTURE:
if( size > 0 && trans != NULL ) {
re->re_result.gval =
(gesture*)safe_malloc(sizeof(gesture));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -