locales.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "config.h"
00032
00033
00034 #include <glib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <dirent.h>
00038
00039
00040
00041
00042 #include "log.h"
00043 #include "locales.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #define LOCALE_NAME_FILE "language.name"
00056 #define LOCALE_LIB_DIR "/usr/lib/locale"
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 static gint compare_by_language_name(gconstpointer a,gconstpointer b);
00069
00070
00071
00072
00073
00074
00075 GSList *locales_list_create()
00076 {
00077 DIR *dirp;
00078 struct dirent *dp;
00079 char language[64] = {0};
00080 GSList *locales_list = NULL;
00081 char *langpath = NULL;
00082 FILE *langfile = NULL;
00083 localeEntry *locale = NULL;
00084
00085 LOGPRINTF("entry");
00086
00087
00088 dirp = opendir(LOCALE_LIB_DIR);
00089 if (dirp == NULL)
00090 {
00091 ERRORPRINTF("could not open directory '%s'", LOCALE_LIB_DIR);
00092 }
00093
00094
00095 while ((dp = readdir(dirp)) != NULL)
00096 {
00097
00098 if ((dp->d_name[0] != '.' || (dp->d_name[1] != '\0' &&
00099 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))))
00100 {
00101
00102 language[0]='\0';
00103
00104
00105 langpath = g_strconcat(LOCALE_LIB_DIR, "/", dp->d_name, "/", LOCALE_NAME_FILE, NULL);
00106 langfile = fopen(langpath, "r");
00107 if (langfile != NULL)
00108 {
00109
00110 fgets(language, 63, langfile);
00111 fclose(langfile);
00112 }
00113 g_free(langpath);
00114
00115
00116 locale = g_new0 (localeEntry, 1);
00117 locale->code = g_strdup(dp->d_name);
00118 if (strlen(language) > 0)
00119 {
00120
00121 if (language[strlen(language)-1] == '\n')
00122 {
00123 language[strlen(language)-1]='\0';
00124 }
00125 locale->language = g_strdup(language);
00126 }
00127 else
00128 {
00129
00130 locale->language = g_strdup(dp->d_name);
00131 }
00132
00133 locales_list = g_slist_prepend(locales_list, locale);
00134 }
00135 }
00136 closedir(dirp);
00137
00138
00139 locales_list = g_slist_sort(locales_list, compare_by_language_name);
00140
00141 return locales_list;
00142 }
00143
00144
00145
00146 GSList *locales_list_codes(GSList *locales_list)
00147 {
00148 GSList *code_list = NULL;
00149
00150 LOGPRINTF("entry");
00151
00152 while (locales_list != NULL)
00153 {
00154 localeEntry *locale = locales_list->data;
00155 code_list = g_slist_append(code_list, locale->code);
00156 locales_list = locales_list->next;
00157 }
00158
00159 return code_list;
00160 }
00161
00162
00163 GSList *locales_list_languages(GSList *locales_list)
00164 {
00165 GSList *lang_list = NULL;
00166
00167 LOGPRINTF("entry");
00168
00169 while (locales_list != NULL)
00170 {
00171 localeEntry *locale = locales_list->data;
00172 lang_list = g_slist_append(lang_list, locale->language);
00173 locales_list = locales_list->next;
00174 }
00175
00176 return lang_list;
00177 }
00178
00179
00180 void locales_list_print(GSList *locales_list)
00181 {
00182 LOGPRINTF("entry");
00183
00184 while (locales_list != NULL)
00185 {
00186 #if (ERROR_ON)
00187 localeEntry *locale = locales_list->data;
00188
00189 ERRORPRINTF("code: %s, language: %s",
00190 (char*) locale->code,
00191 (char*) locale->language);
00192 #endif
00193
00194 locales_list = locales_list->next;
00195 }
00196 }
00197
00198
00199 void locales_list_free(GSList *locales_list)
00200 {
00201 LOGPRINTF("entry");
00202
00203 g_return_if_fail(locales_list != NULL);
00204
00205 while (locales_list != NULL)
00206 {
00207 localeEntry *locale = locales_list->data;
00208
00209
00210 g_free(locale->code);
00211 g_free(locale->language);
00212 g_free(locale);
00213
00214 locales_list =locales_list->next;
00215 }
00216
00217 g_slist_free(locales_list);
00218 locales_list = NULL;
00219 }
00220
00221
00222 static gint compare_by_language_name(gconstpointer a,gconstpointer b)
00223 {
00224 localeEntry *aa = (localeEntry *)a;
00225 localeEntry *bb = (localeEntry *)b;
00226
00227 return strcmp((gchar *) aa->language,(gchar *) bb->language);
00228 }