📄 masterstring.h
字号:
// masterstring - null terminated string manipulation
// class hierarchy , as well as a large series of string related algorithims
// by jared bruni
// my new string algorithims
// jared@lostsidedead.com
// some very important algorithims
// I totaly re-wrote all these from scratch
// so it would work better with the new masasm (my newest project)
// www.lostsidedead.com
// this source file is dedicated to my buds (hehe)
/************ Using MasterString ********************
Within this source file consits of a series of
algorithims and classes. You can inturn use these
different aspects of the source to produce your
own string classes, or to simply manipulate
char arrays. There are multiple levels of functionality
as well as a few base classes for your classes to
inhert from. This source is to be thought of as the
layer ontop of string.h , allowing for simplicity
as well as expanding the types of things it can do.
The Functions**************************************
All the simple functions with lowercased names, are
the c-style functions. These allow you to pass a pointer
to the character array, and manipulate it , in the way
it describes (ex: findstr,stringcopy) etc these functions
are very useful when directly manipulating character arrays
and provide a much deeper form of control over the array.
There are seires of different forms of string searches,
which allow you to have full control. One of my favorite
new ones which Im going to talk about here is findstrloc.
findstrloc stands for find string location, its prototype is
the following.
void findstrloc(char* buff,char* search,MasStrDataLoc* dloc);
This function cycles through the buffer, and every time it
finds an instance of search, it leaves a refrence to it
inside the MasStrDataLoc structure. Pretty nifty, but
there are other ways to be able to cycle through all the
characters, with very explicit searchs.
findstr
ifindstr
findstrb
ifindstrb
etc
These forms of searching for strings within your strings are
extermly useful. They allow you to start off at the begining
and then continue searching were you left off at. Allows you
to search forward, backward, and even within a range of those
two points. findstrcf (find string cycle forward) and findstrcb
(find string cycle backward). There are also a seires of functions
that directly manipulate with ranges. These searchs, return a structure
(MasStrRange) which contains two points. One for the starting location
of the sub string, and one for were it stops. Pretty nifty when needing
to have a full detailed structure of were things are at, to make
the whole manipulation proccess easy.
The Classes / Structures *************************
The next layer ontop of these functions, is the
MasStrObj. This is a object, to use as a base
object to inhert into your string classes, or
whatever other class would need the structure
of these functions.
MasterString object
The master string object, is a String class, which rather
then attempting to just encapsulate all the string functionality
it instead, trys to simplfy the proccess of using the object
but still consist of the same amount of power. The MasterString
inherts from MasStrObj, and then expands upon this to provide
a autosizing string. It does this by creating a pointer to memory
on the freestore, and then freeing that memory at appropriate times.
It also has another layer, were the methods which use a form
of hungarin notation (FindWindow LikeThis) automaticly point to the
string within the Object. Let me give a example of this.
theres the function findstr, and then the method within the
MasterString object, FindString
the following would be how you would call it
char string1[100];
stringcopy(string1, "stuff is x here");
int placeval;
placeval = findstr(string1,"x");
using the MasterString , its actualy quite simpler
MasterString str("stuff is x here");
int placeval;
placeval = str.FindString("x");
When using the MasterString object, it basicly lets you
encapsulate what character array the low level functions
are pointing to, while still allowing you the ability
to directly call any of the other functions.
MasterStringList ********************************
The MasterStringList class , is pretty simple to grasp
and use. Its simply a class which holds a list of
MasterStrings :)
This class incorperates multiple inheritance one of my
favorite features of the C++ language
MasterINI ***************************************
The MasterINI class is also fairly simple. It is a
object that is kinda like a INI file. You basicly
put in strings, by there name, and what the value is
for that name, and it can then be saved & loaded.
Its really easy to use / work, just remember to always
create your size, or its gonna trip! example:
MasterINI mstr(100); // allows 100 entrys
MasterINI mstrx; // has no size
mstrx.Create(100);// now has the size of 100 entrys
- Jared Bruni
Master@LostSideDead.com -
****************************************************/
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
#define NOTFOUND -1
#define INULL -1
#define NOPREV -1
#define NULLI -1
#define STRBEGIN -1
#define MRANGE_HI 1
#define MRANGE_LO 0
#define NULLPOS -1
// string algorithims
void stringcopy(char* dest,char* from); // string copy
void stringattach(char* dest,char* from); // string attach
void leftcopy(char* from,char* dest,int pos);// left copy (leftstr)
void rightcopy(char* from,char* dest,int pos);// right copy (rightstr)
void midcopy(char* from,char* dest,int start_pos,int stop_pos);// middle copy (midstr)
void trimspace(char* buff);// trim space characters
int findstr(char* buffer,char* search);// find string search
int ifindstr(int i,char* buffer,char* search);// find string search from position
int findstrb(char* buffer,char* search); // backwards search
int ifindstrb(int i,char* buffer,char* search); // inside backwards search
int fsearchstr(int start,int stop,char* buff,char* search);// fully controled range search (forward)
int bsearchstr(int start,int stop,char* buff,char* search);// fully controled range search (backward)
void removestr(char* buff,char* str);// remove instances of str in buff
int getlen(char* buff);// get length of the string
void lcase(char* buff);// lower case string
void ucase(char* buff);// upper case string
int lcasel(int c);// lower case letter
int ucasel(int c);// upper case letter
int findoccurance(char* buff,char* search);// find occourances of a specific string
void convertinteger(int integer,char* buff,int base); // convert integer to string
int convertstring(char* buff); // convert string to integer
bool ishexc(char c);// is this character a hexidecimal digit?
bool ishex(char* buff);// is this string a hexidecimal value?
int hextointc(char c);// hexidecimal digit to integer value
int hextoint(char* buff); // hexidecimal digits to integer value
int findoccourance(char* buff,char* search);// find the numeric of times a string occours
void tripcharup(char* buff,int upby);// bump the digits up X times
void tripchardown(char* buff,int downby);// dump the digits down X times
void replacechar(char* string,char findchr,char replace);// replace single character, through out all instances
void replacestr(char* string,char* findstr,char* rep,char* output);// replace string inside string
void rmvinst(char* buff,char* findstr,char* replace,char* output); // remove single instance of string
char randomchar(int max);// produce a random character
char randomchar(int max,int up);// produce a random character, and then make it + up
void randomstr(char* buff,int char_max,int char_size);// random string
void removechar(char* input,char* output,char c); // remove characters from buffer
int findchar(char* str,char c);// find single char (forward)
int ifindchar(int start,char* str,char c); // inside find single char (forward)
int findcharb(char* str,char c);// find single char (backward)
int ifindcharb(int start,char* str,char c); // find single char backward ex
int findcharcf(char* str,int start,int stop,char c);// find single char controled forward
int findcharcb(char* str,int start,int stop,char c);// find single char controled backward
void removestr(char* input,char* output,char* string); // remove instance of string
void rmvstrx(char* buff,char* output,char* string); // remove single instance of string
void strsavefile(char* filename,char* string);// save the string as a text file
int getfilestringlength(char* filename);// get the string length of a text file
bool strloadfile(char* filename,char* output);// load a string from a text file
void reversestring(char* input,char* output); // reverse sring
bool isstrvalid(char* string); // checks to see if the string is valid or not (is null terminated)
// string compares
bool mstrcmp(char* buff1,char* buff2); // case sensitive compare
bool mstrcmpx(char* buff1,char* buff2);// not case sensitive compare
bool insidestr(char* buff1,char* buff2); // am i inside this string?
bool insidestrx(char* buff1,char* buff2);// am i inside this string lowercased ?
void strsep(char* str, char* sleft, int go_left, char* sright, int go_right);// seperate into 2 seperate strings from 2 seperate points
void strsetnull(char* str,int pos); // reset the 0's position
void rmvnullset(char* str); // remove the 0 to end the string
int getnullpos(char* str); // get the position of the null
void trimcom(char* buff, char* output, char startstr,char stopstr);
void asmtrim(char* input,char* output);
int countlines(char* buffer); // counts how many \n characters exisit
int getfirstletter(char* buffer); // get first letter thats not a space
// string data location structure use w/ (findstrl)
struct MasStrDataLoc
{
int* dindex;
int array_size;
char* thestring;
char* searchstr;
bool search_status;
inline MasStrDataLoc()
{
search_status = false;
dindex = 0;
array_size = 0;
searchstr = 0;
}
inline ~MasStrDataLoc()
{
if(dindex != 0)
{
delete [] dindex;
dindex = 0;
}
if(thestring != 0)
{
delete [] thestring;
thestring = 0;
}
if(searchstr != 0)
{
delete [] searchstr;
searchstr = 0;
}
}
inline void create_array(int size)
{
dindex = new int[size];
array_size = size;
}
inline void setstr(char* buff)
{
int len;
len = strlen(buff);
thestring = new char[len+1];
stringcopy(thestring,buff);
}
inline void setstatus(bool x)
{
search_status = x;
}
inline void setsearchstr(char* buff)
{
int len;
len = strlen(buff);
searchstr = new char[len+1];
stringcopy(searchstr,buff);
}
// return pointer to the string which the array holds data for
inline char* getstr()
{
return thestring;
}
inline char* getsearchstr()
{
return searchstr;
}
inline int getarraysize()
{
return array_size;
}
inline int getstringpoint(int index)
{
if(index <= array_size)
{
return dindex[index];
}
}
// maximum string points for search
inline int getmaxpoint()
{
return array_size;
}
// get status
inline bool getstatus()
{
return search_status;
}
inline bool wassearchsuccesful()
{
return getstatus();// was it succesfull?
}
};
// find string location (dumps all locations into the location data structure (MasStrDataLoc))
bool findstrloc(char* buff,char* search,MasStrDataLoc* dloc);// standard
bool findstrlocf(char* buff,char* search,MasStrDataLoc* dloc);// forward
bool findstrlocb(char* buff,char* search,MasStrDataLoc* dloc);// backward
// master string range structure
struct MasStrRange
{
int start_x;
int stop_y;
inline MasStrRange()
{
start_x = 0;
stop_y = 0;
}
inline void SetRange(int x, int y)
{
start_x = x;
stop_y = y;
}
inline int GetRangeX()
{
return start_x;
}
inline int GetRangeY()
{
return stop_y;
}
inline int GetRangeHI()
{
return start_x;
}
inline int GetRangeLO()
{
return stop_y;
}
inline int Clear()
{
start_x = 0;
stop_y = 0;
}
};
// range search structures for range style string manipulation
bool searchrange(char* buff,char* search,MasStrRange* mrang);
void copyleftrange(char* input,char* output,MasStrRange* mrang, int hi_lo);
void copyrightrange(char* input,char* output,MasStrRange* mrang,int hi_lo);
void copymidrange(char* input,char* output,MasStrRange* mrang, int hi_lo,MasStrRange* mrangx, int hi_lox);
bool isearchrange(int startx, char* buff,char* search,MasStrRange* mrang);
// master string list structure string data
struct MasStrList_String
{
char* thestring;
inline ~MasStrList_String()
{
delete [] thestring;
}
inline void set(char* buff)
{
int len;
len = strlen(buff) + 1;
thestring = new char[len];
stringcopy(thestring,buff);
}
inline char* get()
{
return thestring;
}
};
// master string list , list handler structure
struct MasStrList
{
MasStrList_String* strings;
int list_size;
inline ~MasStrList()
{
delete [] strings;
}
inline MasStrList(int list_size)
{
create(list_size);
}
inline MasStrList()
{
list_size = 0;
strings = NULL;
}
inline void create(int list_sizez)
{
list_size = list_sizez;
strings = new MasStrList_String[list_size];
}
inline char* getstr(int index)
{
return strings[index].get();
}
inline int getcount()
{
return list_size;
}
};
// create string list
void createstrlist(char* buffer, char* list_marker,MasStrList* list);
// charwrap easy to use char array, that automaticly removes itself from freestore
struct charwrap
{
char* str;
inline charwrap(int size)
{
str = new char[size];
}
inline charwrap(char* buff)
{
set(buff);
}
inline charwrap(char* buff,bool x)
{
set(buff);
strclearn();
}
inline ~charwrap()
{
delete [] str; // delete that shit
}
inline void set(char* buff)
{
int len;
len = strlen(buff) + 1;
str = new char[len];
stringcopy(str,buff);
}
inline void snew(int size)
{
str = new char[size];
}
// clear off freestore
inline void clear()
{
delete [] str;
str = NULL;
}
inline void strclear()
{
int len;
len = getlen(str);
for(int i = 0; i < len; i++)
{
str[i] = 0;
}
}
inline void strclearn()
{
stringcopy(str,"");
}
inline char* get()
{
return str;
}
inline int len()
{
return getlen(str);
}
inline void qmsg()
{
MessageBox(0,str,"QMSG",MB_OK|MB_ICONINFORMATION);
}
};
// structure of string manipulation algorithims to use for objects to inhert
struct MasStrObj
{
inline void mstr_stringcopy(char* input,char* output)
{
stringcopy(input,output);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -