locales.c

Go to the documentation of this file.
00001 /*
00002  * File name: locales.c
00003  */
00004 
00005 /*
00006  * This file is part of sysd.
00007  *
00008  * sysd is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * sysd is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /*
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 #include "config.h"
00032 
00033 // system include files, between < >
00034 #include <glib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <dirent.h>
00038 
00039 // ereader include files, between < >
00040 
00041 // local include files, between " "
00042 #include "log.h"
00043 #include "locales.h"
00044 
00045 
00046 //----------------------------------------------------------------------------
00047 // Type Declarations
00048 //----------------------------------------------------------------------------
00049 
00050 
00051 //----------------------------------------------------------------------------
00052 // Global Constants
00053 //----------------------------------------------------------------------------
00054 
00055 #define LOCALE_NAME_FILE    "language.name"
00056 #define LOCALE_LIB_DIR      "/usr/lib/locale"
00057 
00058 
00059 //----------------------------------------------------------------------------
00060 // Static Variables
00061 //----------------------------------------------------------------------------
00062 
00063 
00064 //============================================================================
00065 // Local Function Definitions
00066 //============================================================================
00067 
00068 static gint compare_by_language_name(gconstpointer a,gconstpointer b);
00069 
00070 
00071 //============================================================================
00072 // Functions Implementation
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     // open locales directory 
00088     dirp = opendir(LOCALE_LIB_DIR);
00089     if (dirp == NULL)
00090     {
00091         ERRORPRINTF("could not open directory '%s'", LOCALE_LIB_DIR);
00092     }
00093 
00094     // scan directory and store its contents except "." and ".." 
00095     while ((dp = readdir(dirp)) != NULL) 
00096     {
00097         // exclude "." and ".."
00098         if ((dp->d_name[0] != '.' || (dp->d_name[1] != '\0' &&
00099             (dp->d_name[1] != '.' ||  dp->d_name[2] != '\0'))))
00100         {
00101             // reset language name
00102             language[0]='\0';
00103 
00104             // open language name file
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                 // read language name from file
00110                 fgets(language, 63, langfile);
00111                 fclose(langfile);
00112             }
00113             g_free(langpath);
00114 
00115             // add locale to list
00116             locale = g_new0 (localeEntry, 1);
00117             locale->code = g_strdup(dp->d_name);
00118             if (strlen(language) > 0)
00119             {
00120                 // found language name, remove trailing linefeed
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                 // no native name found, store locale code
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     // sort locales by language name
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       // free
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 //    LOGPRINTF("sort: %s, %s", (gchar *) (aa->language),(gchar *) (bb->language));
00227     return strcmp((gchar *) aa->language,(gchar *) bb->language);
00228 }
Generated by  doxygen 1.6.2-20100208