(mflt_enable_new_feature): Document it.
[m17n/m17n-lib.git] / example / mdate.c
1 /* mdate.c -- Show system date/time in all locales.     -*- coding: euc-jp; -*-
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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., 51 Franklin Street, Fifth Floor,
21    02111-1307, USA.  */
22
23 /***en
24     @enpage m17n-date display date and time
25
26     @section m17n-date-synopsis SYNOPSIS
27
28     m17n-date [ OPTION ... ]
29
30     @section m17n-date-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 /***ja
48     @japage m17n-date Æü»þ¤òɽ¼¨¤¹¤ë
49
50     @section m17n-date-synopsis ¥·¥Î¥×¥·¥¹
51
52     m17n-date [ OPTION ... ]
53
54     @section m17n-date-description ÀâÌÀ
55
56     ¥·¥¹¥Æ¥à¤ÎÆü»þ¤ò¤µ¤Þ¤¶¤Þ¤Ê¥í¥±¡¼¥ë¤Ç¥¦¥£¥ó¥É¥¦¤Ëɽ¼¨¤¹¤ë¡£ 
57
58     °Ê²¼¤Î¥ª¥×¥·¥ç¥ó¤¬ÍøÍѤǤ­¤ë¡£ 
59
60     <ul>
61
62     <li> --version
63
64     ¥Ð¡¼¥¸¥ç¥óÈÖ¹æ¤òɽ¼¨¤¹¤ë¡£ 
65
66     <li> -h, --help
67
68     ¤³¤Î¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë¡£
69
70     </ul>
71 */
72
73 #ifndef FOR_DOXYGEN
74
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <time.h>
79 #include <sys/time.h>
80 #include <locale.h>
81
82 #include <m17n.h>
83 #include <m17n-misc.h>
84
85 /* Return a plist of all locales currently avairable on the system.
86    The keys and values of the plist are locale name symbols and
87    pointers to MLocale respectively.  */
88
89 MPlist *
90 list_system_locales ()
91 {
92   FILE *p;
93   char buf[1024];
94   MPlist *plist;
95
96   /* Run the command "locale -a" to get a list of locales.  */
97   if (! (p = popen ("locale -a", "r")))
98     {
99       fprintf (stderr, "Can't run `locale -a'.\n");
100       exit (1);
101     }
102
103   plist = mplist ();
104   /* Read from the pipe one line by one.  */
105   while (fgets (buf, 1024, p))
106     {
107       MLocale *locale;
108       int len = strlen (buf);
109
110       if (buf[len - 1] == '\n')
111         buf[len - 1] = '\0';
112       locale = mlocale_set (LC_TIME, buf);
113
114       /* If the locale is surely usable, it is not duplicated, and we
115          know the corresponding coding system, add it to the list.  */
116       if (locale
117           && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
118           && mlocale_get_prop (locale, Mcoding) != Mnil)
119         mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
120     }
121   pclose (p);
122   return plist;
123 }
124
125
126 /* Print the usage of this program (the name is PROG), and exit with
127    EXIT_CODE.  */
128
129 void
130 help_exit (char *prog, int exit_code)
131 {
132   char *p = prog;
133
134   while (*p)
135     if (*p++ == '/')
136       prog = p;
137
138   printf ("Usage: %s [ OPTION ...]\n", prog);
139   printf ("Display the system date and time in many locales on a window.\n");
140   printf ("The following OPTIONs are available.\n");
141   printf ("  %-13s %s", "--version", "Print version number.\n");
142   printf ("  %-13s %s", "-h, --help", "Print this message.\n");
143   exit (exit_code);
144 }
145
146
147 /* Format MSG by FMT and print the result to the stderr, and exit.  */
148
149 #define FATAL_ERROR(fmt, arg)   \
150   do {                          \
151     fprintf (stderr, fmt, arg); \
152     exit (1);                   \
153   } while (0)
154
155
156 int
157 main (int argc, char **argv)
158 {
159   time_t current_time_t = time (NULL);
160   struct tm *current_time_tm = localtime (&current_time_t);
161   /* List of all locales.  */
162   MPlist *locale_list, *plist;
163   /* Text to be shown.  */
164   MText *mt;
165   int i;
166
167   for (i = 1; i < argc; i++)
168     {
169       if (! strcmp (argv[i], "--help")
170                || ! strcmp (argv[i], "-h")
171                || ! strcmp (argv[i], "-?"))
172         help_exit (argv[0], 0);
173       else if (! strcmp (argv[i], "--version"))
174         {
175           printf ("m17n-date (m17n library) %s\n", M17NLIB_VERSION_NAME);
176           printf ("Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 AIST, JAPAN\n");
177           exit (0);
178         }
179       else
180         help_exit (argv[0], 1);
181     }
182
183
184   /* Initialize the m17n library.  */
185   M17N_INIT ();
186   if (merror_code != MERROR_NONE)
187     FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
188
189   /* Get a locale list in LOCALE_LIST, and generate an M-text that
190      contains date string in each locale.  */
191   locale_list = list_system_locales ();
192   mt = mtext ();
193   plist = locale_list;
194   while (mplist_key (plist) != Mnil)
195     {
196       char *name = msymbol_name (mplist_key (plist));
197       char fmtbuf[256];
198       int len;
199       MLocale *locale = mplist_value (plist);
200       MSymbol coding = mlocale_get_prop (locale, Mcoding);
201       /* One line text for this locale.  The format is:
202          "LOCALE-NAME:  DATE-AND-TIME-STRING".  */
203       MText *thisline;
204
205       sprintf (fmtbuf, "%16s: ", name);
206       len = strlen (fmtbuf);
207       thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
208       if (thisline)
209         {
210           mlocale_set (LC_TIME, name);
211           if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
212             {
213               mtext_cat_char (thisline, '\n');
214               mtext_cat (mt, thisline);
215             }
216           m17n_object_unref (thisline);
217         }
218       plist = mplist_next (plist);
219     }
220
221   /* Show the generated M-text by another example program "mview".  */
222   {
223     FILE *p = popen ("m17n-view", "w");
224
225     if (!p)
226       FATAL_ERROR ("%s\n", "Can't run the program mview!");
227     mconv_encode_stream (Mcoding_utf_8, mt, p);
228     fclose (p);
229   }
230
231   /* Clear away.  */
232   m17n_object_unref (locale_list);
233   m17n_object_unref (mt);
234   M17N_FINI ();
235
236   exit (0);
237 }
238 #endif /* not FOR_DOXYGEN */