PT_Extension.h

 /* 
PT Extensions
    Copyright (C) 2004  Phyrstorm Technologies, Inc.
Copyright (C) 2004  Nate Davis <nate@phyrtech.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef PT_EXTENSION_H
#define PT_EXTENSION_H

#include <vector>
#include <string>
#include <ctype.h>

#include <iostream>

#ifdef USING_ALL_EXTENSIONS
#define USING_STRING_LIB_EXTENSIONS
#define USING_SMTP_EXTENSIONS
#define USING_HTTP_EXTENSIONS
#endif

std::string str_to_string(double);

class Stringable {
public:
virtual std::string toString() {
return str_to_string((long)&(*this));
};
};

#define STRING_ERR ""

#define IDX_NO_DELIM 0
#define IDX_SLASH_DELIM 1
#define IDX_HYPHEN_DELIM 2
#define IDX_DBL_HYPHEN_DELIM 3
#define IDX_EQUALS_STYLE 4
#define IDX_EXTENDED_ARG 5
#define IDX_QUOTED_ARG 6
#define IDX_CASE_INSENSITIVE 7

#define IDX_MAX 8

typedef void* PTVOID;
typedef unsigned long int UINT4;

/* Data structure for MD5 (Message Digest) computation */
typedef struct {
UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */
UINT4 buf[4];                                    /* scratch buffer */
unsigned char in[64];                              /* input buffer */
unsigned char digest[16];     /* actual digest after MD5Final call */
} MD5_CTX;

typedef bool fn_cmd_iterator(PTVOID, PTVOID);

typedef enum {
NO_DELIM=1,
SLASH_DELIM=2, //uses '/' for delimiter
HYPHEN_DELIM=4, //uses '-' for delimiter
DBL_HYPHEN_DELIM=8, //uses '--' for delimiter
EQUALS_STYLE=16, //users key=item method
EXTENDED_ARG=32, //tries to decipher argument lists as contiguous strings without quotes
QUOTED_ARG=64, //assumes extended string arguments are quoted
CASE_INSENSITIVE=128 //case insensitive comparison
} CMD_STYLE;

typedef struct {
std::string key;
std::string value;
} CMD_ARG;

typedef struct {
std::vector<CMD_ARG> items;
bool *style;
} CMD_CTX;

typedef enum {
AM_PM, //returns "am" or "pm"
SWATCH, //returns the internet swatch time
ISO_8601, //returns ISO 8601 timestamp
RFC_2822, //returns RFC 2822 timestamp
DAYS_IN_MONTH, //returns the number of days in the month
DAY_OF_MONTH, //returns the day number of the month
DAY_OF_WEEK, //returns the day number of the week
YEAR, //returns the year
MONTH, //returns the month number of the year
HOUR, //returns the hour of the day
MINUTE, //returns the minute of the hour
SECONDS, //returns the seconds of the minute
LEAP_YEAR, //returns "1" for leap year else "0"
GMT_DIFF //returns the difference from Greenwich time (GMT)
} PT_TIME_TYPE;

typedef enum {
STYLE_CAPITAL=0x01, //JANUARY instead of january
STYLE_CAP_SMART=0x02, //January instead of jan or JANUARY
STYLE_TEXTUAL=0x04, //december instead of 12
STYLE_NUMREIC=0x08, //12 instead of december
STYLE_LEADING_ZERO=0x10, //01 instead of 1, 12 stays as 12
STYLE_FULL_TEXT=0x20, //january instead of jan
STYLE_ABBREVIATE=0x40, //jan instead of january
STYLE_24_HOUR=0x80, //24 hour clock
STYLE_12_HOUR=0x100, //12 hour clock
STYLE_COMMON_SET=0x124 //textual + full text + 12 hour
} PT_TIME_STYLE;

//SMTP: [common name]<user@host.tld>
//minimum: <user@host.tld>
//COMPLETE
std::string addr_smtp_GetCommonName(std::string); //returns common name
std::string addr_smtp_GetFullEmail(std::string); //user@host.tld
std::string addr_smtp_GetUser(std::string); //user
std::string addr_smtp_GetServer(std::string); //host.tld

//HTTP: [http://][user[:pwd]@][v_host][.v_host][.v_host]... domain.tld[:port]
//minimum: domain.tld
//COMPLETE
std::string addr_http_GetTLD(std::string); //returns the top level domain
std::string addr_http_GetUser(std::string); //returns "user" if applicable
std::string addr_http_GetPassword(std::string); //returns "pwd" if applicable
std::vector<std::string> addr_http_GetPrefixes(std::string); //returns a vector with all v_host fields
std::string addr_http_GetDomain(std::string); //returns the second level domain
std::string addr_http_GetPort(std::string);
std::string addr_http_GetQueryString(std::string);
std::string addr_http_StripProtocol(std::string);
std::string addr_http_StripQueryString(std::string); //removes the query string
std::string addr_http_StripUserAndPass(std::string);

