~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* locale_alias - Test program to create a file of locale to language name
                  aliases

  Copyright (C) 2005 Free Software Foundation, Inc.

  Written: Adam Fedor <fedor@gnu.org>
  Date: Oct 2000

  AFAIK: This only works on machines that support setlocale.
  The files created may require hand editing.

   This file is part of the GNUstep Project

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   You should have received a copy of the GNU General Public
   License along with this program; see the file COPYINGv3.
   If not, write to the Free Software Foundation,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

*/

#import	"common.h"

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <locale.h>

#import	"Foundation/NSAutoreleasePool.h"
#import	"Foundation/NSDictionary.h"
#import "GNUstepBase/GSLocale.h"

#define MAXSTRING 100

static int debug=1;

NSMutableDictionary *dict;

int
loc_read_file(const char *dir, const char *file)
{
  FILE *fp;
  char name[1000], *s;
  char buf[1000];
  char locale[MAXSTRING], language[MAXSTRING], country[MAXSTRING];

  if (strcmp(file, "POSIX") == 0)
    return 0;

  snprintf(name, sizeof(name), "%s/%s", dir, file);
  fp = fopen(name, "r");
  if (fp == NULL)
    return -1;

  language[0] = '\0';
  country[0] = '\0';
  while (1)
    {
      fgets(buf, MAXSTRING, fp);
      if (strstr(buf, "anguage") != NULL)
	{
	  sscanf(&buf[2], "%s", language);
	}
      if ((s = strstr(buf, "ocale for")) != NULL)
	{
	  strncpy(country, s + 10, sizeof(country) - 1);
	  country[sizeof(country) - 1] = '\0';
	  s = strchr(country, '\n');
	  if (s)
	    *s = '\0';
	}
      if (strlen(language) > 0)
	break;
    }

  strncpy(locale, file, sizeof(locale) - 1);
  locale[sizeof(locale) - 1] = '\0';
  if (strlen(country) > 0 && strcmp(country, language) != 0)
    {
      strncat(country, language, sizeof(country) - 1 - strlen(country));
      [dict setObject: [NSString stringWithUTF8String: country]
	    forKey: [NSString stringWithUTF8String: locale]];
    }
  locale[2] = '\0';
  [dict setObject: [NSString stringWithUTF8String: language]
	forKey: [NSString stringWithUTF8String: locale]];
  fclose(fp);
  return 0;
}

/* Go through all the files in the directory */
int
loc_get_files(const char *dir)
{
  struct dirent *dp;
  DIR *dirp;

  dirp = opendir(dir);
  while ((dp = readdir(dirp)) != NULL)
    {
      if (isalpha((dp->d_name)[0]))
	{
	  if (debug)
	    printf(" checking %s ...\n", dp->d_name);
	  loc_read_file(dir, dp->d_name);
	}
    }
  closedir(dirp);
  return 0;
}

int
main(int argc, char *argv[])
{
  NSString *lang;
  char *l;
  CREATE_AUTORELEASE_POOL(pool);

  l = setlocale(LC_ALL, "");
  printf("Locale is %s\n", l);

  /* Create Locale.aliases */
  dict = [NSMutableDictionary dictionary];
  loc_get_files("/usr/share/i18n/locales");
  [dict writeToFile: @"Locale.aliases" atomically: NO];

  /* Write out a skeleton file from the current locale */
  dict = GSDomainFromDefaultLocale();
  lang = GSLanguageFromLocale(GSSetLocale(NULL));
  if (lang == nil)
    lang = @"Locale";
  if (dict)
    [dict writeToFile: lang atomically: NO];

  DESTROY(pool);
  return 0;
}