• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • data
  • tools
binfile.h
Go to the documentation of this file.
1 /***************************************************************************
2  binfile.h - Definitions useful for handling KStars binary data files
3  -------------------
4  begin : Sun 27 Jul 2008
5  copyright : (C) 2008 by Akarsh Simha
6  email : akarshsimha@gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 /* NOTE: This file is a NON portable C file that need not be built for
19  KStars to be built. This file is useful only to generate binary
20  data files and ensure that they comply with KStars' format, and
21  hence need not be cross platform.
22 
23  This file does not use Qt or KDE libraries as it is just provided
24  here as a tool to build and test data files for KStars
25 
26  Hence, we shall hide this file from the watchful eyes of Krazy
27 */
28 //krazy:skip
29 
30 #ifndef BINFILE_H
31 #define BINFILE_H
32 
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include "byteorder.h"
39 
40 /* NOTE: HTM_LEVEL and other HTM-related stuff must be defined before using this header */
41 
42 // Bogus Define
43 #ifndef INDEX_ENTRY_SIZE
44 #define INDEX_ENTRY_SIZE 12
45 #endif
46 
47 /*
48  * enum listing out various possible data types
49  */
50 
51 enum dataType {
52  DT_CHAR, /* Character */
53  DT_INT8, /* 8-bit Integer */
54  DT_UINT8, /* 8-bit Unsigned Integer */
55  DT_INT16, /* 16-bit Integer */
56  DT_UINT16, /* 16-bit Unsigned Integer */
57  DT_INT32, /* 32-bit Integer */
58  DT_UINT32, /* 32-bit Unsigned Integer */
59  DT_CHARV, /* Fixed-length array of characters */
60  DT_STR, /* Variable length array of characters, either terminated by NULL or by the limit on field size */
61  DT_SPCL = 128 /* Flag indicating that the field requires special treatment (eg: Different bits may mean different things) */
62 };
63 
64 /*
65  * struct to store the description of a field / data element in the binary files
66  */
67 
68 typedef struct dataElement {
69  char name[10];
70  int8_t size;
71  u_int8_t type;
72  int32_t scale;
73 } dataElement;
74 
75 void charv2str(char *str, char *charv, int n) {
76  int i;
77  for(i = 0; i < n; ++i) {
78  *str = *charv;
79  str++;
80  charv++;
81  }
82  *str = '\0';
83 }
84 
85 int displayDataElementDescription(dataElement *e) {
86  char str[11];
87  charv2str(str, e -> name, 10);
88  printf("\nData Field:\n");
89  printf(" Name: %s\n", str);
90  printf(" Size: %d\n", e -> size);
91  printf(" Type: %d\n", e -> type);
92  printf(" Scale: %ld\n", e -> scale);
93 }
94 
95 // NOTE: Ineffecient. Not to be used for high-productivity
96 // applications
97 void swapbytes(char byteswap, void *ptr, int nbytes) {
98 
99  char *destptr;
100  char *i;
101 
102  if( !byteswap )
103  return;
104 
105  destptr = (char *)malloc(nbytes);
106  i = ((char *)ptr + (nbytes - 1));
107  while( i >= (char *)ptr ) {
108  *destptr = *i;
109  ++destptr;
110  --i;
111  }
112 
113  destptr -= nbytes;
114 
115  memcpy(ptr, (void *)destptr, nbytes);
116  free(destptr);
117 
118 }
119 
120 
121 u_int32_t trixel2number(char *trixel) {
122  int index;
123  u_int32_t id = 0;
124  for( index = HTM_LEVEL + 1; index >= 1; --index ) {
125  id += (trixel[ index ] - '0') * (u_int16_t)round( pow(4, (HTM_LEVEL + 1 - index)) );
126  }
127  id += ( ( trixel[0] == 'S' ) ? round( pow(4, HTM_LEVEL + 1) ) + 1 : 0 );
128  return id;
129 }
130 
131 char *number2trixel(char *trixel, u_int16_t number) {
132 
133  int index;
134  u_int16_t hpv = (u_int16_t)round( pow(4, HTM_LEVEL) ) * 2;
135  if( number >= hpv ) {
136  trixel[ 0 ] = 'S';
137  number -= hpv;
138  }
139  else
140  trixel[ 0 ] = 'N';
141  hpv /= 2;
142 
143  for( index = 1; index < HTM_LEVEL + 2; ++index ) {
144  trixel[ index ] = (number - (number % hpv)) / hpv + '0';
145  number = number % hpv;
146  hpv /= 4;
147  }
148 
149  trixel[ HTM_LEVEL + 2 ] = '\0';
150 
151  return trixel;
152 }
153 
154 
155 /*
156  * Convert a string to an int32_t with a double as an intermediate
157  * i : A pointer to the target int32_t
158  * str : A pointer to the string that carries the data
159  * ndec : Number of decimal places to truncate to
160  */
161 
162 int str2int32(int32_t *i, const char *str, int ndec) {
163 
164  double dbl;
165 
166  if(i == NULL)
167  return 0;
168 
169  dbl = atof(str);
170 
171  *i = (int32_t)(round(dbl * pow(10, ndec)));
172 
173  return 1;
174 
175 }
176 
177 /*
178  * Convert a string to an int16_t with a double as an intermediate
179  * i : A pointer to the target int16_t
180  * str : The string that carries the data
181  * ndec : Number of decimal places to truncate to
182  */
183 
184 int str2int16(int16_t *i, const char *str, int ndec) {
185 
186  double dbl;
187 
188  if(i == NULL || str == NULL)
189  return 0;
190 
191  dbl = atof(str);
192 
193  *i = (int16_t)(round(dbl * pow(10, ndec)));
194 
195  return 1;
196 }
197 
198 /*
199  * Convert a string into a character array for n characters
200  * a : The target array
201  * str : The string that carries the data
202  * n : Number of characters to convert
203  */
204 
205 int str2charv(char *a, const char *str, int n) {
206 
207  int i, ret;
208 
209  if(a == NULL || str == NULL)
210  return 0;
211 
212  ret = 1;
213 
214  for(i = 0; i < n; ++i) {
215  a[i] = ((ret < 0)? '\0' : str[i]);
216  if(str[i] == '\0') /* We can do this safely because we aren't storing binary data in the DB */
217  ret = -1;
218  }
219 
220  return ret;
221 }
222 
223 /*
224  * Check whether the string passed is blank
225  * str : String to check
226  * returns 1 if the string is blank, 0 otherwise.
227  */
228 
229 int isblank(char *str) {
230 
231  if(str == NULL)
232  return 1;
233 
234  while(*str != '\0') {
235  if(*str != ' ' && *str != '\n' && *str != '\r' && *str != '\t')
236  return 0;
237  ++str;
238  }
239 
240  return 1;
241 }
242 
243 /*
244  * Write one data element description into a binary file header.
245  *
246  * f : Handle of file to write into
247  * name : Name of the field, as a string. Max as specified in struct dataElement
248  * size : Size (in bytes) of the field
249  * type : Type of the field, as specified in enum dataType
250  * scale : Scale factor used for conversion of fixed-point reals to integers. N/A to DT_CHARV, DT_STR and DT_CHAR
251  */
252 
253 int writeDataElementDescription(FILE *f, char *name, int8_t size, enum dataType type, int32_t scale) {
254  struct dataElement de;
255  if(f == NULL || name == NULL)
256  return 0;
257  str2charv(de.name, name, 10);
258  de.size = size;
259  de.type = type;
260  de.scale = scale;
261  fwrite(&de, sizeof(struct dataElement), 1, f);
262  return 1;
263 }
264 
265 int writeIndexEntry(FILE *hf, u_int32_t trixel_id, u_int32_t offset, u_int32_t nrec) {
266 
267  if(hf == NULL)
268  return 0;
269 
270  fwrite(&trixel_id, 4, 1, hf);
271  fwrite(&offset, 4, 1, hf);
272  fwrite(&nrec, 4, 1, hf);
273 
274  /* Put this just for safety, in case we change our mind - we should avoid screwing things up */
275  if(4 + 4 + 4 != INDEX_ENTRY_SIZE) {
276  fprintf(stderr, "CODE ERROR: 4 + 4 + 4 != INDEX_ENTRY_SIZE\n");
277  }
278 
279  return 1;
280 }
281 
282 /*
283  * "Increments" a trixel
284  *
285  * str : String to hold the incremented trixel
286  */
287 /*
288 void nextTrixel(char *trixel) {
289 
290  char *ptr = trixel + HTM_LEVEL + 1;
291  while(ptr > trixel) {
292  *ptr = *ptr + 1;
293  if(*ptr != '4')
294  break;
295  *ptr = '0';
296  ptr--;
297  }
298  if(*ptr == 'N')
299  *ptr = 'S';
300  else if(*ptr == 'S')
301  *ptr = '0';
302 }
303 
304 */
305 #endif
DT_INT8
Definition: binfile.h:53
number2trixel
char * number2trixel(char *trixel, u_int16_t number)
Definition: binfile.h:131
str2charv
int str2charv(char *a, const char *str, int n)
Definition: binfile.h:205
swapbytes
void swapbytes(char byteswap, void *ptr, int nbytes)
Definition: binfile.h:97
charv2str
void charv2str(char *str, char *charv, int n)
Definition: binfile.h:75
dataElement
struct dataElement dataElement
DT_STR
Definition: binfile.h:60
DT_UINT16
Definition: binfile.h:56
DT_UINT32
Definition: binfile.h:58
DT_SPCL
Definition: binfile.h:61
DT_INT16
Definition: binfile.h:55
DT_INT32
Definition: binfile.h:57
NaN::f
const float f
Definition: nan.h:36
dataElement::type
u_int8_t type
Definition: binfile.h:71
writeIndexEntry
int writeIndexEntry(FILE *hf, u_int32_t trixel_id, u_int32_t offset, u_int32_t nrec)
Definition: binfile.h:265
dataElement
A structure describing a data field in the file.
Definition: binfilehelper.h:33
dataElement::scale
qint32 scale
Definition: binfilehelper.h:37
str2int32
int str2int32(int32_t *i, const char *str, int ndec)
Definition: binfile.h:162
dataElement::type
quint8 type
Definition: binfilehelper.h:36
byteorder.h
dataElement::name
char name[10]
Definition: binfilehelper.h:34
INDEX_ENTRY_SIZE
#define INDEX_ENTRY_SIZE
Definition: binfile.h:44
str2int16
int str2int16(int16_t *i, const char *str, int ndec)
Definition: binfile.h:184
dataElement::size
int8_t size
Definition: binfile.h:70
HTM_LEVEL
#define HTM_LEVEL
Definition: nomadbinfile2mysql.h:22
DT_CHAR
Definition: binfile.h:52
writeDataElementDescription
int writeDataElementDescription(FILE *f, char *name, int8_t size, enum dataType type, int32_t scale)
Definition: binfile.h:253
isblank
int isblank(char *str)
Definition: binfile.h:229
dataElement::scale
int32_t scale
Definition: binfile.h:72
displayDataElementDescription
int displayDataElementDescription(dataElement *e)
Definition: binfile.h:85
DT_UINT8
Definition: binfile.h:54
dataType
dataType
Definition: binfile.h:51
trixel2number
u_int32_t trixel2number(char *trixel)
Definition: binfile.h:121
dataElement::size
qint8 size
Definition: binfilehelper.h:35
DT_CHARV
Definition: binfile.h:59
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal