forked from mirror/openmw-tes3mp
Project clean-up: move 'misc' into the platform lib, update CMake to build 'platform' as a lib, and various fixes for Visual Studio compiler warnings
parent
82bbc69453
commit
819c146ad1
@ -0,0 +1,14 @@
|
||||
project(Platform)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/*)
|
||||
file(GLOB_RECURSE CAELUM_HDR include/*)
|
||||
|
||||
set(SOURCES
|
||||
fileops.cpp
|
||||
fileops.hpp
|
||||
slice_array.hpp
|
||||
stdint.h
|
||||
stringops.cpp
|
||||
stringops.hpp
|
||||
strings.h)
|
||||
add_library(platform STATIC ${SOURCES})
|
@ -0,0 +1,11 @@
|
||||
#include "fileops.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace OMW { namespace Platform {
|
||||
|
||||
bool isFile(const char *name)
|
||||
{
|
||||
boost::filesystem::path cfg_file_path(name);
|
||||
return boost::filesystem::exists(cfg_file_path);
|
||||
}
|
||||
}}
|
@ -0,0 +1,10 @@
|
||||
#ifndef __FILEOPS_H_
|
||||
#define __FILEOPS_H_
|
||||
|
||||
namespace OMW { namespace Platform {
|
||||
|
||||
/// Check if a given path is an existing file (not a directory)
|
||||
bool isFile(const char *name);
|
||||
}}
|
||||
|
||||
#endif
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
OpenMW - The completely unofficial reimplementation of Morrowind
|
||||
Copyright (C) 2008-2010 Nicolay Korslund
|
||||
Email: < korslund@gmail.com >
|
||||
WWW: http://openmw.sourceforge.net/
|
||||
|
||||
This file (slice_array.h) is part of the OpenMW package.
|
||||
|
||||
OpenMW is distributed as free software: you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License
|
||||
version 3, as published by the Free Software Foundation.
|
||||
|
||||
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
|
||||
version 3 along with this program. If not, see
|
||||
http://www.gnu.org/licenses/ .
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _SLICE_ARRAY_H_
|
||||
#define _SLICE_ARRAY_H_
|
||||
|
||||
// A simple array implementation containing a pointer and a
|
||||
// length. Used for holding slices into a data buffer.
|
||||
#include <string.h>
|
||||
template <class T>
|
||||
struct SliceArray
|
||||
{
|
||||
const T* ptr;
|
||||
size_t length;
|
||||
|
||||
/// Initialize to zero length
|
||||
SliceArray() : ptr(0), length(0) {}
|
||||
|
||||
/// Initialize from pointer + length
|
||||
SliceArray(const T* _ptr, size_t _length)
|
||||
: ptr(_ptr), length(_length) {}
|
||||
|
||||
/// Initialize from null-terminated string
|
||||
SliceArray(const char* str)
|
||||
{
|
||||
ptr = str;
|
||||
length = strlen(str);
|
||||
}
|
||||
|
||||
bool operator==(SliceArray &t)
|
||||
{
|
||||
return
|
||||
length == t.length &&
|
||||
(memcmp(ptr,t.ptr, length*sizeof(T)) == 0);
|
||||
}
|
||||
|
||||
/// Only use this for stings
|
||||
bool operator==(const char* str)
|
||||
{
|
||||
return
|
||||
str[length] == 0 &&
|
||||
(strncmp(ptr, str, length) == 0);
|
||||
}
|
||||
|
||||
/** This allocates a copy of the data. Only use this for debugging
|
||||
and error messages. */
|
||||
std::string toString()
|
||||
{ return std::string(ptr,length); }
|
||||
};
|
||||
|
||||
typedef SliceArray<char> SString;
|
||||
typedef SliceArray<int> IntArray;
|
||||
typedef SliceArray<float> FloatArray;
|
||||
|
||||
#endif
|
@ -0,0 +1,63 @@
|
||||
#include "stringops.hpp"
|
||||
|
||||
#include <string.h>
|
||||
#include "strings.h"
|
||||
|
||||
namespace OMW { namespace Platform {
|
||||
|
||||
bool begins(const char* str1, const char* str2)
|
||||
{
|
||||
while(*str2)
|
||||
{
|
||||
if(*str1 == 0 || *str1 != *str2) return false;
|
||||
|
||||
str1++;
|
||||
str2++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ends(const char* str1, const char* str2)
|
||||
{
|
||||
int len1 = strlen(str1);
|
||||
int len2 = strlen(str2);
|
||||
|
||||
if(len1 < len2) return false;
|
||||
|
||||
return strcmp(str2, str1+len1-len2) == 0;
|
||||
}
|
||||
|
||||
// True if the given chars match, case insensitive
|
||||
static bool icmp(char a, char b)
|
||||
{
|
||||
if(a >= 'A' && a <= 'Z')
|
||||
a += 'a' - 'A';
|
||||
if(b >= 'A' && b <= 'Z')
|
||||
b += 'a' - 'A';
|
||||
|
||||
return a == b;
|
||||
}
|
||||
|
||||
bool ibegins(const char* str1, const char* str2)
|
||||
{
|
||||
while(*str2)
|
||||
{
|
||||
if(*str1 == 0 || !icmp(*str1,*str2)) return false;
|
||||
|
||||
str1++;
|
||||
str2++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool iends(const char* str1, const char* str2)
|
||||
{
|
||||
int len1 = strlen(str1);
|
||||
int len2 = strlen(str2);
|
||||
|
||||
if(len1 < len2) return false;
|
||||
|
||||
return strcasecmp(str2, str1+len1-len2) == 0;
|
||||
}
|
||||
|
||||
}}
|
@ -0,0 +1,19 @@
|
||||
#ifndef __STRINGOPS_H
|
||||
#define __STRINGOPS_H
|
||||
|
||||
namespace OMW { namespace Platform {
|
||||
|
||||
/// Returns true if str1 begins with substring str2
|
||||
bool begins(const char* str1, const char* str2);
|
||||
|
||||
/// Returns true if str1 ends with substring str2
|
||||
bool ends(const char* str1, const char* str2);
|
||||
|
||||
/// Case insensitive, returns true if str1 begins with substring str2
|
||||
bool ibegins(const char* str1, const char* str2);
|
||||
|
||||
/// Case insensitive, returns true if str1 ends with substring str2
|
||||
bool iends(const char* str1, const char* str2);
|
||||
}}
|
||||
|
||||
#endif
|
@ -0,0 +1 @@
|
||||
*_test
|
@ -0,0 +1,6 @@
|
||||
hello, len=5
|
||||
001
|
||||
hell, len=4
|
||||
010
|
||||
01
|
||||
4 3
|
@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../slice_array.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
SString s, t;
|
||||
s = SString("hello");
|
||||
cout << s.toString() << ", len=" << s.length << endl;
|
||||
cout << (s=="hel") << (s=="hell") << (s=="hello") << endl;
|
||||
t = s;
|
||||
|
||||
s = SString("othello"+2, 4);
|
||||
cout << s.toString() << ", len=" << s.length << endl;
|
||||
cout << (s=="hel") << (s=="hell") << (s=="hello") << endl;
|
||||
|
||||
cout << (s==t) << (SString("hello")==t) << endl;
|
||||
|
||||
const int arr[4] = {1,2,3,4};
|
||||
|
||||
IntArray ia(arr,4);
|
||||
|
||||
cout << ia.length << " " << ia.ptr[2] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "../stringops.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
assert(begins("abc", "a"));
|
||||
assert(begins("abc", "ab"));
|
||||
assert(begins("abc", "abc"));
|
||||
assert(begins("abcd", "abc"));
|
||||
|
||||
assert(!begins("abc", "b"));
|
||||
assert(!begins("abc", "bc"));
|
||||
assert(!begins("abc", "bcd"));
|
||||
assert(!begins("abc", "abcd"));
|
||||
|
||||
assert(ibegins("Abc", "a"));
|
||||
assert(ibegins("aBc", "ab"));
|
||||
assert(ibegins("abC", "abc"));
|
||||
assert(ibegins("abcD", "abc"));
|
||||
|
||||
assert(!ibegins("abc", "b"));
|
||||
assert(!ibegins("abc", "bc"));
|
||||
assert(!ibegins("abc", "bcd"));
|
||||
assert(!ibegins("abc", "abcd"));
|
||||
|
||||
assert(ends("abc", "c"));
|
||||
assert(ends("abc", "bc"));
|
||||
assert(ends("abc", "abc"));
|
||||
assert(ends("abcd", "abcd"));
|
||||
|
||||
assert(!ends("abc", "b"));
|
||||
assert(!ends("abc", "ab"));
|
||||
assert(!ends("abc", "bcd"));
|
||||
assert(!ends("abc", "abcd"));
|
||||
|
||||
assert(iends("Abc", "c"));
|
||||
assert(iends("aBc", "bc"));
|
||||
assert(iends("abC", "abc"));
|
||||
assert(iends("abcD", "abcd"));
|
||||
|
||||
assert(!iends("abc", "b"));
|
||||
assert(!iends("abc", "ab"));
|
||||
assert(!iends("abc", "bcd"));
|
||||
assert(!iends("abc", "abcd"));
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
make || exit
|
||||
|
||||
mkdir -p output
|
||||
|
||||
PROGS=*_test
|
||||
|
||||
for a in $PROGS; do
|
||||
if [ -f "output/$a.out" ]; then
|
||||
echo "Running $a:"
|
||||
./$a | diff output/$a.out -
|
||||
else
|
||||
echo "Creating $a.out"
|
||||
./$a > "output/$a.out"
|
||||
git add "output/$a.out"
|
||||
fi
|
||||
done
|
Loading…
Reference in New Issue