[MPlayer-dev-eng] liabf/af.c translatables to help_mp

Alex Beregszaszi alex at fsn.hu
Thu Mar 16 12:41:01 CET 2006


Hi,

> > > mp_msg and translated messaging residing in help_*.h. Why not using
> > > gettext for that translation matter as any other sane project?
> > 
> > Because it seems to need setlocale, which in turn breaks MPlayer all oer
> > the place. Or in other words: gettext: broken as designed.
> 
> What about designing a similar system? Code is better than just ideas, I
> will send a small implementation.

Attached is a lame implementation.

-- 
Alex Beregszaszi	email: alex at fsn.hu
Free Software Network	cell: +36 70 3144424
-------------- next part --------------
#include "etr.h"

main() {
    etr_init("test.etr");
    printf(etr("hello world\n"));
    printf(etr("hello mumbasa\n"));
    printf(etr("hello %d\n"), 1);
    printf(etr("szia %d\n"), 1);
    etr_close();
}
-------------- next part --------------
# test file

@hello world
szia vilag
-------------- next part --------------
#ifndef ETR_H
#define ETR_H

void etr_init(char *file);
char *etr(char *str);
void etr_close();

#endif
-------------- next part --------------
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "etr.h"

static int etr_fd = -1;
typedef struct hash_t {
    char *orig; // replace with hashing
    char *new;
} hash_t;
static hash_t *etr_hash = NULL;
static int etr_entries = 0;

// LOL, for debugging only
static char *escape(char *line) {
    int i;
    for (i = 0; i < strlen(line); i++)
	if (line[i] == '%') {
	    line[i] = '@';
	    printf("esc: %d\n", i);
	}
    return line;
}

static char *sreadline(int fd) {
    char buf[256], tmp;
    int pos = 0;
    
    do {
	if (read(fd, &tmp, 1) != 1)
	    return NULL;
	buf[pos++] = tmp;
    } while(tmp != '\n' && tmp != 0 && pos < 256);
    buf[pos] = 0;
//    printf("readline: %s\n", escape(strdup(buf)));
    return strdup(buf);
}

void etr_init(char *file) {
    if (file)
	etr_fd = open(file, O_RDONLY);
//    printf("successfully opened %s (%d)\n", file, etr_fd);

    // load hash
    if (etr_fd != -1) {
	while(1) {
	    char *line = sreadline(etr_fd);
	
	    if (!line)
		break; // eof
	    if (line[0] == '#') {
		free(line);
		continue; // comment
	    }
	    // original text starts with @
	    if (line[0] == '@') {
		hash_t *n;

		etr_entries++;
		etr_hash = realloc(etr_hash, sizeof(hash_t)*etr_entries);
		memset(&etr_hash[etr_entries-1], 0, sizeof(hash_t));
//		printf("new entry: %d %x %x\n", etr_entries-1, etr_hash, &etr_hash[etr_entries-1]);

		n = &etr_hash[etr_entries-1];
		n->orig = strdup(line+1);
		n->new = strdup(sreadline(etr_fd));
	    }
	
    	    free(line);
	}
    }
//    printf("init done, %d entries\n", etr_entries);
}

void etr_close() {
    if (etr_fd != -1)
	close(etr_fd);
    if (etr_hash) {
	int i;
	for (i = 0; i < etr_entries; i++) {
	    hash_t e = etr_hash[i];
	    if (e.orig)
	        free(e.orig);
	    if (e.new)
	        free(e.new);
	}
	free(etr_hash);
    }
    etr_entries = 0;
    etr_hash = NULL;
    etr_fd = -1;
}


char *etr(char *orig) {
    int i;

    if (etr_fd < 0)
	return orig;

    for (i = 0; i < etr_entries; i++) {
        hash_t *e = &etr_hash[i];
//	printf("checking entry: '%s' vs '%s'\n",
//	    escape(strdup(e->orig)), escape(strdup(orig)));
	if (!strcmp(e->orig, orig))
	    return e->new;
    }
 
    return orig; // fallback to original
}


More information about the MPlayer-dev-eng mailing list