/legacy_one/trunk/byteptr.h

# · C Header · 116 lines · 50 code · 17 blank · 49 comment · 5 complexity · 61a4b64fe1be40b3d7dd059e6faaf686 MD5 · raw file

  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id: byteptr.h 633 2010-04-27 20:36:48Z wesleyjohnson $
  5. //
  6. // Copyright (C) 1998-2000 by DooM Legacy Team.
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // as published by the Free Software Foundation; either version 2
  11. // of the License, or (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. //
  19. // $Log: byteptr.h,v $
  20. // Revision 1.6 2003/05/04 02:26:39 sburke
  21. // Fix problems in adapting to __BIG_ENDIAN__ machines.
  22. //
  23. // Revision 1.5 2000/10/21 08:43:28 bpereira
  24. // no message
  25. //
  26. // Revision 1.4 2000/04/16 18:38:06 bpereira
  27. // no message
  28. //
  29. //
  30. // DESCRIPTION:
  31. // Macro to read/write from/to a char*, used for packet cration and such...
  32. //
  33. //-----------------------------------------------------------------------------
  34. #include "m_swap.h"
  35. #ifndef BYTEPTR_H
  36. #define BYTEPTR_H
  37. // These are used in save game, network communications, and reading some wad lumps.
  38. // TODO FIXME the reliance on specific sizes for longs, shorts etc in this file is like asking for horrible bugs
  39. static inline int16_t read_16(byte **p)
  40. {
  41. int16_t temp = *(int16_t *)*p;
  42. *p += sizeof(int16_t);
  43. return LE_SWAP16(temp);
  44. }
  45. static inline int32_t read_32(byte **p)
  46. {
  47. int32_t temp = *(int32_t *)*p;
  48. *p += sizeof(int32_t);
  49. return LE_SWAP32(temp);
  50. }
  51. static inline void write_16(byte **p, int16_t val)
  52. {
  53. *(int16_t *)*p = LE_SWAP16(val);
  54. *p += sizeof(int16_t);
  55. }
  56. static inline void write_32(byte **p, int32_t val)
  57. {
  58. *(int32_t *)*p = LE_SWAP32(val);
  59. *p += sizeof(int32_t);
  60. }
  61. // These are used in d_netcmd, d_netfil, and p_saveg
  62. // [WDJ] Change all short,long to stdint types.
  63. #define WRITEBYTE(p,b) *(p)++ = (b)
  64. #define WRITECHAR(p,b) *(p)++ = (byte)(b)
  65. #define WRITE16(p,b) write_16(&p, b)
  66. #define WRITEU16(p,b) write_16(&p, b)
  67. #define WRITE32(p,b) write_32(&p, b)
  68. #define WRITEU32(p,b) write_32(&p, b)
  69. #define WRITEFIXED(p,b) write_32(&p, b)
  70. #define WRITEANGLE(p,b) write_32(&p, b)
  71. #define WRITEBOOLEAN(p,b) *(p)++ = (b?1:0)
  72. // [WDJ]
  73. // Put {} around all stmt macros with more than one line to protect against
  74. // use as body of if,while, etc..
  75. // Would make these inline functions, but cannot because they
  76. // modify their parameters.
  77. #define WRITESTRING(p,b) { int tmp_i=0; do { WRITECHAR((p), (b)[tmp_i]); } while ((b)[tmp_i++]); }
  78. #define WRITESTRINGN(p,b,n) { int tmp_i=0; do { WRITECHAR((p), (b)[tmp_i]); if (!(b)[tmp_i]) break; tmp_i++; } while (tmp_i<(n)); }
  79. #define WRITEMEM(p,s,n) { memcpy((p),(s),(n)); (p)+=(n); }
  80. // [WDJ] Put () around all macros that return values, to ensure closure in expr.
  81. // Change all short,long to stdint types.
  82. #define READBYTE(p) (*(p)++)
  83. #define READCHAR(p) ((char)*(p)++)
  84. #define READ16(p) ((int16_t)read_16(&p))
  85. #define READU16(p) ((uint16_t)read_16(&p))
  86. #define READ32(p) ((int32_t)read_32(&p))
  87. #define READU32(p) ((uint32_t)read_32(&p))
  88. #define READFIXED(p) ((fixed_t)read_32(&p))
  89. #define READANGLE(p) ((angle_t)read_32(&p))
  90. #define READBOOLEAN(p) ((boolean)(*(p)++))
  91. // [WDJ]
  92. // Put {} around all stmt macros with more than one line to protect against
  93. // use as body of if,while, etc..
  94. // Would make these inline functions, but cannot because they
  95. // modify their parameters.
  96. #define READSTRING(p,s) { int tmp_i=0; do { (s)[tmp_i] = READBYTE(p); } while ((s)[tmp_i++]); }
  97. #define SKIPSTRING(p) { while(READBYTE(p)); }
  98. #define READMEM(p,s,n) { memcpy(s, p, n); p+=n; }
  99. #endif