GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dlong.c
Go to the documentation of this file.
1 #include "G3d_intern.h"
2 
3 /*---------------------------------------------------------------------------*/
4 
5 int G3d_longEncode(long *source, unsigned char *dst, int nofNums)
6 {
7  long *src, d;
8  int eltLength, nBytes;
9  unsigned char *dstStop, tmp;
10 
11  eltLength = G3D_LONG_LENGTH;
12  nBytes = 8;
13 
14  d = 1;
15 
16  while (eltLength--) {
17  dstStop = dst + nofNums;
18  src = source;
19 
20  while (dst != dstStop) {
21  tmp = ((*src++ / d) % 256);
22  if (tmp != 0)
23  nBytes = G3D_MIN(nBytes, eltLength);
24  *dst++ = tmp;
25  }
26 
27  d *= 256;
28  }
29 
30  return G3D_LONG_LENGTH - nBytes;
31 }
32 
33 /*---------------------------------------------------------------------------*/
34 
35 void
36 G3d_longDecode(unsigned char *source, long *dst, int nofNums, int longNbytes)
37 {
38  long *dest;
39  int eltLength;
40  unsigned char *srcStop;
41 
42  eltLength = longNbytes;
43 
44  source += nofNums * eltLength - 1;
45 
46  eltLength--;
47  srcStop = source - nofNums;
48  dest = dst;
49  dest += nofNums - 1;
50  while (source != srcStop) {
51  *dest = *source--;
52  if ((eltLength >= G3D_LONG_LENGTH) && (*dest != 0))
53  G3d_fatalError("G3d_longDecode: decoded long too long");
54  dest--;
55  }
56 
57  while (eltLength--) {
58  srcStop = source - nofNums;
59  dest = dst;
60  dest += nofNums - 1;
61  while (source != srcStop) {
62  *dest *= 256;
63  *dest += *source--;
64  if ((eltLength >= G3D_LONG_LENGTH) && (*dest != 0))
65  G3d_fatalError("G3d_longDecode: decoded long too long");
66  dest--;
67  }
68  }
69 }
#define G3D_MIN(a, b)
Definition: G3d_intern.h:25
#define G3D_LONG_LENGTH
Definition: G3d_intern.h:11
int G3d_longEncode(long *source, unsigned char *dst, int nofNums)
Definition: g3dlong.c:5
void G3d_longDecode(unsigned char *source, long *dst, int nofNums, int longNbytes)
Definition: g3dlong.c:36
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58