I infer from Google search results that strings.h (from here) is for UNIX systems. I would like to wrap the following line with a macro check of whether the host's operating system is Linux/UNIX. It would be much appreciated to hear suggestions about it. Thanks in advance.
#include <strings.h>
strings.h
contains only a few functions, most of which are simply different names for functions that are in the standard library (such as bcmp()
<--> memcmp()
). If your code uses these functions, instead of throwing #ifdef
s around why not just write your own set?
Then everyone gets to use them and be happily conditional-compilation free.
Here's a not-fully-tested set of functions in the public domain that you can use at your own risk:
#include <string.h>
#include <ctype.h>
int bcmp(const void * p1, const void * p2, size_t n)
{
return memcmp( p1, p2, n);
}
void bcopy(const void * src, void * dst, size_t n)
{
memmove( dst, src, n); /* note different order of args - yuck */
}
void bzero(void * p, size_t n)
{
memset( p, 0, n);
}
char *index(const char * s, int c)
{
return strchr( s, c);
}
char *rindex(const char * s, int c)
{
return strrchr( s, c);
}
int strcasecmp(const char* s1, const char* s2)
{
for (;;) {
int c1 = tolower( *((unsigned char*) s1++));
int c2 = tolower( *((unsigned char*) s2++));
if ((c1 != c2) || (c1 == '\0')) {
return( c1 - c2);
}
}
}
int strncasecmp(const char* s1, const char* s2, size_t n)
{
for (; n != 0; --n) {
int c1 = tolower( *((unsigned char*) s1++));
int c2 = tolower( *((unsigned char*) s2++));
if ((c1 != c2) || (c1 == '\0')) {
return( c1 - c2);
}
}
return( 0);
}
int ffs(int v)
{
unsigned int x = v;
int c = 1;
/*
* adapted from from
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
*
* a modified binary search algorithm to count 0 bits from
* the right (lsb). This algorithm should work regardless
* of the size of ints on the platform.
*
*/
/* a couple special cases */
if (x == 0) return 0;
if (x & 1) return 1; /* probably pretty common */
c = 1;
while ((x & 0xffff) == 0) {
x >>= 16;
c += 16;
}
if ((x & 0xff) == 0) {
x >>= 8;
c += 8;
}
if ((x & 0x0f) == 0) {
x >>= 4;
c += 4;
}
if ((x & 0x03) == 0) {
x >>= 2;
c += 2;
}
c -= (x & 1);
c += 1; /* ffs() requires indexing bits from 1 */
/* ie., the lsb is bit 1, not bit 0 */
return c;
}
One option would be:
#ifndef _WIN32
#include <strings.h>
#endif
Per MSDN, _WIN32
is "defined for applications for Win32 and Win64. Always defined."
A compiler or standard library for an operating system other than Windows is, of course, free to define _WIN32
, but that would be... most unusual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With