httpmessage  0.4.0
HTTP message parsing library
preprocessor.h
Go to the documentation of this file.
1 /**************************************************************************************$
2  * httpmessage
3  ***************************************************************************************
4  * Copyright © 2022 by Renaud Guillard (dev@nore.fr)
5  * Distributed under the terms of the MIT License, see LICENSE
6  ***************************************************************************************
7  */
8 
14 #if !defined (LIBHTTPMESSAGGE_PREPROCESSOR_H__)
15 #define LIBHTTPMESSAGGE_PREPROCESSOR_H__
16 
17 #if !defined (HMAPI)
18 # if defined (__GNUC__)
19 
20 # define HMAPI __attribute__((visibility("default")))
21 # else
22 
23 # define HMAPI
24 # endif
25 #endif
26 
27 #if defined(__cplusplus)
28 
31 # define HTTPMESSAGE_C_BEGIN extern "C" {
32 
35 # define HTTPMESSAGE_C_END }
36 #else
37 
40 # define HTTPMESSAGE_C_BEGIN
41 
44 # define HTTPMESSAGE_C_END
45 #endif
46 
47 #if !defined (HTTPMESSAGE_POINTER_SIZE)
48 # if defined(__SIZEOF_POINTER__)
49 # define HTTPMESSAGE_POINTER_SIZE (__SIZEOF_POINTER__)
50 # elif (defined(_WIN64))
51 || (defined(__arm64__)&& (__arm64__))
52 || (defined(_LP64)&& _LP64))
53 || (defined(__x86_64)&& (__x86_64))
54 || (defined(__x86_64__)&& (__x86_64__))
55 # define HTTPMESSAGE_POINTER_SIZE 8
56 # elif defined (__ARMEL__) && (__ARMEL__)
57 # if defined(__ARM_ARCH_ISA_A64) && __ARM_ARCH_ISA_A64
58 # define HTTPMESSAGE_POINTER_SIZE 8
59 # else
60 # define HTTPMESSAGE_POINTER_SIZE 4
61 # endif
62 # elif (defined (__i386) && (__i386))
63 || (defined(__i386__)&& (__i386__))
64 || (defined(__i486)&& (__i486))
65 || (defined(__i486__)&& (__i486__))
66 || (defined(__i686)&& (__i686))
67 || (defined(__i686__)&& (__i686__))
68 || defined(_WIN32)
69 # define HTTPMESSAGE_POINTER_SIZE 4
70 # else
71 # error "Unable to detect pointer size"
72 # endif
73 #endif
74 
75 #if (HTTPMESSAGE_POINTER_SIZE == 8)
76 # define HTTPMESSAGE_PAD64(variable, size) char variable[size];
77 # define HTTPMESSAGE_PAD32(variable, size)
78 #elif (HTTPMESSAGE_POINTER_SIZE == 4)
79 # define HTTPMESSAGE_PAD64(variable, size)
80 # define HTTPMESSAGE_PAD32(variable, size) char variable[size];
81 #else
82 # error "Unsupported pointer size"
83 #endif
84 
89 #define HTTPMESSAGE_TEXT_WRITE_FILE(_written, _file, _text, _length) { \
90  size_t _result = fwrite(_text, (size_t)1, (size_t)_length, _file); \
91  if ((size_t)_result != (size_t)(_length)) return HTTPMESSAGE_ERROR_WRITE; \
92  _written += (size_t)_length; \
93 }
94 
98 #define HTTPMESSAGE_PRINTF_FILE(_written, _file, ...) { \
99  int _result = fprintf (_file, __VA_ARGS__); \
100  if (_result < 0) return _result; \
101  _written += (size_t)_result; \
102 }
103 
107 #define HTTPMESSAGE_STRING_WRITE_FILE(_written, _file, _string) \
108  HTTPMESSAGE_TEXT_WRITE_FILE(_written, _file, (_string).text, (_string).length)
109 
114 #define HTTPMESSAGE_TEXT_WRITE_BUFFER(_output, _output_size, _text, _length) { \
115  if ((size_t)(_output_size) < (size_t)(_length)) return HTTPMESSAGE_ERROR_OVERFLOW; \
116  memcpy (_output, _text, _length); \
117  _output += (size_t)(_length); _output_size -= (size_t)(_length); \
118 }
119 
124 #define HTTPMESSAGE_PRINTF_BUFFER(_output, _output_size, ...) { \
125  int _result = snprintf (_output, _output_size, __VA_ARGS__); \
126  if (_result < 0) return _result; \
127  _output += (size_t)_result; _output_size -= (size_t)_result; \
128 }
129 
133 #define HTTPMESSAGE_STRING_WRITE_BUFFER(_output, _output_size, _string) \
134  HTTPMESSAGE_TEXT_WRITE_BUFFER(_output, _output_size, (_string).text, (_string).length)
135 
136 #endif /* LIBHTTPMESSAGGE_PREPROCESSOR_H__ */
137