summaryrefslogtreecommitdiffstats
path: root/lib/libsi18n/coreres.c
blob: ee179764c3ca333f8e3934b768ea2f7f546f4ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "i18n.h"

#include "propset.h"

#include "coreres.h"

Resource* core_res_init_resource(const char* path, const char* package)
{
    PropertiesSet *propset;
    char *directory;
    char *filename;
    char *file_path;
    char *p, *q;
    char *filep;
    Resource *hres;

    /*********************
      Create full path information
      eg. ./es40/admin  and  cgi.bin.start  ==>  
            ./es40/admin/cgi/bin/start.properties
    **********************/
    file_path = (char *) malloc (strlen(path) + strlen(package) + 20);


    strcpy(file_path, path);
    if (path[strlen(path)-1] != '/')
        strcat(file_path, "/");

    p = file_path + strlen(file_path);
    q = (char *) package;
    
    filep = p - 1;

    /*  Append package to file_path
        p: end positon of path + 1
        q: start position of package
     */
    while (q && *q) {
        if (*q == '.') {
            filep = q;
            *p ++ = '/';
        }
        else
            *p ++ = *q ++;

    }
    *p = '\0';

    *filep = '\0';
    filename = filep + 1;
    directory = file_path;

    propset = PropertiesInit (directory, filename);

    if (propset == NULL)
        return NULL;

    hres = (Resource *) malloc(sizeof(Resource));
    memset(hres, 0, sizeof(Resource));

    hres->path = strdup(file_path);
    hres->propset = propset;

    if (file_path)
        free (file_path);

    return hres;
}

const char *core_res_getstring(Resource *hres, char *key, ACCEPT_LANGUAGE_LIST lang) 
{

    if (key == NULL)
        return NULL;

    if (hres) {
        return PropertiesGetString(hres->propset, key, lang);
    }

    return NULL;
}

void core_res_destroy_resource(Resource *hres)
{
    if (hres) {
        if (hres->path)
            free(hres->path);
        if (hres->package)
            free(hres->package);
        PropertiesDestroy(hres->propset);

        free(hres);
    }
}