Converter::~Converter()
{
    if (!s_loaded)
        return;

    if (handle != NULL && handle != (opencc_t) -1)
        opencc_close(handle);
}
Exemple #2
0
void
_opencc_close(openccobject* self)
{
    if (self->opencc) {
        opencc_close(self->opencc);
        self->opencc = NULL;
    }
}
QString Converter::convert(QString & text)
{
    QByteArray text_utf8 = text.toUtf8();
    const char * buffer_in = text_utf8.data();
    char * buffer_out = opencc_convert_utf8(handle, buffer_in, -1);
    if (buffer_out == (char *) -1)
    {
        opencc_perror("Opencc runtime:");
        opencc_close(handle);
        return ""; //TODO failed
    }
    QString retval = QString::fromUtf8(buffer_out);
    free(buffer_out);
    return retval;
}
void Converter::setConfig(const char *config)
{
    if (!s_loaded)
        return;

    if (handle != NULL)
        opencc_close(handle);

    handle = opencc_open(config);
    if (handle == (opencc_t) -1)
    {
        opencc_perror("Opencc loading:");
        return;
    }
    m_loaded = true;
}
Exemple #5
0
const char *Convert(const char *input, const char *config) {
	if(strlen(config) > 16) {
		return 0;
	}

	char configFile[256] = "/usr/share/opencc/";
	strcat(configFile, config);
	strcat(configFile, ".json");

	opencc_t p = opencc_open(configFile);
	char *out = opencc_convert_utf8(p, input, strlen(input));
	out[strlen(input)] = '\0';

	opencc_close(p);

	return out;
}
Exemple #6
0
void convert(const char * input_file, const char * output_file, const char * config_file)
{
	opencc_t od = opencc_open(config_file);
	if (od == (opencc_t) -1)
	{
		opencc_perror(_("OpenCC initialization error"));
		exit(1);
	}

	FILE * fp = stdin;
	FILE * fpo = stdout;
	
	if (input_file)
	{
		fp = fopen(input_file, "r");
		if (!fp)
		{
			fprintf(stderr, _("Can not read file: %s\n"), input_file);
			exit(1);
		}
	}
	
	if (output_file)
	{
		fpo = fopen(output_file, "w");
		if (!fpo)
		{
			fprintf(stderr, _("Can not write file: %s\n"), output_file);
			exit(1);
		}
	}
	
	size_t size = BUFFER_SIZE;
	char * buffer_in = NULL, * buffer_out = NULL;
	buffer_in = (char *) malloc(size * sizeof(char));
	
	while (fgets(buffer_in, size, fp) != NULL)
	{
		size_t freesize = size;
		
		char * buffer_in_p = buffer_in;
		size_t line_length = strlen(buffer_in_p);
		while (line_length + 1 == freesize && buffer_in_p[line_length - 2] != '\n')
		{
			//如果一行沒讀完,則最後一個字符不是換行,且讀滿緩衝區
			buffer_in_p += size - 1;
			freesize = size + 1;
			size += size;
			size_t offset = buffer_in_p - buffer_in;
			buffer_in = (char *) realloc(buffer_in, size * sizeof(char));
			buffer_in_p = buffer_in + offset;
			
			if (fgets(buffer_in_p, freesize, fp) == NULL)
				break;

			line_length = strlen(buffer_in_p);
		}
		
		buffer_out = opencc_convert_utf8(od, buffer_in, (size_t) -1);
		if (buffer_out != (char *) -1)
		{
			fprintf(fpo, "%s", buffer_out);
		}
		else
		{
			opencc_perror(_("OpenCC error"));
			break;
		}
	}
	
	opencc_close(od);
	
	free(buffer_in);
	free(buffer_out);
	
	fclose(fp);
	fclose(fpo);
}
ChineseConvertor::~ChineseConvertor()
{
    if (opencc != NULL && opencc != (opencc_t) -1)
        opencc_close(opencc);
}
Exemple #8
0
void Opencc_Delete(void *id) {
	opencc_close(id);
}
Exemple #9
0
void convert(const char * input_file, const char * output_file, const char * config_file)
{
	opencc_t od = opencc_open(config_file);
	if (od == (opencc_t) -1)
	{
		opencc_perror(_("OpenCC initialization error"));
		exit(1);
	}

	FILE * fp = stdin;
	FILE * fpo = stdout;
	
	if (input_file)
	{
		fp = fopen(input_file, "r");
		if (!fp)
		{
			fprintf(stderr, _("Can not read file: %s\n"), input_file);
			exit(1);
		}
	}
	
	if (output_file)
	{
		fpo = fopen(output_file, "w");
		if (!fpo)
		{
			fprintf(stderr, _("Can not write file: %s\n"), output_file);
			exit(1);
		}
	}
	
	size_t size = BUFFER_SIZE;
	char * buffer_in = NULL, * buffer_out = NULL;
	buffer_in = (char *) malloc(size * sizeof(char));

    while (!feof(fp))
    {
        size_t read = fread(buffer_in, 1, size, fp);

        // If we haven't finished reading after filling the entire buffer,
        // then it could be that we broke within an UTF-8 character, in
        // that case we must backtrack and find the boundary
        if (read == size) {
            // Find the boundary of last UTF-8 character
            int i;
            for (i = read - 1; i >= 0; i--)
            {
                char c = buffer_in[i];
                if (!(c & 0x80) || ((c & 0xC0) == 0xC0))
                    break;
            }

            if (i >= 0) {
                buffer_in[i] = '\0';
                // Since we read a bit too much, move file pointer back
                // just that much so that we can continue reading
                size_t n = read - i;
                fseek(fp, -n, SEEK_CUR);
            }
        }

		buffer_out = opencc_convert_utf8(od, buffer_in, (size_t) -1);
		if (buffer_out != (char *) -1)
		{
			fprintf(fpo, "%s", buffer_out);
			free(buffer_out);
		}
		else
		{
			opencc_perror(_("OpenCC error"));
			break;
		}
	}
	
	opencc_close(od);
	
	free(buffer_in);
	
	fclose(fp);
	fclose(fpo);
}