pi-macros.h

Go to the documentation of this file.
00001 /*
00002  * $Id: pi-macros.h,v 1.17 2006-10-17 13:24:07 desrod Exp $
00003  *
00004  * pi-macros.h: pilot-link specific macro defintions
00005  *
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Library General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or (at
00009  * your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00014  * General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020  
00021 #ifndef _PILOT_MACROS_H_
00022 #define _PILOT_MACROS_H_
00023 
00024 #include <time.h>
00025 #include "pi-args.h"
00026 
00027 typedef unsigned long recordid_t;
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00033         extern double get_float PI_ARGS((void *));
00034         extern void set_float PI_ARGS((void *, double));
00035         extern int compareTm PI_ARGS((struct tm * a, struct tm * b));
00036 
00037 #ifdef __cplusplus
00038 }
00039 #endif
00040 #ifndef __cplusplus
00041 #define get_long(ptr) \
00042         (unsigned long) ( \
00043         (((unsigned long)((unsigned char *)(ptr))[0]) << 24) | \
00044         (((unsigned long)((unsigned char *)(ptr))[1]) << 16) | \
00045         (((unsigned long)((unsigned char *)(ptr))[2]) << 8) | \
00046         (((unsigned long)((unsigned char *)(ptr))[3]) ) )
00047 
00048 
00049 #define get_treble(ptr) ((unsigned long)\
00050                         ((((unsigned char*)(ptr))[0] << 16) | \
00051                          (((unsigned char*)(ptr))[1] << 8)  | \
00052                          (((unsigned char*)(ptr))[2])))
00053 #define get_short(ptr) ((unsigned short)\
00054                        ((((unsigned char*)(ptr))[0] << 8)  | \
00055                         (((unsigned char*)(ptr))[1])))
00056 #define get_byte(ptr) (((unsigned char*)(ptr))[0])
00057 #define get_slong(ptr) (signed long)(\
00058                                 (get_long((ptr)) > 0x7FFFFFFF) ?\
00059                                 (((signed long)(get_long((ptr)) & 0x7FFFFFFF)) - 0x80000000):\
00060                                 ((signed long)(get_long((ptr))))\
00061                                 )
00062 #define get_streble(ptr) (signed long)(\
00063                                 (get_treble((ptr)) > 0x7FFFFF) ?\
00064                                 (((signed long)(get_treble((ptr)) & 0x7FFFFF)) - 0x800000):\
00065                                 ((signed long)(get_treble((ptr))))\
00066                                 )
00067 #define get_sshort(ptr) (signed short)(\
00068                                 (get_short((ptr)) > 0x7FFF) ?\
00069                                 (((signed short)(get_short((ptr)) & 0x7FFF)) - 0x8000):\
00070                                 ((signed short)(get_short((ptr))))\
00071                                 )
00072 #define get_sbyte(ptr) (signed char)(\
00073                                 (get_byte((ptr)) > 0x7F) ?\
00074                                 (((signed char)(get_byte((ptr)) & 0x7F)) - 0x80):\
00075                                 ((signed char)(get_byte((ptr))))\
00076                                 )
00077 #define set_long(ptr,val) ((((unsigned char*)(ptr))[0] = (unsigned char)(((unsigned long)(val)) >> 24) & 0xff), \
00078                           (((unsigned char*)(ptr))[1] = (((unsigned long)(val)) >> 16) & 0xff), \
00079                           (((unsigned char*)(ptr))[2] = (((unsigned long)(val)) >> 8) & 0xff), \
00080                           (((unsigned char*)(ptr))[3] = (((unsigned long)(val)) >> 0) & 0xff))
00081 #define set_slong(ptr,val) set_long((ptr),((unsigned long)(\
00082                             (((signed long)(val)) < 0) ?\
00083                             ((unsigned long)(((signed long)(val)) + 0x80000000) | 0x80000000) :\
00084                             (val)\
00085                            )))
00086 #define set_treble(ptr,val) ((((unsigned char*)(ptr))[0] = (unsigned char)(((unsigned long)(val)) >> 16) & 0xff), \
00087                              (((unsigned char*)(ptr))[1] = (((unsigned long)(val)) >> 8) & 0xff), \
00088                              (((unsigned char*)(ptr))[2] = (((unsigned long)(val)) >> 0) & 0xff))
00089 #define set_streble(ptr,val) set_treble((ptr),((unsigned long)(\
00090                             (((signed long)(val)) < 0) ?\
00091                             ((unsigned long)(((signed long)(val)) + 0x800000) | 0x800000) :\
00092                             (val)\
00093                            )))
00094 #define set_short(ptr,val) ((((unsigned char*)(ptr))[0] = (((unsigned short)(val)) >> 8) & 0xff), \
00095                             (((unsigned char*)(ptr))[1] = (((unsigned short)(val)) >> 0) & 0xff))
00096 #define set_sshort(ptr,val) set_short((ptr),((unsigned short)(\
00097                             (((signed short)(val)) < 0) ?\
00098                             ((unsigned short)(((signed short)(val)) + 0x8000) | 0x8000) :\
00099                             (val)\
00100                            )))
00101 #define set_byte(ptr,val) (((unsigned char*)(ptr))[0]=(val))
00102 #define set_sbyte(ptr,val) set_byte((ptr),((unsigned char)(\
00103                             (((signed char)(val)) < 0) ?\
00104                             ((unsigned char)(((signed char)(val)) + 0x80) | 0x80) :\
00105                             (val)\
00106                            )))
00107 #define char4(c1,c2,c3,c4) (((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4))
00108 #else                           /*ifdef __cplusplus */
00109 inline unsigned long get_long(const void *buf)
00110 {
00111         unsigned char *ptr = (unsigned char *) buf;
00112 
00113         return (*ptr << 24) | (*(++ptr) << 16) | (*(++ptr) << 8) |
00114             *(++ptr);
00115 }
00116 
00117 inline signed long get_slong(const void *buf)
00118 {
00119         unsigned long val = get_long(buf);
00120 
00121         if (val > 0x7FFFFFFF)
00122                 return ((signed long) (val & 0x7FFFFFFF)) - 0x80000000;
00123         else
00124                 return val;
00125 }
00126 
00127 inline unsigned long get_treble(const void *buf)
00128 {
00129         unsigned char *ptr = (unsigned char *) buf;
00130 
00131         return (*ptr << 16) | (*(++ptr) << 8) | *(++ptr);
00132 }
00133 
00134 inline signed long get_streble(const void *buf)
00135 {
00136         unsigned long val = get_treble(buf);
00137 
00138         if (val > 0x7FFFFF)
00139                 return ((signed long) (val & 0x7FFFFF)) - 0x800000;
00140         else
00141                 return val;
00142 }
00143 
00144 inline int get_short(const void *buf)
00145 {
00146         unsigned char *ptr = (unsigned char *) buf;
00147 
00148         return (*ptr << 8) | *(++ptr);
00149 }
00150 
00151 inline signed short get_sshort(const void *buf)
00152 {
00153         unsigned short val = get_short(buf);
00154 
00155         if (val > 0x7FFF)
00156                 return ((signed short) (val & 0x7FFF)) - 0x8000;
00157         else
00158                 return val;
00159 }
00160 
00161 inline int get_byte(const void *buf)
00162 {
00163         return *((unsigned char *) buf);
00164 }
00165 
00166 inline signed char get_sbyte(const void *buf)
00167 {
00168         unsigned char val = get_byte(buf);
00169 
00170         if (val > 0x7F)
00171                 return ((signed char) (val & 0x7F)) - 0x80;
00172         else
00173                 return val;
00174 }
00175 
00176 inline void set_long(void *buf, const unsigned long val)
00177 {
00178         unsigned char *ptr = (unsigned char *) buf;
00179 
00180         *ptr = (unsigned char) ((val >> 24) & 0xff);
00181         *(++ptr) = (unsigned char) ((val >> 16) & 0xff);
00182         *(++ptr) = (unsigned char) ((val >> 8) & 0xff);
00183         *(++ptr) = (unsigned char) (val & 0xff);
00184 }
00185 
00186 inline void set_slong(void *buf, const signed long val)
00187 {
00188         unsigned long uval;
00189 
00190         if (val < 0) {
00191                 uval = (val + 0x80000000);
00192                 uval |= 0x80000000;
00193         } else
00194                 uval = val;
00195         set_long(buf, uval);
00196 }
00197 
00198 inline void set_treble(void *buf, const unsigned long val)
00199 {
00200         unsigned char *ptr = (unsigned char *) buf;
00201 
00202         *ptr = (unsigned char) ((val >> 16) & 0xff);
00203         *(++ptr) = (unsigned char) ((val >> 8) & 0xff);
00204         *(++ptr) = (unsigned char) (val & 0xff);
00205 }
00206 
00207 inline void set_streble(void *buf, const signed long val)
00208 {
00209         unsigned long uval;
00210 
00211         if (val < 0) {
00212                 uval = (val + 0x800000);
00213                 uval |= 0x800000;
00214         } else
00215                 uval = val;
00216         set_treble(buf, uval);
00217 }
00218 
00219 inline void set_short(void *buf, const int val)
00220 {
00221         unsigned char *ptr = (unsigned char *) buf;
00222 
00223         *ptr = (val >> 8) & 0xff;
00224         *(++ptr) = val & 0xff;
00225 }
00226 
00227 inline void set_sshort(void *buf, const signed short val)
00228 {
00229         unsigned short uval;
00230 
00231         if (val < 0) {
00232                 uval = (val + 0x8000);
00233                 uval |= 0x8000;
00234         } else
00235                 uval = val;
00236         set_treble(buf, uval);
00237 }
00238 
00239 inline void set_byte(void *buf, const int val)
00240 {
00241         *((unsigned char *) buf) = val;
00242 }
00243 
00244 inline void set_sbyte(void *buf, const signed char val)
00245 {
00246         unsigned char uval;
00247 
00248         if (val < 0) {
00249                 uval = (val + 0x80);
00250                 uval |= 0x80;
00251         } else
00252                 uval = val;
00253         set_byte(buf, uval);
00254 }
00255 
00256 inline struct tm *getBufTm(struct tm *t, const void *buf, int setTime)
00257 {
00258         unsigned short int d = get_short(buf);
00259 
00260         t->tm_year = (d >> 9) + 4;
00261         t->tm_mon = ((d >> 5) & 15) - 1;
00262         t->tm_mday = d & 31;
00263 
00264         if (setTime) {
00265                 t->tm_hour = 0;
00266                 t->tm_min = 0;
00267                 t->tm_sec = 0;
00268         }
00269 
00270         t->tm_isdst = -1;
00271 
00272         mktime(t);
00273 
00274         return t;
00275 }
00276 
00277 inline void setBufTm(void *buf, const struct tm *t)
00278 {
00279         set_short(buf,
00280                   ((t->tm_year - 4) << 9) | ((t->tm_mon +
00281                                               1) << 5) | t->tm_mday);
00282 }
00283 
00284 inline unsigned long char4(char c1, char c2, char c3, char c4)
00285 {
00286         return (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
00287 }
00288 
00289 #endif                          /*__cplusplus*/
00290 #endif                          /* _PILOT_MACROS_H_ */

© 1996-2007 by pilot-link.org. All rights reserved.