DataWorks
Database Library/Client
Loading...
Searching...
No Matches
dw_util.h
Go to the documentation of this file.
1/* $Id: dw_util.h 78 2024-05-22 06:08:48Z nishi $ */
2/* --- START LICENSE --- */
3/* -------------------------------------------------------------------------- */
4/* Copyright (c) 2024 Crabware. */
5/* Redistribution and use in source and binary forms, with or without modific */
6/* ation, are permitted provided that the following conditions are met: */
7/* 1. Redistributions of source code must retain the above copyright noti */
8/* ce, this list of conditions and the following disclaimer. */
9/* 2. Redistributions in binary form must reproduce the above copyright n */
10/* otice, this list of conditions and the following disclaimer in the documen */
11/* tation and/or other materials provided with the distribution. */
12/* 3. Neither the name of the copyright holder nor the names of its contr */
13/* ibutors may be used to endorse or promote products derived from this softw */
14/* are without specific prior written permission. */
15/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */
16/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */
17/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */
18/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */
19/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */
20/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */
21/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */
22/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */
23/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */
24/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */
25/* OF SUCH DAMAGE. */
26/* -------------------------------------------------------------------------- */
27/* --- END LICENSE --- */
28
29#ifndef __DATAWORKS_DW_UTIL_H__
30#define __DATAWORKS_DW_UTIL_H__
31
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#include <stdbool.h>
44#include <stdio.h>
45
46#include "dataworks.h"
47
48char* __dw_strdup(const char* a);
49char* __dw_strcat(const char* a, const char* b);
50bool __dw_strcaseequ(const char* a, const char* b);
51bool __dw_lockfile(FILE* fp);
52bool __dw_unlockfile(FILE* fp);
53
54#define __dw_xstr(x) #x
55#define __dw_str(x) __dw_xstr(x)
56
57/* Converts NE to BE */
58#define __dw_big_endian(arg, type, exec) \
59 { \
60 type __original = arg; \
61 signed char* __ptr = (signed char*)&__original; \
62 type __converted; \
63 signed char* __converted_ptr = (signed char*)&__converted; \
64 int __i; \
65 int __endian_check = 1; \
66 char __endian = (1 == *(volatile char*)&__endian_check) ? 'L' : 'B'; \
67 for(__i = 0; __i < sizeof(type); __i++) { \
68 if(__endian == 'L') { \
69 __converted_ptr[sizeof(type) - __i - 1] = __ptr[__i]; \
70 } else { \
71 __converted_ptr[__i] = __ptr[__i]; \
72 } \
73 } \
74 exec; \
75 }
76
77/* Converts BE to NE */
78#define __dw_native_endian(arg, type, exec) \
79 { \
80 type __original = arg; \
81 signed char* __ptr = (signed char*)&__original; \
82 type __converted; \
83 signed char* __converted_ptr = (signed char*)&__converted; \
84 int __i; \
85 int __endian_check = 1; \
86 char __endian = (1 == *(volatile char*)&__endian_check) ? 'L' : 'B'; \
87 for(__i = 0; __i < sizeof(type); __i++) { \
88 if(__endian == 'L') { \
89 __converted_ptr[sizeof(type) - __i - 1] = __ptr[__i]; \
90 } else { \
91 __converted_ptr[__i] = __ptr[__i]; \
92 } \
93 } \
94 exec; \
95 }
96
97#ifdef __cplusplus
98}
99#endif
100
101#endif
DataWorks info.