Add translation_driver.c, allow separate memory buffers for multiple instances

This commit is contained in:
meepingsnesroms 2018-01-24 18:35:14 -08:00
parent f07571f8eb
commit 9f2e395c45
4 changed files with 60 additions and 11 deletions

View File

@ -9,27 +9,28 @@ static const ocr_driver_t *ocr_backends[] = {
};
static const ocr_driver_t *current_ocr_backend = NULL;
static void *ocr_data = NULL;
bool ocr_driver_init(void)
{
/*TODO: find name of active driver*/
bool success = false;
ocr_data = NULL;
if (current_ocr_backend)
success = (*current_ocr_backend->init)();
return success;
ocr_data = (*current_ocr_backend->init)();
return ocr_data != NULL;
}
void ocr_driver_free(void)
{
if (current_ocr_backend)
(*current_ocr_backend->free)();
if (current_ocr_backend && ocr_data)
(*current_ocr_backend->free)(ocr_data);
}
char* ocr_driver_get_text(struct ocr_image_info image)
{
char* image_string = NULL;
if (current_ocr_backend)
image_string = (*current_ocr_backend->get_text)(image);
if (current_ocr_backend && ocr_data)
image_string = (*current_ocr_backend->get_text)(ocr_data, image);
return image_string;
}

View File

@ -15,7 +15,8 @@ typedef struct ocr_driver
void* (*init)();
void (*free)(void* data);
char* (*get_text)(struct ocr_image_info image);
/* returned char pointers do not need to be freed but are 1 time use, they may be destroyed on the next call to get_text */
char* (*get_text)(void* data, struct ocr_image_info image);
const char *ident;
} ocr_driver_t;
@ -27,6 +28,8 @@ extern const ocr_driver_t ocr_null;
bool ocr_driver_init(void);
void ocr_driver_free(void);
/* returned char pointers do not need to be freed but are 1 time use, they may be destroyed on the next call to ocr_driver_get_text */
char* ocr_driver_get_text(struct ocr_image_info image);
#endif

View File

@ -0,0 +1,40 @@
#include "translation_driver.h"
#include "ocr_driver.h"
static const translation_driver_t *translation_backends[] = {
&translation_cached_google,
&ocr_null,
NULL
};
static const translation_driver_t *current_translation_backend = NULL;
static void *translation_data = NULL;
bool translation_driver_init(void)
{
/*TODO: find name of active driver*/
translation_data = NULL;
if (current_translation_backend)
translation_data = (*current_translation_backend->init)();
return translation_data != NULL;
}
void translation_driver_free(void)
{
if (current_translation_backend && translation_data)
(*current_translation_backend->free)(translation_data);
}
char* translation_driver_translate_image(struct ocr_image_info image)
{
char* translated_text = NULL;
if (current_translation_backend && translation_data)
{
if (current_translation_backend->translate_image)
translated_text = (*current_translation_backend->translate_image)(translation_data, image);
else
translated_text = (*current_translation_backend->translate_text)(translation_data, ocr_driver_get_text(image));
}
return translated_text;
}

View File

@ -24,9 +24,10 @@ typedef struct translation_driver
void (*free)(void* data);
/* use translate_image if non NULL else run image through ocr driver then run translate_text */
/* returned char pointers do not need to be freed but are 1 time use, they may be destroyed on the next call to translate_image/text */
/* NOTE: translate_image is allowed to call the ocr driver itself if it wants */
char* (*translate_text)(const char* game_text);
char* (*translate_image)(struct ocr_image_info image);
char* (*translate_text)(void* data, const char* game_text);
char* (*translate_image)(void* data, struct ocr_image_info image);
const char *ident;
} translation_driver_t;
@ -34,6 +35,10 @@ typedef struct translation_driver
extern const translation_driver_t translation_cached_google;
extern const translation_driver_t translation_null;
char* translation_translate_image(struct ocr_image_info image);
bool translation_driver_init(void);
void translation_driver_free(void);
/* returned char pointers do not need to be freed but are 1 time use, they may be destroyed on the next call to translation_driver_translate_image */
char* translation_driver_translate_image(struct ocr_image_info image);
#endif