//CMD lib
//IN PROGRESS
void cmd_init(CMD_CTX *, int);
void cmd_update(CMD_CTX *, char **, int sz);
void cmd_update(CMD_CTX *, const std::string &, int);
std::string cmd_get_value(CMD_CTX *, const std::string &);
bool cmd_isset(CMD_CTX *, const std::string &);
void cmd_iterator(CMD_CTX *, fn_cmd_iterator *, PTVOID);

//private
void cmd_add(CMD_CTX *, const std::string &, const std::string &);
void cmd_entry(CMD_CTX *, const std::string &);

void cmd_debug(CMD_CTX *);

//string lib
//IN PROGRESS
std::string str_TrimLeft(std::string, std::string);
std::string str_TrimRight(std::string, std::string);
std::string str_strip_left(std::string, const char &);
std::string str_strip_right(std::string, const char &);
int str_CountChar(std::string, std::string);
std::vector<std::string> str_explode(char, const std::string &, char);
std::string str_b64_encode(const std::string &);
std::string str_b64_decode(const std::string &);
std::string str_to_upper(std::string);
std::string str_to_lower(std::string);
std::string str_pad_both(std::string, const std::string &);
std::string str_pad_right(std::string, const std::string &, int sz);
std::string str_ctoh(unsigned char);
std::string str_tohex(const std::string &);
std::string str_fromhex(const std::string &);
char str_htoc(const std::string &);
std::string str_md5string(const std::string &str);
std::string str_hmac_md5string(const std::string &key, const std::string &text);

std::string str_date(const PT_TIME_TYPE &, const int s = STYLE_COMMON_SET);

std::string str_to_string(double);
std::string str_to_string(Stringable *);
std::string str_to_string(Stringable &);

template <class itemType> std::string stl_serialize(void *tmpl) {
unsigned char *buf = new unsigned char[sizeof(itemType)];
buf = reinterpret_cast<unsigned char*>(tmpl);

std::string t;
for(int i=0;i<sizeof(itemType);i++)
t += buf[i];

return str_tohex(t);
};

void stl_unserialize(void *tmpl, const std::string &);
//base 64 private
static void str_b64_encode_group(std::string &, const std::string &, int);
static bool str_b64_decode_group(std::string &, const std::string &);
//md5 private
static void Transform (UINT4 *, UINT4 *);
void MD5Init(MD5_CTX *);
void MD5Update(MD5_CTX *, unsigned char *, unsigned int);
void MD5Final(MD5_CTX *);

//SMTP extensions
#ifdef USING_SMTP_EXTENSIONS
#define GetCommonName addr_smtp_GetCommonName
#define GetAddress addr_smtp_GetFullEmail
#define GetSMTPUser addr_smtp_GetUser
#define GetServer addr_smtp_GetServer
#endif

//HTTP extensions
#ifdef USING_HTTP_EXTENSIONS
#define GetTld addr_http_GetTLD
#define GetHTTPUser addr_http_GetUser
#define GetPassword addr_http_GetPassword
#define GetPrefixes addr_http_GetPrefixes
#define GetDomain addr_http_GetDomain
#define GetPort addr_http_GetPort
#define GetQueryString addr_http_GetQueryString
#endif

//string lib extensions
#ifdef USING_STRING_LIB_EXTENSIONS
#define triml(a, b) str_TrimLeft(a, b)
#define trimr(a, b) str_TrimRight(a, b)
#define stripl(a, b) str_strip_left(a, b)
#define stripr(a, b) str_strip_right(a, b)
#define count(a, b) str_CountChar(a, b)
#define explode(a, b, c) str_explode(a, b, c)
#define b64_encode(a) str_b64_encode(a)
#define b64_decode(a) str_b64_decode(a)
#define ctoh(a) str_ctoh(a)
#define htoc(a) str_htoc(a)
#define to_hex(a) str_tohex(a)
#define from_hex(a) str_fromhex(a)
#define md5string(a) str_md5string(a)
#define hmac_md5string(a, b) str_hmac_md5string(a, b)
#define pad_both(a, b) str_pad_both(a, b)
#define pad_right(a, b, c) str_pad_right(a, b, c)
#define toupper(a) str_to_upper(a)
#define tolower(a) str_to_lower(a)
#define date(a, b) str_date(a, b)
#define to_string(a) str_to_string(a)

#define CMDInit(a, b) cmd_init(a, b)
#define CMDUpdate(a, b, c) cmd_update(a, b, c)
#define CMDGet(a, b) cmd_get_value(a, b)
#define CMDIsset(a, b) cmd_isset(a, b)
#define CMDIterator(a, b, c) cmd_iterator(a, b, c)
#endif //USING_STRING_LIB_EXTENSIONS

#endif

Project Homepage: