*** empty log message ***
[m17n/m17n-lib.git] / example / mdate.c
1 /* mdate.c -- Show the sysmet date and time in all locales.
2    Copyright (C) 2003, 2004
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 /***en
24     @page mdate display date and time
25
26     @section mdate-synopsis SYNOPSIS
27
28     mdate [ OPTION ... ]
29
30     @section mdate-description DESCRIPTION
31
32     Display the system date and time in many locales on a window.
33
34     The following OPTIONs are available.
35
36     <ul>
37
38     <li> --version
39
40     Print version number.
41
42     <li> -h, --help
43
44     Print this message.
45     </ul>
46 */
47
48 #ifndef FOR_DOXYGEN
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <sys/time.h>
55 #include <locale.h>
56
57 #include <m17n.h>
58 #include <m17n-misc.h>
59
60 #define VERSION "1.0"
61
62 /* Return a plist of all locales currently avairable on the system.
63    The keys and values of the plist are locale name symbols and
64    pointers to MLocale respectively.  */
65
66 MPlist *
67 list_system_locales ()
68 {
69   FILE *p;
70   char buf[1024];
71   MPlist *plist;
72
73   /* Run the command "locale -a" to get a list of locales.  */
74   if (! (p = popen ("locale -a", "r")))
75     {
76       fprintf (stderr, "Can't run `locale -a'.\n");
77       exit (1);
78     }
79
80   plist = mplist ();
81   /* Read from the pipe one line by one.  */
82   while (fgets (buf, 1024, p))
83     {
84       MLocale *locale;
85       int len = strlen (buf);
86
87       if (buf[len - 1] == '\n')
88         buf[len - 1] = '\0';
89       locale = mlocale_set (LC_TIME, buf);
90
91       /* If the locale is surely usable, it is not duplicated, and we
92          know the corresponding coding system, add it to the list.  */
93       if (locale
94           && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
95           && mlocale_get_prop (locale, Mcoding) != Mnil)
96         mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
97     }
98   pclose (p);
99   return plist;
100 }
101
102
103 /* Print the usage of this program (the name is PROG), and exit with
104    EXIT_CODE.  */
105
106 void
107 help_exit (char *prog, int exit_code)
108 {
109   char *p = prog;
110
111   while (*p)
112     if (*p++ == '/')
113       prog = p;
114
115   printf ("Usage: %s [ OPTION ...]\n", prog);
116   printf ("Display the system date and time in many locales on a window.\n");
117   printf ("The following OPTIONs are available.\n");
118   printf ("  %-13s %s", "--version", "Print version number.\n");
119   printf ("  %-13s %s", "-h, --help", "Print this message.\n");
120   exit (exit_code);
121 }
122
123
124 /* Format MSG by FMT and print the result to the stderr, and exit.  */
125
126 #define FATAL_ERROR(fmt, arg)   \
127   do {                          \
128     fprintf (stderr, fmt, arg); \
129     exit (1);                   \
130   } while (0)
131
132
133 int
134 main (int argc, char **argv)
135 {
136   time_t current_time_t = time (NULL);
137   struct tm *current_time_tm = localtime (&current_time_t);
138   /* List of all locales.  */
139   MPlist *locale_list, *plist;
140   /* Text to be shown.  */
141   MText *mt;
142   int i;
143
144   for (i = 1; i < argc; i++)
145     {
146       if (! strcmp (argv[i], "--help")
147                || ! strcmp (argv[i], "-h")
148                || ! strcmp (argv[i], "-?"))
149         help_exit (argv[0], 0);
150       else if (! strcmp (argv[i], "--version"))
151         {
152           printf ("mdate (m17n library) %s\n", VERSION);
153           printf ("Copyright (C) 2003 AIST, JAPAN\n");
154           exit (0);
155         }
156       else
157         help_exit (argv[0], 1);
158     }
159
160
161   /* Initialize the m17n library.  */
162   M17N_INIT ();
163   if (merror_code != MERROR_NONE)
164     FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
165
166   /* Get a local list in LOCALE_LIST, and generate an M-text that
167      contains date string in each locale.  */
168   locale_list = list_system_locales ();
169   mt = mtext ();
170   plist = locale_list;
171   while (mplist_key (plist) != Mnil)
172     {
173       char *name = msymbol_name (mplist_key (plist));
174       char fmtbuf[256];
175       int len;
176       MLocale *locale = mplist_value (plist);
177       MSymbol coding = mlocale_get_prop (locale, Mcoding);
178       /* One line text for this locale.  The format is:
179          "LOCALE-NAME:  DATE-AND-TIME-STRING".  */
180       MText *thisline;
181
182       sprintf (fmtbuf, "%16s: ", name);
183       len = strlen (fmtbuf);
184       thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
185       if (thisline)
186         {
187           mlocale_set (LC_TIME, name);
188           if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
189             {
190               mtext_cat_char (thisline, '\n');
191               mtext_cat (mt, thisline);
192             }
193           m17n_object_unref (thisline);
194         }
195       plist = mplist_next (plist);
196     }
197
198   /* Show the generated M-text by another example program "mview".  */
199   {
200     FILE *p = popen ("mview", "w");
201
202     if (!p)
203       FATAL_ERROR ("%s\n", "Can't run the program mview!");
204     mconv_encode_stream (Mcoding_utf_8, mt, p);
205     fclose (p);
206   }
207
208   /* Clear away.  */
209   m17n_object_unref (locale_list);
210   m17n_object_unref (mt);
211   M17N_FINI ();
212
213   exit (0);
214 }
215 #endif /* not FOR_DOXYGEN */