File: fmt_text.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (140 lines) | stat: -rw-r--r-- 4,600 bytes parent folder | download
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
/*  camserv - An internet streaming picture application
 *
 *  Copyright (C) 1999  Jon Travis (jon@dsndata.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#include "fmt_text.h"
#include "config.h"

static const char *Weekdays[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
				  "Thursday", "Friday", "Saturday" };

static const char *Months[] = { "January", "February", "March", "April", "May",
				"June", "July", "August", "September", 
				"October", "November", "December" };

/*
 * cam_fmt_buf:  Format a text string using information about time, dates,
 *               and other various 'info'.
 *
 * Arguments:    fmt = Format to fulfill.  Every char is copied directly
 *                     to 'dest', unless it is a '$', in which case a following
 *                     meta-char will be replaced by filled in information
 *                     (read the source)
 *               dest = Destination to place all of the text into
 *               destlen = # of bytes allocated for dest
 */

void cam_fmt_buf( const char *fmt, char *dest, int destlen ){
  const char *cp;
  char  *destcp, *overdest;
  struct tm *loctime;
  time_t curtime;

  time( &curtime );
  loctime = localtime( &curtime );

  overdest = dest + destlen;
  for( cp = fmt, destcp = dest; *cp && destcp < overdest; cp++ ){
    if( *cp == '$' && *(cp+1))	{
      cp++;
      switch( *cp ) {
      case '$' : *destcp++ = '$';
	break;
				
      case 'H' : destcp += snprintf( destcp, overdest - destcp,
				     "%02d", loctime->tm_hour );
	break;
      case 'k' : destcp += snprintf( destcp, overdest - destcp,
				    "%2d", loctime->tm_hour );
        break;
      case 'I' : destcp += snprintf( destcp, overdest - destcp, "%02d", 
				    loctime->tm_hour > 12 ? 	
				    loctime->tm_hour - 12 :
				    loctime->tm_hour );
        break;
      case 'l' : destcp += snprintf( destcp, overdest - destcp,"%2d", 
				    loctime->tm_hour > 12 ? 	
				    loctime->tm_hour - 12 :
				    loctime->tm_hour );
        break;
      case 'M' : destcp += snprintf( destcp, overdest - destcp,"%02d",  
				     loctime->tm_min );
	break;

      case 'p' : destcp += snprintf( destcp, overdest - destcp, "%s", 
				    (loctime->tm_hour < 12 ) ? "AM" :
				    "PM" );
        break;
      case 'S' : destcp += snprintf( destcp, overdest - destcp,"%02d",  
				     loctime->tm_sec );
	break;
      case 's' : 
	destcp += snprintf( destcp, overdest - destcp, "%d",  
				     (int)curtime );
        break;
						
      case 'T' : destcp += snprintf( destcp, overdest - destcp,
				     "%02d:%02d:%02d",
				     loctime->tm_hour,
				     loctime->tm_min,
				     loctime->tm_sec );
        break;
      case 'X' : destcp += snprintf( destcp, overdest - destcp,
				     "%02d:%02d:%02d %s",
				    (loctime->tm_hour > 12 ) ? 
				    loctime->tm_hour - 12 :
				    loctime->tm_hour,
				    loctime->tm_min,
				    loctime->tm_sec,
				    (loctime->tm_hour < 12 ) ? "AM" :
				    "PM" );
        break;
      case 'a' : destcp += snprintf( destcp, overdest - destcp,"%.3s", 
				    Weekdays[ loctime->tm_wday ] );
        break;
      case 'A' : destcp += snprintf( destcp, overdest - destcp,"%s", 
				    Weekdays[ loctime->tm_wday ] );
        break;
      case 'b' : destcp += snprintf( destcp, overdest - destcp,"%.3s",
				    Months[ loctime->tm_mon ] );
        break;
      case 'B' : destcp += snprintf( destcp, overdest - destcp,"%s",
				    Months[ loctime->tm_mon ] );
        break;
      case 'c' : destcp += snprintf( destcp, overdest - destcp,
				     "%s", ctime( &curtime));
	break;
      case 'v' : destcp += snprintf( destcp, overdest - destcp,
				     "%s", VERSION );
        break;
      default :
	break;
      }
    } else {
      *destcp++ = *cp;
    }
  }
  *destcp = '\0';
}