Gammu internals  1.38.0
misc.h File Reference
#include <stdio.h>
#include <time.h>
#include <gammu-config.h>
#include <gammu-types.h>
#include <gammu-datetime.h>
#include <gammu-misc.h>
#include <gammu-debug.h>
Include dependency graph for misc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GSM_CutLines
 

Macros

#define GAMMU_URL   "<http://www.gammu.org>"
 
#define BACKUP_MAIN_HEADER   "; This file format was designed for Gammu and is compatible with Gammu+"
 
#define BACKUP_INFO_HEADER   "; See " GAMMU_URL " for more info"
 
#define socket_invalid   (-1)
 

Typedefs

typedef int socket_type
 

Functions

void SplitLines (const char *message, const size_t messagesize, GSM_CutLines *lines, const char *whitespaces, const size_t spaceslen, const char *quotes, const size_t quoteslen, const gboolean eot)
 
const char * GetLineString (const char *message, GSM_CutLines *lines, int start)
 
int GetLineLength (const char *message, const GSM_CutLines *lines, int start)
 
void InitLines (GSM_CutLines *lines)
 
void FreeLines (GSM_CutLines *lines)
 
void CopyLineString (char *dest, const char *src, const GSM_CutLines *lines, int start)
 
void GetTimeDifference (unsigned long diff, GSM_DateTime *DT, gboolean Plus, int multi)
 
GSM_DateTime GSM_AddTime (GSM_DateTime DT, GSM_DeltaTime delta)
 
int GetDayOfYear (unsigned int year, unsigned int month, unsigned int day)
 
int GetWeekOfMonth (unsigned int year, unsigned int month, unsigned int day)
 
int GetDayOfWeek (unsigned int year, unsigned int month, unsigned int day)
 
void StripSpaces (char *buff)
 

Macro Definition Documentation

§ BACKUP_INFO_HEADER

#define BACKUP_INFO_HEADER   "; See " GAMMU_URL " for more info"

Definition at line 12 of file misc.h.

§ BACKUP_MAIN_HEADER

#define BACKUP_MAIN_HEADER   "; This file format was designed for Gammu and is compatible with Gammu+"

Definition at line 11 of file misc.h.

§ GAMMU_URL

#define GAMMU_URL   "<http://www.gammu.org>"

Definition at line 10 of file misc.h.

§ socket_invalid

#define socket_invalid   (-1)

Definition at line 115 of file misc.h.

Typedef Documentation

§ socket_type

typedef int socket_type

Definition at line 114 of file misc.h.

Function Documentation

§ CopyLineString()

void CopyLineString ( char *  dest,
const char *  src,
const GSM_CutLines lines,
int  start 
)

Copies line to variable.

Definition at line 522 of file misc.c.

References GetLineLength(), and GetLineStringPos().

523 {
524  int len;
525  const char *pos;
526 
527  len = GetLineLength(src, lines, start);
528  pos = GetLineStringPos(src, lines, start);
529  if (pos == NULL) {
530  dest[0] = '\0';
531  return;
532  }
533  memcpy(dest, pos, len);
534  dest[len] = '\0';
535 }
int GetLineLength(const char *message UNUSED, const GSM_CutLines *lines, int start)
Definition: misc.c:517
const char * GetLineStringPos(const char *message, const GSM_CutLines *lines, int start)
Definition: misc.c:483

§ FreeLines()

void FreeLines ( GSM_CutLines lines)

Frees line cut structure.

Definition at line 392 of file misc.c.

References GSM_CutLines::allocated, GSM_CutLines::numbers, and GSM_CutLines::retval.

Referenced by NOKIA_EncodeDateTime().

393 {
394  free(lines->numbers);
395  lines->numbers = NULL;
396  lines->allocated = 0;
397  free(lines->retval);
398  lines->retval = NULL;
399 }
size_t * numbers
Definition: misc.h:38
size_t allocated
Definition: misc.h:42
char * retval
Definition: misc.h:46

§ GetLineLength()

int GetLineLength ( const char *  message,
const GSM_CutLines lines,
int  start 
)

Returns line length.

Parameters
messageParsed message.
linesParsed lines information.
startWhich line we want.

§ GetLineString()

const char* GetLineString ( const char *  message,
GSM_CutLines lines,
int  start 
)

Returns pointer to static buffer containing line.

Parameters
messageParsed message.
linesParsed lines information.
startWhich line we want.

Definition at line 492 of file misc.c.

References dbgprintf, GetLineLength(), GetLineStringPos(), and GSM_CutLines::retval.

Referenced by NOKIA_EncodeDateTime().

493 {
494  int len=0;
495  const char *pos;
496 
497  pos = GetLineStringPos(message, lines, start);
498  if (pos == NULL) {
499  return NULL;
500  }
501 
502  len = GetLineLength(message, lines, start);
503 
504  lines->retval = (char *)realloc(lines->retval, len + 1);
505  if (lines->retval == NULL) {
506  dbgprintf(NULL, "Allocation failed!\n");
507  return NULL;
508  }
509 
510  memcpy(lines->retval, pos, len);
511 
512  lines->retval[len] = '\0';
513 
514  return lines->retval;
515 }
int GetLineLength(const char *message UNUSED, const GSM_CutLines *lines, int start)
Definition: misc.c:517
const char * GetLineStringPos(const char *message, const GSM_CutLines *lines, int start)
Definition: misc.c:483
#define dbgprintf
Definition: debug.h:72
char * retval
Definition: misc.h:46

§ GetTimeDifference()

void GetTimeDifference ( unsigned long  diff,
GSM_DateTime DT,
gboolean  Plus,
int  multi 
)

Definition at line 247 of file misc.c.

References GSM_DateTime::Day, dbgprintf, Fill_GSM_DateTime(), Fill_Time_T(), GSM_DateTime::Hour, GSM_DateTime::Minute, GSM_DateTime::Month, GSM_DateTime::Second, and GSM_DateTime::Year.

Referenced by GSM_GetCalendarRecurranceRepeat(), and NOKIA_EncodeDateTime().

248 {
249  time_t t_time;
250 
251  t_time = Fill_Time_T(*DT);
252 
253  if (Plus) {
254  t_time += diff*multi;
255  } else {
256  t_time -= diff*multi;
257  }
258 
259  Fill_GSM_DateTime(DT, t_time);
260  dbgprintf(NULL, "EndTime: %02i-%02i-%04i %02i:%02i:%02i\n",
261  DT->Day,DT->Month,DT->Year,DT->Hour,DT->Minute,DT->Second);
262 }
void Fill_GSM_DateTime(GSM_DateTime *Date, time_t timet)
Definition: misc.c:170
#define dbgprintf
Definition: debug.h:72
time_t Fill_Time_T(GSM_DateTime DT)
Definition: misc.c:189

§ GSM_AddTime()

GSM_DateTime GSM_AddTime ( GSM_DateTime  DT,
GSM_DeltaTime  delta 
)

Definition at line 223 of file misc.c.

References GSM_DateTime::Day, GSM_DeltaTime::Day, Fill_GSM_DateTime(), GSM_DateTime::Hour, GSM_DeltaTime::Hour, GSM_DateTime::Minute, GSM_DeltaTime::Minute, GSM_DateTime::Month, GSM_DateTime::Second, GSM_DeltaTime::Second, and GSM_DateTime::Year.

Referenced by GSM_Calendar_AdjustDate(), GSM_DecodeVCALENDAR_VTODO(), and GSM_ToDo_AdjustDate().

224 {
225  struct tm tm_time;
226  time_t t_time;
227  GSM_DateTime Date;
228 
229  memset(&tm_time, 0, sizeof(tm_time));
230  tm_time.tm_year = DT.Year - 1900;
231  tm_time.tm_mon = DT.Month - 1;
232  tm_time.tm_mday = DT.Day;
233  tm_time.tm_hour = DT.Hour;
234  tm_time.tm_min = DT.Minute;
235  tm_time.tm_sec = DT.Second;
236  tm_time.tm_isdst = -1;
237 
238  /* TODO: This works only for dates after 1970. But birthday dates may be before, so a more general
239  method than mktime /localtime should be used. */
240  t_time = mktime (&tm_time);
241  t_time = t_time + delta.Second + 60* (delta.Minute + 60* (delta.Hour + 24*delta.Day));
242 
243  Fill_GSM_DateTime ( &Date, t_time);
244  return Date;
245 }
void Fill_GSM_DateTime(GSM_DateTime *Date, time_t timet)
Definition: misc.c:170

