Kannel: Open Source WAP and SMS gateway  svn-r5335
timestamp.c File Reference
#include <stdio.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Functions

static Octstrread_line (FILE *f, Octstr *buf)
 
static int remove_long (long *p, Octstr *os)
 
static int remove_prefix (Octstr *os, Octstr *prefix)
 
static int parse_date (struct universaltime *ut, Octstr *os)
 
int main (void)
 

Function Documentation

◆ main()

int main ( void  )

Definition at line 139 of file timestamp.c.

References date_convert_universal(), GW_PANIC, gwlib_init(), gwlib_shutdown(), log_set_output_level(), octstr_create, octstr_destroy(), octstr_get_cstr, panic, parse_date(), and read_line().

140 {
141  struct universaltime ut;
142  Octstr *os;
143  Octstr *buf;
144 
145  gwlib_init();
146  buf = octstr_create("");
147  while ((os = read_line(stdin, buf)) != NULL) {
148  if (parse_date(&ut, os) == -1)
149  panic(0, "Bad line: %s", octstr_get_cstr(os));
150  printf("%ld %s\n", date_convert_universal(&ut), octstr_get_cstr(os));
151  octstr_destroy(os);
152  }
153 
155  gwlib_shutdown();
156 
157  return 0;
158 }
static int parse_date(struct universaltime *ut, Octstr *os)
Definition: timestamp.c:120
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void log_set_output_level(enum output_level level)
Definition: log.c:253
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
static Octstr * read_line(FILE *f, Octstr *buf)
Definition: timestamp.c:74
Definition: octstr.c:118
#define panic
Definition: log.h:87
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
long date_convert_universal(struct universaltime *t)
Definition: date.c:118
Definition: log.h:69

◆ parse_date()

static int parse_date ( struct universaltime ut,
Octstr os 
)
static

Definition at line 120 of file timestamp.c.

References universaltime::day, universaltime::hour, universaltime::minute, universaltime::month, octstr_imm(), remove_long(), remove_prefix(), universaltime::second, and universaltime::year.

Referenced by main().

121 {
122  if (remove_long(&ut->year, os) == -1 ||
123  remove_prefix(os, octstr_imm("-")) == -1 ||
124  remove_long(&ut->month, os) == -1 ||
125  remove_prefix(os, octstr_imm("-")) == -1 ||
126  remove_long(&ut->day, os) == -1 ||
127  remove_prefix(os, octstr_imm(" ")) == -1 ||
128  remove_long(&ut->hour, os) == -1 ||
129  remove_prefix(os, octstr_imm(":")) == -1 ||
130  remove_long(&ut->minute, os) == -1 ||
131  remove_prefix(os, octstr_imm(":")) == -1 ||
132  remove_long(&ut->second, os) == -1 ||
133  remove_prefix(os, octstr_imm(" ")) == -1)
134  return -1;
135  return 0;
136 }
long year
Definition: date.h:72
long minute
Definition: date.h:74
static int remove_prefix(Octstr *os, Octstr *prefix)
Definition: timestamp.c:111
static int remove_long(long *p, Octstr *os)
Definition: timestamp.c:99
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
long day
Definition: date.h:70
long second
Definition: date.h:75
long hour
Definition: date.h:73
long month
Definition: date.h:71

◆ read_line()

static Octstr* read_line ( FILE *  f,
Octstr buf 
)
static

Definition at line 74 of file timestamp.c.

References octstr_append_data(), octstr_copy, octstr_delete(), octstr_len(), and octstr_search_char().

Referenced by main().

75 {
76  Octstr *os;
77  char cbuf[8*1024];
78  size_t n;
79  long pos;
80 
81  pos = octstr_search_char(buf, '\n', 0);
82  while (pos == -1 && (n = fread(cbuf, 1, sizeof(cbuf), f)) > 0) {
83  octstr_append_data(buf, cbuf, n);
84  pos = octstr_search_char(buf, '\n', 0);
85  }
86 
87  if (pos == -1) {
88  pos = octstr_len(buf);
89  if (pos == 0)
90  return NULL;
91  }
92  os = octstr_copy(buf, 0, pos);
93  octstr_delete(buf, 0, pos + 1);
94 
95  return os;
96 }
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1497
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118

◆ remove_long()

static int remove_long ( long *  p,
Octstr os 
)
static

Definition at line 99 of file timestamp.c.

References octstr_delete(), and octstr_parse_long().

Referenced by parse_date().

100 {
101  long pos;
102 
103  pos = octstr_parse_long(p, os, 0, 10);
104  if (pos == -1)
105  return -1;
106  octstr_delete(os, 0, pos);
107  return 0;
108 }
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:749

◆ remove_prefix()

static int remove_prefix ( Octstr os,
Octstr prefix 
)
static

Definition at line 111 of file timestamp.c.

References octstr_delete(), octstr_len(), and octstr_ncompare().

Referenced by parse_date().

112 {
113  if (octstr_ncompare(os, prefix, octstr_len(prefix)) != 0)
114  return -1;
115  octstr_delete(os, 0, octstr_len(prefix));
116  return 0;
117 }
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
int octstr_ncompare(const Octstr *ostr1, const Octstr *ostr2, long n)
Definition: octstr.c:952
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.