00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #include <stdio.h>
00070
00071 #include "gwlib/gwlib.h"
00072
00073
00074 static Octstr *read_line(FILE *f, Octstr *buf)
00075 {
00076 Octstr *os;
00077 char cbuf[8*1024];
00078 size_t n;
00079 long pos;
00080
00081 pos = octstr_search_char(buf, '\n', 0);
00082 while (pos == -1 && (n = fread(cbuf, 1, sizeof(cbuf), f)) > 0) {
00083 octstr_append_data(buf, cbuf, n);
00084 pos = octstr_search_char(buf, '\n', 0);
00085 }
00086
00087 if (pos == -1) {
00088 pos = octstr_len(buf);
00089 if (pos == 0)
00090 return NULL;
00091 }
00092 os = octstr_copy(buf, 0, pos);
00093 octstr_delete(buf, 0, pos + 1);
00094
00095 return os;
00096 }
00097
00098
00099 static int remove_long(long *p, Octstr *os)
00100 {
00101 long pos;
00102
00103 pos = octstr_parse_long(p, os, 0, 10);
00104 if (pos == -1)
00105 return -1;
00106 octstr_delete(os, 0, pos);
00107 return 0;
00108 }
00109
00110
00111 static int remove_prefix(Octstr *os, Octstr *prefix)
00112 {
00113 if (octstr_ncompare(os, prefix, octstr_len(prefix)) != 0)
00114 return -1;
00115 octstr_delete(os, 0, octstr_len(prefix));
00116 return 0;
00117 }
00118
00119
00120 static int parse_date(struct universaltime *ut, Octstr *os)
00121 {
00122 long pos;
00123
00124 pos = 0;
00125
00126 if (remove_long(&ut->year, os) == -1 ||
00127 remove_prefix(os, octstr_imm("-")) == -1 ||
00128 remove_long(&ut->month, os) == -1 ||
00129 remove_prefix(os, octstr_imm("-")) == -1 ||
00130 remove_long(&ut->day, os) == -1 ||
00131 remove_prefix(os, octstr_imm(" ")) == -1 ||
00132 remove_long(&ut->hour, os) == -1 ||
00133 remove_prefix(os, octstr_imm(":")) == -1 ||
00134 remove_long(&ut->minute, os) == -1 ||
00135 remove_prefix(os, octstr_imm(":")) == -1 ||
00136 remove_long(&ut->second, os) == -1 ||
00137 remove_prefix(os, octstr_imm(" ")) == -1)
00138 return -1;
00139 return 0;
00140 }
00141
00142
00143 int main(void)
00144 {
00145 struct universaltime ut;
00146 Octstr *os;
00147 Octstr *buf;
00148
00149 gwlib_init();
00150 buf = octstr_create("");
00151 while ((os = read_line(stdin, buf)) != NULL) {
00152 if (parse_date(&ut, os) == -1)
00153 panic(0, "Bad line: %s", octstr_get_cstr(os));
00154 printf("%ld %s\n", date_convert_universal(&ut), octstr_get_cstr(os));
00155 octstr_destroy(os);
00156 }
00157
00158 log_set_output_level(GW_PANIC);
00159 gwlib_shutdown();
00160
00161 return 0;
00162 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.