13 #include "../gsmstate.h"
16 /* Based on article in Polish PC-Kurier 8/1998 page 104
17 * Archive on http://www.pckurier.pl
19 char *DayOfWeek (int year
, int month
, int day
)
22 static char DayOfWeekChar
[10];
27 w
=(day
+(31*q
) / 12 + r
+ r
/ 4 - r
/ 100 + r
/ 400) % 7;
28 strcpy(DayOfWeekChar
,"");
30 case 0: strcpy(DayOfWeekChar
,"Sun"); break;
31 case 1: strcpy(DayOfWeekChar
,"Mon"); break;
32 case 2: strcpy(DayOfWeekChar
,"Tue"); break;
33 case 3: strcpy(DayOfWeekChar
,"Wed"); break;
34 case 4: strcpy(DayOfWeekChar
,"Thu"); break;
35 case 5: strcpy(DayOfWeekChar
,"Fri"); break;
36 case 6: strcpy(DayOfWeekChar
,"Sat"); break;
41 void Fill_GSM_DateTime(GSM_DateTime
*Date
, time_t timet
)
45 now
= localtime(&timet
);
46 Date
->Year
= now
->tm_year
;
47 Date
->Month
= now
->tm_mon
+1;
48 Date
->Day
= now
->tm_mday
;
49 Date
->Hour
= now
->tm_hour
;
50 Date
->Minute
= now
->tm_min
;
51 Date
->Second
= now
->tm_sec
;
54 void GSM_GetCurrentDateTime (GSM_DateTime
*Date
)
56 Fill_GSM_DateTime(Date
, time(NULL
));
59 if (Date
->Year
>90) Date
->Year
= Date
->Year
+1900;
60 else Date
->Year
= Date
->Year
+2000;
64 time_t Fill_Time_T(GSM_DateTime DT
, int TZ
)
66 struct tm tm_starttime
;
67 unsigned char buffer
[30];
69 dbgprintf(" StartTime : %02i-%02i-%04i %02i:%02i:%02i\n",
70 DT
.Day
,DT
.Month
,DT
.Year
,DT
.Hour
,DT
.Minute
,DT
.Second
);
73 #if defined(WIN32) || defined(__SVR4)
74 sprintf(buffer
,"TZ=PST+%i",TZ
);
77 sprintf(buffer
,"PST+%i",TZ
);
78 setenv("TZ",buffer
,1);
83 memset(&tm_starttime
, 0, sizeof(tm_starttime
));
84 tm_starttime
.tm_year
= DT
.Year
- 1900;
85 tm_starttime
.tm_mon
= DT
.Month
- 1;
86 tm_starttime
.tm_mday
= DT
.Day
;
87 tm_starttime
.tm_hour
= DT
.Hour
;
88 tm_starttime
.tm_min
= DT
.Minute
;
89 tm_starttime
.tm_sec
= DT
.Second
;
90 tm_starttime
.tm_isdst
= 0;
92 return mktime(&tm_starttime
);
95 void GetTimeDifference(unsigned long diff
, GSM_DateTime
*DT
, bool Plus
, int multi
)
99 t_time
= Fill_Time_T(*DT
,8);
102 t_time
+= diff
*multi
;
104 t_time
-= diff
*multi
;
107 Fill_GSM_DateTime(DT
, t_time
);
108 DT
->Year
= DT
->Year
+ 1900;
109 dbgprintf(" EndTime : %02i-%02i-%04i %02i:%02i:%02i\n",
110 DT
->Day
,DT
->Month
,DT
->Year
,DT
->Hour
,DT
->Minute
,DT
->Second
);
113 char *OSDateTime (GSM_DateTime dt
, bool TimeZone
)
116 static char retval
[200],retval2
[200];
120 setlocale(LC_ALL
, ".OCP");
123 /* Based on article in Polish PC-Kurier 8/1998 page 104
124 * Archive on http://www.pckurier.pl
126 p
=(14-dt
.Month
) / 12;
129 w
=(dt
.Day
+(31*q
) / 12 + r
+ r
/ 4 - r
/ 100 + r
/ 400) % 7;
131 timeptr
.tm_yday
= 0; /* FIXME */
132 timeptr
.tm_isdst
= -1; /* FIXME */
133 timeptr
.tm_year
= dt
.Year
- 1900;
134 timeptr
.tm_mon
= dt
.Month
- 1;
135 timeptr
.tm_mday
= dt
.Day
;
136 timeptr
.tm_hour
= dt
.Hour
;
137 timeptr
.tm_min
= dt
.Minute
;
138 timeptr
.tm_sec
= dt
.Second
;
141 timeptr
.tm_zone
= NULL
;
145 strftime(retval2
, 200, "%#c", &timeptr
);
147 strftime(retval2
, 200, "%c", &timeptr
);
150 if (dt
.Timezone
>= 0) {
151 sprintf(retval
," +%02i00",dt
.Timezone
);
153 sprintf(retval
," -%02i00",dt
.Timezone
);
155 strcat(retval2
,retval
);
157 /* If don't have weekday name, include it */
158 strftime(retval
, 200, "%A", &timeptr
);
159 if (strstr(retval2
,retval
)==NULL
) {
160 /* Check for abbreviated weekday */
161 strftime(retval
, 200, "%a", &timeptr
);
162 if (strstr(retval2
,retval
)==NULL
) {
163 strcat(retval2
," (");
164 strcat(retval2
,retval
);
170 setlocale(LC_ALL
, ".ACP");
176 char *OSDate (GSM_DateTime dt
)
179 static char retval
[200],retval2
[200];
183 setlocale(LC_ALL
, ".OCP");
186 /* Based on article in Polish PC-Kurier 8/1998 page 104
187 * Archive on http://www.pckurier.pl
189 p
=(14-dt
.Month
) / 12;
192 w
=(dt
.Day
+(31*q
) / 12 + r
+ r
/ 4 - r
/ 100 + r
/ 400) % 7;
194 timeptr
.tm_yday
= 0; /* FIXME */
195 timeptr
.tm_isdst
= -1; /* FIXME */
196 timeptr
.tm_year
= dt
.Year
- 1900;
197 timeptr
.tm_mon
= dt
.Month
- 1;
198 timeptr
.tm_mday
= dt
.Day
;
199 timeptr
.tm_hour
= dt
.Hour
;
200 timeptr
.tm_min
= dt
.Minute
;
201 timeptr
.tm_sec
= dt
.Second
;
204 timeptr
.tm_zone
= NULL
;
208 strftime(retval2
, 200, "%#x", &timeptr
);
210 strftime(retval2
, 200, "%x", &timeptr
);
212 /* If don't have weekday name, include it */
213 strftime(retval
, 200, "%A", &timeptr
);
214 if (strstr(retval2
,retval
)==NULL
) {
215 /* Check also for short name */
216 strftime(retval
, 200, "%a", &timeptr
);
217 if (strstr(retval2
,retval
)==NULL
) {
218 strcat(retval2
," (");
219 strcat(retval2
,retval
);
225 setlocale(LC_ALL
, ".ACP");
231 bool CheckDate(GSM_DateTime
*date
)
233 /* FIXME: This could also check if day is correct for selected month */
234 return date
->Year
!= 0 &&
235 date
->Month
>= 1 && date
->Month
<= 12 &&
236 date
->Day
>= 1 && date
->Day
<= 31;
239 bool CheckTime(GSM_DateTime
*date
)
241 return date
->Hour
<= 23 && date
->Hour
>= 0 &&
242 date
->Minute
<= 59 && date
->Minute
>= 0 &&
243 date
->Second
<= 59 && date
->Second
>= 0;
246 int GetLine(FILE *File
, char *Line
, int count
)
250 if (fgets(Line
, count
, File
)) {
251 ptr
=Line
+strlen(Line
)-1;
253 while ( (*ptr
== '\n' || *ptr
== '\r') && ptr
>=Line
) *ptr
--='\0';
261 void SplitLines(unsigned char *message
, int messagesize
, GSM_Lines
*lines
, unsigned char *whitespaces
, int spaceslen
, bool eot
)
264 bool whitespace
=true, nowwhite
;
266 for (i
=0;i
<MAX_LINES
*2;i
++) lines
->numbers
[i
]=0;
268 for (i
=0;i
<messagesize
;i
++) {
270 for (j
=0;j
<spaceslen
;j
++) {
271 if (whitespaces
[j
] == message
[i
]) {
278 lines
->numbers
[number
]=i
;
284 lines
->numbers
[number
]=i
;
291 if (eot
&& !whitespace
) lines
->numbers
[number
]=messagesize
;
294 char *GetLineString(unsigned char *message
, GSM_Lines lines
, int start
)
296 static char retval
[800];
298 memcpy(retval
,message
+ lines
.numbers
[start
*2-2],lines
.numbers
[start
*2-2+1]-lines
.numbers
[start
*2-2]);
299 retval
[lines
.numbers
[start
*2-2+1]-lines
.numbers
[start
*2-2]]=0;
304 void CopyLineString(unsigned char *dest
, unsigned char *src
, GSM_Lines lines
, int start
)
306 memcpy(dest
,GetLineString(src
, lines
, start
),strlen(GetLineString(src
, lines
, start
)));
307 dest
[strlen(GetLineString(src
, lines
, start
))] = 0;
310 Debug_Info di
= {0,NULL
,false,""};
313 int dbgprintf(const char *format
, ...)
317 static unsigned char nextline
[2000]="";
318 unsigned char buffer
[2000];
319 GSM_DateTime date_time
;
321 if (di
.dl
== DL_TEXTALL
|| di
.dl
== DL_TEXTALLDATE
) {
322 va_start(argp
, format
);
323 result
= vsprintf(buffer
, format
, argp
);
324 strcat(nextline
, buffer
);
325 if (strstr(buffer
, "\n")) {
326 if (di
.dl
== DL_TEXTALLDATE
) {
327 GSM_GetCurrentDateTime(&date_time
);
328 fprintf(di
.df
,"%s %4d/%02d/%02d %02d:%02d:%02d: %s",
329 DayOfWeek(date_time
.Year
, date_time
.Month
, date_time
.Day
),
330 date_time
.Year
, date_time
.Month
, date_time
.Day
,
331 date_time
.Hour
, date_time
.Minute
, date_time
.Second
,nextline
);
333 fprintf(di
.df
,"%s",nextline
);
335 strcpy(nextline
, "");
345 #define SMPRINTF_MAX_TIME 30
347 /* assumption: if \n is present it is always the last char,
348 * string never of the form "......\n..."
350 int smfprintf(FILE *f
, const char *format
, ...)
354 static unsigned char prevline
[2000] = "", nextline
[2000]="";
355 static unsigned int linecount
=0;
356 unsigned char buffer
[2000];
357 GSM_DateTime date_time
;
359 if (f
== NULL
) return 0;
360 va_start(argp
, format
);
361 result
= vsprintf(buffer
, format
, argp
);
362 strcat(nextline
, buffer
);
363 if (strstr(buffer
, "\n")) {
364 if (ftell(f
) < 5000000) {
365 GSM_GetCurrentDateTime(&date_time
);
367 if (di
.dl
== DL_TEXTALLDATE
|| di
.dl
== DL_TEXTERRORDATE
|| di
.dl
== DL_TEXTDATE
) {
368 fprintf(f
,"%s %4d/%02d/%02d %02d:%02d:%02d: <%i> %s",
369 DayOfWeek(date_time
.Year
, date_time
.Month
, date_time
.Day
),
370 date_time
.Year
, date_time
.Month
, date_time
.Day
,
371 date_time
.Hour
, date_time
.Minute
, date_time
.Second
,linecount
,prevline
);
373 fprintf(f
,"%s",prevline
);
377 if (di
.dl
== DL_TEXTALLDATE
|| di
.dl
== DL_TEXTERRORDATE
|| di
.dl
== DL_TEXTDATE
) {
378 fprintf(f
,"%s %4d/%02d/%02d %02d:%02d:%02d: %s",
379 DayOfWeek(date_time
.Year
, date_time
.Month
, date_time
.Day
),
380 date_time
.Year
, date_time
.Month
, date_time
.Day
,
381 date_time
.Hour
, date_time
.Minute
, date_time
.Second
,nextline
);
383 fprintf(f
,"%s",nextline
);
385 strcpy(prevline
, nextline
);
387 strcpy(nextline
, "");
394 bool GSM_SetDebugLevel(char *info
, Debug_Info
*di
)
396 if (!strcmp(info
,"nothing")) {di
->dl
= 0; return true;}
397 if (!strcmp(info
,"text")) {di
->dl
= DL_TEXT
; return true;}
398 if (!strcmp(info
,"textall")) {di
->dl
= DL_TEXTALL
; return true;}
399 if (!strcmp(info
,"binary")) {di
->dl
= DL_BINARY
; return true;}
400 if (!strcmp(info
,"errors")) {di
->dl
= DL_TEXTERROR
; return true;}
401 if (!strcmp(info
,"textdate")) {di
->dl
= DL_TEXTDATE
; return true;}
402 if (!strcmp(info
,"textalldate")) {di
->dl
= DL_TEXTALLDATE
; return true;}
403 if (!strcmp(info
,"errorsdate")) {di
->dl
= DL_TEXTERRORDATE
; return true;}
407 /* Dumps a message */
408 void DumpMessage(FILE *df
, const unsigned char *message
, int messagesize
)
411 unsigned char buffer
[200];
413 if (df
==NULL
|| messagesize
== 0) return;
417 memset(buffer
,0x20,sizeof(buffer
));
420 for (i
= 0; i
< messagesize
; i
++) {
421 sprintf(buffer
+j
*4,"%02x",message
[i
]);
422 buffer
[j
*4+2] = 0x20;
423 if (isprint(message
[i
]) && message
[i
]!=0x09) {
424 if (j
!= len
-1) buffer
[j
*4+2] = message
[i
];
425 buffer
[(len
-1)*4+j
+3] = message
[i
];
427 buffer
[(len
-1)*4+j
+3] = '.';
429 if (j
!= len
-1 && i
!= messagesize
-1) buffer
[j
*4+3] = '|';
431 smfprintf(df
,"%s\n",buffer
);
432 memset(buffer
,0x20,sizeof(buffer
));
439 if (j
!= 0) smfprintf(df
,"%s",buffer
);
443 /* How should editor hadle tabs in this file? Add editor commands here.
444 * vim: noexpandtab sw=8 ts=8 sts=8: