DataWorks
Database Library/Client
Loading...
Searching...
No Matches
util.c
1/* $Id: util.c 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#include "dw_util.h"
30
31#include <ctype.h>
32#include <stdbool.h>
33#include <stdlib.h>
34#include <string.h>
35#ifdef __MINGW32__
36#include <fileapi.h>
37#else
38#include <unistd.h>
39#endif
40
41char* __dw_strdup(const char* a) {
42 char* str = malloc(strlen(a) + 1);
43 memcpy(str, a, strlen(a));
44 str[strlen(a)] = 0;
45 return str;
46}
47
48char* __dw_strcat(const char* a, const char* b) {
49 char* str = malloc(strlen(a) + strlen(b) + 1);
50 memcpy(str, a, strlen(a));
51 memcpy(str + strlen(a), b, strlen(b));
52 str[strlen(a) + strlen(b)] = 0;
53 return str;
54}
55
56bool __dw_strcaseequ(const char* a, const char* b) {
57 if(strlen(a) != strlen(b)) return false;
58 int i;
59 for(i = 0; a[i] != 0; i++) {
60 if(tolower(a[i]) != tolower(b[i])) return false;
61 }
62 return true;
63}
64
65bool __dw_lockfile(FILE* fp) {
66 off_t off = ftell(fp);
67 fseek(fp, 0, SEEK_SET);
68#if defined(DOS)
69
70#elif defined(__MINGW32__)
71 OVERLAPPED overlap = {0};
72 LockFileEx(fp, LOCKFILE_EXCLUSIVE_LOCK, 0, MAXDWORD, MAXDWORD, &overlap);
73#else
74 lockf(fileno(fp), F_LOCK, 0);
75#endif
76 fseek(fp, off, SEEK_SET);
77 return false;
78}
79
80bool __dw_unlockfile(FILE* fp) {
81 off_t off = ftell(fp);
82 fseek(fp, 0, SEEK_SET);
83 fflush(fp);
84#if defined(DOS)
85
86#elif defined(__MINGW32__)
87 OVERLAPPED overlap = {0};
88 UnlockFileEx(fp, 0, MAXDWORD, MAXDWORD, &overlap);
89#else
90 lockf(fileno(fp), F_ULOCK, 0);
91#endif
92 fseek(fp, off, SEEK_SET);
93 return false;
94}
DataWorks utils.