gpio: Auto-export ports.
[utils.git] / capitalize.c
CommitLineData
1c2fdcc2
FT
1#include <stdlib.h>
2#include <stdio.h>
3#include <wchar.h>
4#include <wctype.h>
5#include <locale.h>
6
7wchar_t *exclude[] = {
8 L"a",
9 L"an",
10 L"the",
11 L"in",
12 L"to",
13 L"from",
14 L"of",
15 L"and",
16 L"or",
17 NULL
18};
19
20wchar_t *seps = L"-.?!";
21wchar_t *nonseps = L"\'";
22
23void capitalize(wchar_t *str)
24{
25 wchar_t *p, *p2, **ce;
26 int f;
27
28 p = str;
29 f = 1;
30 while(1) {
31 while(!iswalnum(*p)) {
32 if(*p == L'\0')
33 return;
34 if(wcschr(seps, *p) != NULL)
35 f = 1;
36 p++;
37 }
38 p2 = p;
39 while(iswalnum(*p) || (wcschr(nonseps, *p) != NULL)) {
40 *p = towlower(*p);
41 p++;
42 }
43 if(f) {
44 *p2 = towupper(*p2);
45 f = 0;
46 } else {
47 for(ce = exclude; *ce != NULL; ce++) {
48 if(!wcsncmp(*ce, p2, p - p2) && ((*ce)[p - p2] == L'\0'))
49 break;
50 }
51 if(*ce == NULL)
52 *p2 = towupper(*p2);
53 }
54 }
55}
56
57int main(int argc, char **argv)
58{
59 int i;
60 wchar_t buf[256];
61
62 setlocale(LC_ALL, "");
63 if(argc > 1) {
64 for(i = 1; i < argc; i++) {
65 mbstowcs(buf, argv[i], sizeof(buf) / sizeof(*buf));
66 capitalize(buf);
67 fputws(buf, stdout);
68 fputwc(L'\n', stdout);
69 }
70 } else {
71 while(1) {
72 if(fgetws(buf, sizeof(buf) / sizeof(*buf), stdin) == NULL) {
73 if(feof(stdin))
74 break;
75 perror("reading input");
76 return(1);
77 }
78 capitalize(buf);
79 fputws(buf, stdout);
80 }
81 }
82 return(0);
83}
84
85/*
86 * Local Variables:
87 * compile-command: "gcc -Wall -g -o capitalize capitalize.c"
88 * End:
89 */