2
0

isdatconv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isdatconv.c /main/3 1995/10/23 11:37:16 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isdatconv.c
  33. *
  34. * Description:
  35. * Conversion function between machine dependent and the X/OPEN
  36. * machine independent formats.
  37. *
  38. * Some pieces of code may not be very "structured", but they result in
  39. * optimized code with the -O compiler option.
  40. *
  41. */
  42. #include "isam_impl.h"
  43. #define BLANK ' '
  44. /* conversion functions for sparc architecture */
  45. /* ldlong() - Load a long integer from a potentially unaligned address */
  46. long
  47. ldlong(char *p)
  48. {
  49. int i;
  50. #if LONG_BIT == 64
  51. unsigned long val;
  52. #else
  53. unsigned int val;
  54. #endif
  55. val = 0;
  56. for (i=0; i<LONGSIZE ; i++)
  57. val = (val << 8) + *((unsigned char *)p++);
  58. return ((long)val);
  59. }
  60. /* stlong() - Store a long integer at a potentially unaligned address */
  61. int
  62. stlong(long val, char *p)
  63. {
  64. int i;
  65. p += LONGSIZE;
  66. for (i=0; i<LONGSIZE ; i++)
  67. *--p = (val >> 8*i) & 255;
  68. return(0);
  69. }
  70. /* ldint() - Load a short integer from a potentially unaligned address */
  71. short
  72. ldint(char *p)
  73. {
  74. unsigned int val;
  75. val = *((unsigned char *)p++);
  76. val = (val << 8) + *((unsigned char *)p++);
  77. return ((short)val);
  78. }
  79. /* ldunshort - load a unshort integer : for 64K record length */
  80. u_short
  81. ldunshort(char *p)
  82. {
  83. unsigned int val;
  84. val = *((unsigned char *)p++);
  85. val = (val << 8) + *((unsigned char *)p++);
  86. return ((u_short)val);
  87. }
  88. /* stint() - Store a short integer at a potentially unaligned address */
  89. int
  90. stint(short val, char *p)
  91. {
  92. p += SHORTSIZE;
  93. *--p = val & 255;
  94. *--p = (val >> 8) & 255;
  95. return(0);
  96. }
  97. /* ldchar() - Load character field */
  98. int
  99. ldchar(char *src, int len, char *dst)
  100. {
  101. char *p;
  102. if (len <= 0)
  103. return 0;
  104. /* Load the entire string. */
  105. memcpy((void *) dst, (const void *) src, len);
  106. /* Remove trailing blanks. */
  107. p = dst + len;
  108. while (--p >= dst) {
  109. if (*p != BLANK) {
  110. break;
  111. }
  112. }
  113. *++p = '\0';
  114. return 0;
  115. }
  116. int
  117. stchar(char *src, char *dst, int len)
  118. {
  119. char c;
  120. if (len <= 0)
  121. return 0;
  122. /* Copy up to NULL character. */
  123. do {
  124. if ((c = *src++) == '\0')
  125. break;
  126. *dst++ = c;
  127. } while (--len > 0);
  128. /* Pad with blanks. */
  129. if (len > 0)
  130. (void) memset((void *) dst, BLANK, len);
  131. return 0;
  132. }
  133. /* ldchar2() - Load character field (C style, NULL padded) */
  134. int
  135. ldchar2(char *src, int len, char *dst)
  136. {
  137. if (len <= 0)
  138. return 0;
  139. /* Load the entire string. */
  140. memcpy((void *) dst, (const void *) src, len);
  141. *(dst + len) = '\0';
  142. return 0;
  143. }
  144. int
  145. stchar2(char *src, char *dst, int len)
  146. {
  147. char c;
  148. if (len <= 0)
  149. return 0;
  150. /* Copy up to a NULL character. */
  151. do {
  152. if ((c = *src++) == '\0')
  153. break;
  154. *dst++ = c;
  155. } while (--len > 0);
  156. /* Pad with NULLs. */
  157. if (len > 0)
  158. memset(dst, 0, len);
  159. return 0;
  160. }
  161. /* ldfloat() - Load a float number from a potentially unaligned address */
  162. float
  163. ldfloat(char *p)
  164. {
  165. union {
  166. float fval;
  167. int ival;
  168. } uval;
  169. unsigned int val;
  170. val = *((unsigned char *)p++);
  171. val = (val << 8) + *((unsigned char *)p++);
  172. val = (val << 8) + *((unsigned char *)p++);
  173. val = (val << 8) + *((unsigned char *)p++);
  174. uval.ival = val;
  175. return (uval.fval);
  176. }
  177. /* stfloat() - Store a float number at a potentially unaligned address */
  178. /* f, Bug - it is passed as double */
  179. int
  180. stfloat(float f, char *p)
  181. {
  182. unsigned val;
  183. union {
  184. float fval;
  185. int ival;
  186. } uval;
  187. uval.fval = f; /* This fixes compiler bug */
  188. val = uval.ival;
  189. p += LONGSIZE;
  190. *--p = val & 255;
  191. *--p = (val >> 8) & 255;
  192. *--p = (val >> 16) & 255;
  193. *--p = (val >> 24) & 255;
  194. return(0);
  195. }
  196. #if sparc | mc68000 /* MRJ */
  197. /* ldbld() - Load a double float number from a potentially unaligned address */
  198. double
  199. lddbl(char *p)
  200. {
  201. double val;
  202. memcpy((void *)&val, (const void *) p, DOUBLESIZE);
  203. return (val);
  204. }
  205. /* stdbl() - Store a double float number at a potentially unaligned address */
  206. int
  207. stdbl(double val, char *p)
  208. {
  209. memcpy ( p,(char *)&val, DOUBLESIZE);
  210. return 0;
  211. }
  212. #else /* 386i -- do it the long way round.... */
  213. /* ldbld() - Load a double float number from a potentially unaligned address */
  214. double
  215. lddbl(char *p)
  216. {
  217. union {
  218. double rval;
  219. char sval[DOUBLESIZE];
  220. } x;
  221. char *q;
  222. int i;
  223. q = x.sval;
  224. p += DOUBLESIZE;
  225. for (i=0; i<DOUBLESIZE; i++)
  226. *q++ = *--p;
  227. return (x.rval);
  228. }
  229. /* stdbl() - Store a double float number at a potentially unaligned address */
  230. int
  231. stdbl(double val, char *p)
  232. {
  233. union {
  234. double rval;
  235. char sval[DOUBLESIZE];
  236. } x;
  237. char *q;
  238. int i;
  239. x.rval = val;
  240. q = x.sval;
  241. p += DOUBLESIZE;
  242. for (i=0; i<DOUBLESIZE; i++)
  243. *--p = *q++ ;
  244. return(0);
  245. }
  246. #endif /* sparc */