1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* XMail by Davide Libenzi ( Intranet and Internet mail server )
* Copyright (C) 1999,..,2004 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <davidel@xmailserver.org>
*
*/
#ifndef _SYSMACROS_H
#define _SYSMACROS_H
#define SRand() srand((unsigned int) (SysMsTime() * SysGetCurrentThreadId()))
#define NbrCeil(n, a) ((((n) + (a) - 1) / (a)) * (a))
#define NbrFloor(n, a) (((n) / (a)) * (a))
#define Sign(v) (((v) < 0) ? -1: +1)
#define Min(a, b) (((a) < (b)) ? (a): (b))
#define Max(a, b) (((a) > (b)) ? (a): (b))
#define Abs(v) (((v) > 0) ? (v): -(v))
#define INext(i, n) ((((i) + 1) < (n)) ? ((i) + 1): 0)
#define IPrev(i, n) (((i) > 0) ? ((i) - 1): ((n) - 1))
#define LIndex2D(i, j, n) ((i) * (n) + (j))
#define ZeroData(d) memset(&(d), 0, sizeof(d))
#define CountOf(t) (sizeof(t) / sizeof((t)[0]))
#define SetEmptyString(s) (s)[0] = '\0'
#define IsEmptyString(s) (*(s) == '\0')
#define CStringSize(s) (sizeof(s) - 1)
#define StrCmdMatch(s, c) StrNCmdMatch(s, c, CStringSize(c))
#define StrSkipSpaces(p) for (; (*(p) == ' ') || (*(p) == '\t'); (p)++)
#define CharISame(a, b) (tolower(a) == tolower(b))
#define StrINComp(s, t) strnicmp(s, t, strlen(t))
#define StrNComp(s, t) strncmp(s, t, strlen(t))
#define StrSINComp(s, t) strnicmp(s, t, CStringSize(t))
#define StrSNComp(s, t) strncmp(s, t, CStringSize(t))
#define StrNCpy(t, s, n) do { strncpy(t, s, n); (t)[(n) - 1] = '\0'; } while (0)
#define StrSNCpy(t, s) StrNCpy(t, s, sizeof(t))
#define StrSNCat(t, s) StrNCat(t, s, sizeof(t))
#define Cpy2Sz(d, s, n) do { memcpy(d, s, (n) * sizeof(*(s))); (d)[n] = 0; } while (0)
#define StrEnd(s) ((char *) (s) + strlen(s))
#define CheckRemoveFile(fp) ((SysExistFile(fp)) ? SysRemove(fp) : 0)
#define SysFreeNullify(p) do { SysFree(p), (p) = NULL; } while(0)
#define FCloseNullify(f) do { if ((f) != NULL) fclose(f); (f) = NULL; } while (0)
#define ErrorSet(e) (ErrSetErrorCode(e), e)
#define ErrorPush() int __iPushedError = ErrGetErrorCode()
#define ErrorPop() ErrorSet(__iPushedError)
#define ErrorFetch() __iPushedError
#define DatumStrSet(d, s) do { (d)->pData = (char *) (s); (d)->lSize = strlen(s); } while (0)
#define IsDotFilename(f) ((f)[0] == '.')
#define IsEmailAddress(a) (strchr((a), '@') != NULL)
#define MemMatch(s, n, m, l) ((n) == (l) && memcmp(s, m, n) == 0)
#define EquivDatum(a, b) ((a)->lSize == (b)->lSize && memcmp((a)->pData, (b)->pData, (a)->lSize) == 0)
#define ArrayInit(a, v) do { unsigned int __i; for (__i = 0; __i < CountOf(a); __i++) (a)[__i] = (v); } while (0)
#define StrVSprint(r, l, f) do { \
int iCurrSize = 256; \
int iPSize; \
va_list Args; \
for (;;) { \
r = (char *) SysAlloc(iCurrSize); \
if (r == NULL) \
break; \
va_start(Args, l); \
if (((iPSize = SysVSNPrintf(r, iCurrSize - 1, f, Args)) >= 0) && \
iPSize < iCurrSize) { \
va_end(Args); \
break; \
} \
va_end(Args); \
if (iPSize > 0) \
iCurrSize = (4 * iPSize) / 3 + 2; \
else \
iCurrSize *= 2; \
SysFree(r); \
} \
} while (0)
/* Inline functions */
inline char *StrNCat(char *pszDest, char const *pszSrc, int iMaxSize)
{
int iDestLength = strlen(pszDest);
if (iDestLength < iMaxSize)
StrNCpy(pszDest + iDestLength, pszSrc, iMaxSize - iDestLength);
return pszDest;
}
inline int StrNCmdMatch(char const *pszCmdLine, char const *pszCmd, int iCmdLength)
{
return strnicmp(pszCmdLine, pszCmd, iCmdLength) == 0 &&
(pszCmdLine[iCmdLength] == '\0' || strchr(" \r\n\t", pszCmdLine[iCmdLength]) != NULL);
}
inline char *AppendChar(char *pszString, int iChar)
{
int iStrLength = strlen(pszString);
if (iStrLength == 0 || pszString[iStrLength - 1] != iChar) {
pszString[iStrLength] = iChar;
pszString[iStrLength + 1] = '\0';
}
return pszString;
}
inline char *AppendSlash(char *pszPath)
{
return AppendChar(pszPath, SYS_SLASH_CHAR);
}
inline char *DelFinalChar(char *pszString, int iChar)
{
int iStrLength = strlen(pszString);
if (iStrLength > 0 && pszString[iStrLength - 1] == iChar)
pszString[iStrLength - 1] = '\0';
return pszString;
}
inline char *DelFinalSlash(char *pszPath)
{
return DelFinalChar(pszPath, SYS_SLASH_CHAR);
}
inline int ToUpper(int iChar)
{
return ((iChar >= 'a') && (iChar <= 'z')) ? ('A' + (iChar - 'a')) : iChar;
}
inline int ToLower(int iChar)
{
return ((iChar >= 'A') && (iChar <= 'Z')) ? ('a' + (iChar - 'A')) : iChar;
}
inline int IsPrimeNumber(int iNumber)
{
if (iNumber > 3) {
if (iNumber & 1) {
int iHalfNumber = iNumber / 2;
for (int ii = 3; ii < iHalfNumber; ii += 2)
if ((iNumber % ii) == 0)
return 0;
} else
return 0;
}
return 1;
}
inline char *ClearEOL(char *pszBuffer)
{
int iSize = strlen(pszBuffer);
for (; iSize > 0 && (pszBuffer[iSize - 1] == '\r' || pszBuffer[iSize - 1] == '\n');
iSize--);
pszBuffer[iSize] = '\0';
return pszBuffer;
}
#endif
|