// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   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
// -------------------------- END HEADER -------------------------------------

#ifndef MALSTRING_H
#define MALSTRING_H

#include <string>
#include <vector>

/// Return true if the character is whitespace. False otherwise.
bool isWhiteSpace(char c);

/// Simple function to see if a string starts with another string.
/// (needle is the string we're looking for, haystack is the string
/// we're looking in).
bool strStartsWith(const std::string &needle, const std::string &haystack);

/// Return true if one string ends with another string. (needle is the
/// string we're looking for, haystack is the string we're looking in).
bool strEndsWith(const std::string &needle, const std::string &haystack);

/// Generate tokens from a string. Stores saved tokens into the
/// passed-in tokens parameter.
void stringTokenize(const std::string &str, const std::string &delims, std::vector<std::string> &tokens, bool allowEmpty = false);

// TODO: Advanced string tokenization, escape/unescape, etc.

/// Escape a string.
std::string stringEscape(const std::string &str);

/// Unescape a string.
std::string stringUnescape(const std::string &str);

/// Encode a binary buffer as a string of hex values..
void strEncodeHex(const void *buf, int length, std::string &str, int columns = 80);

/// Decode a string of hex values. Ownership of the buffer given to the caller.
char *strDecodeHex(const std::string &str, int *length);

class SimpleBuffer {
	
	public:
	
		SimpleBuffer(void);
		SimpleBuffer(const void *data, int length);
		~SimpleBuffer(void);
		
		void addData(const void *buffer, int length);
		const char *getData(void) const;
		int getLength(void) const;
		
		const char *getDataAndAdvance(void *dst, int bytes);

        int compare(const void *buffer, int length);
		
        void clear(void);

	private:
	
		char *data;
		int length;
		int readPtr;
};

#endif
