From 28b2eafa35b49b06281bbca04a9b9c913de50a07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Domonkos=20Lezs=C3=A1k?= <lezsakdomi1@gmail.com>
Date: Wed, 6 Dec 2017 18:13:33 +0100
Subject: [PATCH] Statisfy the Coding Guidelines
---
src/bin/setlocale.cpp | 78 +++++++++++++++++++++++++++++++--------------------
1 file changed, 47 insertions(+), 31 deletions(-)
diff --git a/src/bin/setlocale.cpp b/src/bin/setlocale.cpp
index 97ab210019..a2689535d1 100644
a
|
b
|
matches(const char* a, const char* b)
|
29 | 29 | return !strcasecmp(a, b); |
30 | 30 | } |
31 | 31 | |
32 | | static const char* helpMessage = |
| 32 | |
| 33 | static const char* kHelpMessage = |
33 | 34 | "Set system-wide locale\n" |
34 | 35 | "\n" |
35 | 36 | "Arguments for long options are mandatory for short options too.\n" |
… |
… |
static const char* helpMessage =
|
38 | 39 | "\n" |
39 | 40 | "Arguments for listing locales:\n" |
40 | 41 | " -l, --list Switch to list mode.\n" |
41 | | " Output format: language<TAB>country<TAB>script<TAB>variant<TAB>...name...<NEWLINE>\n" |
| 42 | " Output format: language<TAB>country<TAB>script<TAB>" |
| 43 | "variant<TAB>...name...<NEWLINE>\n" |
42 | 44 | "\n" |
43 | 45 | "Arguments for setting locale:\n" |
44 | 46 | " -s, --script Specify script for locale\n" |
45 | 47 | " -x, --variant Specify language variant\n" |
46 | 48 | ; |
47 | | static const char* noCodeSpecifiedMessage = "Error: No language code specified.\n"; |
48 | | static const char* tooManyTrailingArgsMessage = "Error: To many (%d) trailing arguments specified. Only a language code (and maybe a country code) allowed.\n"; |
49 | | static const char* unknownArgMessage = "Error: Unknown argument: -%c\n"; |
50 | | static const char* usageMessage = |
| 49 | static const char* kNoCodeSpecifiedMessage = |
| 50 | "Error: No language code specified.\n"; |
| 51 | static const char* kTooManyTrailingArgsMessage = |
| 52 | "Error: To many (%d) trailing arguments specified." |
| 53 | "Only a language code (and maybe a country code) allowed.\n"; |
| 54 | static const char* kUnknownArgMessage = "Error: Unknown argument: -%c\n"; |
| 55 | static const char* kUsageMessage = |
51 | 56 | "Usage: setlocale -l|--list\n" |
52 | 57 | "or setlocale -h|--help\n" |
53 | | "or setlocale language [country] [-s|--script script] [-v|--variant variant]\n" |
| 58 | "or setlocale language [country] [-s|--script script]" |
| 59 | "[-v|--variant variant]\n" |
54 | 60 | ; |
55 | 61 | |
56 | 62 | int |
… |
… |
main(const int argc, char* const argv[])
|
71 | 77 | { "variant", required_argument, NULL, 'x' }, |
72 | 78 | }; |
73 | 79 | const char* optstring = "hls:x:"; |
74 | | |
75 | | int c; |
76 | | while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) |
| 80 | |
| 81 | for (int c = getopt_long(argc, argv, optstring, longopts, NULL); |
| 82 | c != -1; c = getopt_long(argc, argv, optstring, longopts, NULL)) |
77 | 83 | { |
78 | | switch (c) { |
| 84 | switch (c) |
| 85 | { |
79 | 86 | case 'h': |
80 | | puts(usageMessage); |
81 | | puts(helpMessage); |
| 87 | puts(kUsageMessage); |
| 88 | puts(kHelpMessage); |
82 | 89 | return 0; |
83 | 90 | |
84 | 91 | case 'l': |
… |
… |
main(const int argc, char* const argv[])
|
94 | 101 | break; |
95 | 102 | |
96 | 103 | default: |
97 | | fprintf(stderr, unknownArgMessage, c); |
| 104 | // error |
| 105 | fprintf(stderr, kUnknownArgMessage, c); |
98 | 106 | fputc('\n', stderr); |
99 | | fputs(usageMessage, stderr); |
| 107 | fputs(kUsageMessage, stderr); |
100 | 108 | return 1; |
101 | 109 | } |
102 | 110 | } |
103 | 111 | |
104 | 112 | { |
105 | | // check for proper command line, requiring exactly one of list or query |
106 | | if (list) { |
107 | | if (argc != optind) { |
108 | | fputs(listModeButTrailingArgsMessage, stderr); |
| 113 | if (list) |
| 114 | { |
| 115 | if (argc != optind) |
| 116 | { |
| 117 | fputs(kListModeButTrailingArgsMessage, stderr); |
109 | 118 | fputc('\n', stderr); |
110 | | fputs(usageMessage, stderr); |
| 119 | fputs(kUsageMessage, stderr); |
111 | 120 | return 1; |
112 | 121 | } |
113 | 122 | } else { |
114 | | int numRemainOptions = argc-optind; |
115 | | switch (numRemainOptions) { |
| 123 | int numRemainOptions = argc - optind; |
| 124 | switch (numRemainOptions) |
| 125 | { |
116 | 126 | case 2: |
117 | | queryCountry = argv[optind+1]; //fallthru |
| 127 | queryCountry = argv[optind + 1]; |
| 128 | // fallthru |
118 | 129 | case 1: |
119 | 130 | queryCode = argv[optind]; |
120 | 131 | break; |
121 | 132 | |
122 | 133 | case 0: |
123 | 134 | if (list) break; |
124 | | fputs(noCodeSpecifiedMessage, stderr); |
| 135 | fputs(kNoCodeSpecifiedMessage, stderr); |
125 | 136 | fputc('\n', stderr); |
126 | | fputs(usageMessage, stderr); |
| 137 | fputs(kUsageMessage, stderr); |
127 | 138 | return 1; |
128 | 139 | |
129 | 140 | default: |
130 | | fprintf(stderr, tooManyTrailingArgsMessage, numRemainOptions); |
| 141 | fprintf(stderr, kTooManyTrailingArgsMessage, |
| 142 | numRemainOptions); |
131 | 143 | fputc('\n', stderr); |
132 | | fputs(usageMessage, stderr); |
| 144 | fputs(kUsageMessage, stderr); |
133 | 145 | return 1; |
134 | 146 | } |
135 | 147 | } |
… |
… |
main(const int argc, char* const argv[])
|
141 | 153 | BLocaleRoster::Default()->GetAvailableLanguages(&languages); |
142 | 154 | |
143 | 155 | const char* id; |
144 | | for (int32 i = 0; languages.FindString("language", i, &id) == B_OK; ++i) { |
| 156 | for (int32 i = 0; languages.FindString("language", i, &id) == B_OK; i++) |
| 157 | { |
145 | 158 | BLanguage* language; |
146 | | if (BLocaleRoster::Default()->GetLanguage(id, &language) == B_OK) { |
| 159 | if (BLocaleRoster::Default()->GetLanguage(id, &language) == B_OK) |
| 160 | { |
147 | 161 | // extract all the relevant information about the language |
148 | 162 | BString name; |
149 | 163 | language->GetNativeName(name); |
… |
… |
main(const int argc, char* const argv[])
|
163 | 177 | else if (!strcasecmp(code, queryCode) |
164 | 178 | && matches(country, queryCountry) |
165 | 179 | && matches(script, queryScript) |
166 | | && matches(variant, queryVariant)) { |
| 180 | && matches(variant, queryVariant)) |
| 181 | { |
167 | 182 | // found a match! set the new locale |
168 | 183 | BMessage preferred; |
169 | 184 | preferred.AddString("language", id); |
… |
… |
main(const int argc, char* const argv[])
|
181 | 196 | |
182 | 197 | // if we've gotten this far and the -l flag wasn't passed, the requested |
183 | 198 | // locale wasn't found |
184 | | if (!list) { |
| 199 | if (!list) |
| 200 | { |
185 | 201 | fprintf(stderr, "Locale not found\n"); |
186 | 202 | return 1; |
187 | 203 | } |