§ InitLines()

void InitLines ( GSM_CutLines lines)

Initializes line cut structure.

Definition at line 385 of file misc.c.

References GSM_CutLines::allocated, GSM_CutLines::numbers, and GSM_CutLines::retval.

Referenced by NOKIA_EncodeDateTime().

386 {
387  lines->numbers = NULL;
388  lines->allocated = 0;
389  lines->retval = NULL;
390 }
size_t * numbers
Definition: misc.h:38
size_t allocated
Definition: misc.h:42
char * retval
Definition: misc.h:46

§ SplitLines()

void SplitLines ( const char *  message,
const size_t  messagesize,
GSM_CutLines lines,
const char *  whitespaces,
const size_t  spaceslen,
const char *  quotes,
const size_t  quoteslen,
const gboolean  eot 
)

Calculates string cut points to split it to lines.

Definition at line 401 of file misc.c.

References GSM_CutLines::allocated, FALSE, GSM_CutLines::numbers, and TRUE.

Referenced by NOKIA_EncodeDateTime().

405 {
406  size_t i=0,number=0,j=0, lastquote = 0;
407  gboolean whitespace = TRUE, nowwhite = FALSE, insidequotes = FALSE;
408 
409  /* Clean current lines */
410  for (i = 0; i < lines->allocated; i++) {
411  lines->numbers[i] = 0;
412  }
413 
414  /* Go through message */
415  for (i = 0; i < messagesize; i++) {
416  /* Reallocate buffer if needed */
417  if (number + 2 >= lines->allocated) {
418  lines->allocated += 20;
419  lines->numbers = (size_t *)realloc(lines->numbers, lines->allocated * sizeof(size_t));
420  if (lines->numbers == NULL) {
421  return;
422  }
423  for (j = lines->allocated - 20; j < lines->allocated; j++) {
424  lines->numbers[j] = 0;
425  }
426  }
427 
428  nowwhite = FALSE;
429 
430  /* Check for quotes */
431  for (j = 0; j < quoteslen; j++) {
432  if (quotes[j] == message[i]) {
433  insidequotes = !(insidequotes);
434  lastquote = i;
435  break;
436  }
437  }
438 
439  /* Find matching quote */
440  if (insidequotes) {
441  continue;
442  }
443 
444 rollback_quote:
445  /* Check for whitespace */
446  for (j = 0; j < spaceslen; j++) {
447  if (whitespaces[j] == message[i]) {
448  nowwhite = TRUE;
449  break;
450  }
451  }
452 
453  /* Split line if there is change from whitespace to non whitespace */
454  if (whitespace) {
455  if (!nowwhite) {
456  lines->numbers[number] = i;
457  number++;
458  whitespace = FALSE;
459  }
460  } else {
461  if (nowwhite) {
462  lines->numbers[number] = i;
463  number++;
464  whitespace = TRUE;
465  }
466 
467  }
468  }
469 
470  /* Check for unterminated quotes and roll back to ignore them */
471  if (number % 2 == 1 && insidequotes) {
472  insidequotes = FALSE;
473  i = lastquote;
474  goto rollback_quote;
475  }
476 
477  /* Store possible end if there was not just whitespace */
478  if (eot && !whitespace) {
479  lines->numbers[number] = messagesize;
480  }
481 }
size_t * numbers
Definition: misc.h:38
size_t allocated
Definition: misc.h:42
int gboolean
Definition: gammu-types.h:23
#define FALSE
Definition: gammu-types.h:25
#define TRUE
Definition: gammu-types.h:28

§ StripSpaces()

void StripSpaces ( char *  buff)

Strips spaces from string.

Parameters
[in,out]buffString where to strip buffers.

Definition at line 713 of file misc.c.

Referenced by GSM_InitConnection_Log().

714 {
715  ssize_t i = 0;
716 
717  while(isspace(buff[i])) {
718  i++;
719  }
720  if (i > 0) {
721  memmove(buff, buff + i, strlen(buff + i));
722  }
723  i = strlen(buff) - 1;
724  while(isspace(buff[i]) && i >= 0) {
725  buff[i] = 0;
726  i--;
727  }
728 
729 }