cdi_util.c 2.52 KB
Newer Older
1
#include <stdio.h>
2
#include <stdlib.h>
3

Uwe Schulzweida's avatar
Uwe Schulzweida committed
4
void cdiDecodeParam(int param, int *pnum, int *pcat, int *pdis)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
{
6
7
8
9
10
11
12
13
  unsigned uparam = (unsigned)param;
  unsigned upnum;

  *pdis = 0xff   & uparam;
  *pcat = 0xff   & uparam >> 8;
  upnum = 0xffff & uparam >> 16;
  if ( upnum > 0x7fffU ) upnum = 0x8000U - upnum;
  *pnum = (int)upnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
16
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
int cdiEncodeParam(int pnum, int pcat, int pdis)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
{
19
  unsigned uparam, upnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20

21
22
  if ( pcat < 0 || pcat > 255 ) pcat = 255;
  if ( pdis < 0 || pdis > 255 ) pdis = 255;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23

24
25
  upnum = (unsigned)pnum;
  if ( pnum < 0 ) upnum = (unsigned)(0x8000 - pnum);
26

27
  uparam = upnum << 16 | (unsigned)(pcat << 8) | (unsigned)pdis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28

29
  return ((int)uparam);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
32
33
34
35
}


void cdiDecodeDate(int date, int *year, int *month, int *day)
{

36
37
38
39
40
41
  int iyear = date / 10000;
  *year = iyear;
  int idate = abs(date - iyear * 10000),
    imonth = idate / 100;
  *month = imonth;
  *day   = idate - imonth * 100;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
43
44
45
46
}


int cdiEncodeDate(int year, int month, int day)
{
47
48
  int iyear = abs(year),
    date = iyear * 10000 + month * 100 + day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
52
53
54
55
  if ( year < 0 ) date = -date;
  return (date);
}


void cdiDecodeTime(int time, int *hour, int *minute, int *second)
{
56
57
58
59
60
61
  int ihour = time / 10000,
    itime = time - ihour * 10000,
    iminute = itime / 100;
  *hour   = ihour;
  *minute = iminute;
  *second = itime - iminute * 100;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
64
65
66
}


int cdiEncodeTime(int hour, int minute, int second)
{
67
  int time = hour*10000 + minute*100 + second;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68

69
  return time;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
}
71
72
73
74
75
76
77
78
79


void cdiParamToString(int param, char *paramstr, int maxlen)
{
  int dis, cat, num;
  int len;

  cdiDecodeParam(param, &num, &cat, &dis);

80
  size_t umaxlen = maxlen >= 0 ? (unsigned)maxlen : 0U;
81
  if ( dis == 255 && (cat == 255 || cat == 0 ) )
82
    len = snprintf(paramstr, umaxlen, "%d", num);
83
  else  if ( dis == 255 )
84
    len = snprintf(paramstr, umaxlen, "%d.%d", num, cat);
85
  else
86
    len = snprintf(paramstr, umaxlen, "%d.%d.%d", num, cat, dis);
87

88
  if ( len >= maxlen || len < 0)
89
    fprintf(stderr, "Internal problem (%s): size of input string is too small!\n", __func__);
90
}
91
92


Oliver Heidmann's avatar
Oliver Heidmann committed
93
const char *cdiUnitNamePtr(int cdi_unit)
94
{
Oliver Heidmann's avatar
Oliver Heidmann committed
95
  const char *cdiUnits[] = {
96
97
98
99
100
101
102
103
    /*  0 */  "undefined",
    /*  1 */  "Pa",
    /*  2 */  "hPa",
    /*  3 */  "mm",
    /*  4 */  "cm",
    /*  5 */  "dm",
    /*  6 */  "m",
  };
104
  enum { numUnits = (int) (sizeof(cdiUnits)/sizeof(char *)) };
Oliver Heidmann's avatar
Oliver Heidmann committed
105
  const char *name = ( cdi_unit > 0 && cdi_unit < numUnits ) ?
106
107
    cdiUnits[cdi_unit] : NULL;
  return name;
108
109
}

110
111
112
113
114
115
116
117
118
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */