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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* mdate.c -- Show system date/time in all locales. -*- coding: euc-jp; -*-
Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
National Institute of Advanced Industrial Science and Technology (AIST)
Registration Number H15PRO112
This file is part of the m17n library.
The m17n library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
The m17n library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the m17n library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
02111-1307, USA. */
/***en
@enpage m17n-date display date and time
@section m17n-date-synopsis SYNOPSIS
m17n-date [ OPTION ... ]
@section m17n-date-description DESCRIPTION
Display the system date and time in many locales on a window.
The following OPTIONs are available.
<ul>
<li> --version
Print version number.
<li> -h, --help
Print this message.
</ul>
*/
/***ja
@japage m17n-date ɽ
@section m17n-date-synopsis Υץ
m17n-date [ OPTION ... ]
@section m17n-date-description
ƥޤޤʥǥɥɽ롣
ʲΥץѤǤ롣
<ul>
<li> --version
Сֹɽ롣
<li> -h, --help
Υåɽ롣
</ul>
*/
#ifndef FOR_DOXYGEN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <locale.h>
#include <m17n.h>
#include <m17n-misc.h>
/* Return a plist of all locales currently avairable on the system.
The keys and values of the plist are locale name symbols and
pointers to MLocale respectively. */
MPlist *
list_system_locales ()
{
FILE *p;
char buf[1024];
MPlist *plist;
/* Run the command "locale -a" to get a list of locales. */
if (! (p = popen ("locale -a", "r")))
{
fprintf (stderr, "Can't run `locale -a'.\n");
exit (1);
}
plist = mplist ();
/* Read from the pipe one line by one. */
while (fgets (buf, 1024, p))
{
MLocale *locale;
int len = strlen (buf);
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
locale = mlocale_set (LC_TIME, buf);
/* If the locale is surely usable, it is not duplicated, and we
know the corresponding coding system, add it to the list. */
if (locale
&& ! mplist_get (plist, mlocale_get_prop (locale, Mname))
&& mlocale_get_prop (locale, Mcoding) != Mnil)
mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
}
pclose (p);
return plist;
}
/* Print the usage of this program (the name is PROG), and exit with
EXIT_CODE. */
void
help_exit (char *prog, int exit_code)
{
char *p = prog;
while (*p)
if (*p++ == '/')
prog = p;
printf ("Usage: %s [ OPTION ...]\n", prog);
printf ("Display the system date and time in many locales on a window.\n");
printf ("The following OPTIONs are available.\n");
printf (" %-13s %s", "--version", "Print version number.\n");
printf (" %-13s %s", "-h, --help", "Print this message.\n");
exit (exit_code);
}
/* Format MSG by FMT and print the result to the stderr, and exit. */
#define FATAL_ERROR(fmt, arg) \
do { \
fprintf (stderr, fmt, arg); \
exit (1); \
} while (0)
int
main (int argc, char **argv)
{
time_t current_time_t = time (NULL);
struct tm *current_time_tm = localtime (¤t_time_t);
/* List of all locales. */
MPlist *locale_list, *plist;
/* Text to be shown. */
MText *mt;
int i;
for (i = 1; i < argc; i++)
{
if (! strcmp (argv[i], "--help")
|| ! strcmp (argv[i], "-h")
|| ! strcmp (argv[i], "-?"))
help_exit (argv[0], 0);
else if (! strcmp (argv[i], "--version"))
{
printf ("m17n-date (m17n library) %s\n", M17NLIB_VERSION_NAME);
printf ("Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 AIST, JAPAN\n");
exit (0);
}
else
help_exit (argv[0], 1);
}
/* Initialize the m17n library. */
M17N_INIT ();
if (merror_code != MERROR_NONE)
FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
/* Get a locale list in LOCALE_LIST, and generate an M-text that
contains date string in each locale. */
locale_list = list_system_locales ();
mt = mtext ();
plist = locale_list;
while (mplist_key (plist) != Mnil)
{
char *name = msymbol_name (mplist_key (plist));
char fmtbuf[256];
int len;
MLocale *locale = mplist_value (plist);
MSymbol coding = mlocale_get_prop (locale, Mcoding);
/* One line text for this locale. The format is:
"LOCALE-NAME: DATE-AND-TIME-STRING". */
MText *thisline;
sprintf (fmtbuf, "%16s: ", name);
len = strlen (fmtbuf);
thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
if (thisline)
{
mlocale_set (LC_TIME, name);
if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
{
mtext_cat_char (thisline, '\n');
mtext_cat (mt, thisline);
}
m17n_object_unref (thisline);
}
plist = mplist_next (plist);
}
/* Show the generated M-text by another example program "mview". */
{
FILE *p = popen ("m17n-view", "w");
if (!p)
FATAL_ERROR ("%s\n", "Can't run the program mview!");
mconv_encode_stream (Mcoding_utf_8, mt, p);
fclose (p);
}
/* Clear away. */
m17n_object_unref (locale_list);
m17n_object_unref (mt);
M17N_FINI ();
exit (0);
}
#endif /* not FOR_DOXYGEN */
|