Ich möchte meine Utils Klasse, welche meiner Meinung nach nützliche Funktionen enthält, der Öffentlichkeit nicht vorenthalten.
Folgende Funktionen sind zurzeit vorhanden:
CopySelf
StringRemoveLeft
StringRemoveRight
GetPCName
GetPCUsername
StringBetween
StringInString
Utils.h:
//CopyRight to gehaxelt.in
//www.gehaxelt.in
#ifndef UTILS_H
#define UTILS_H
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
class Utils
{
public:
Utils();
virtual ~Utils();
int CopySelf(string destinationfile,bool overwrite);
/** Copies the executed File to the given destination
# Parameter 1: Destitionfile to be copied to.
# Parameter 2: Specefies if an existing file should be overwritten (true: YES, false: NO)
# Return: 0: succeded , 1:error
**/
string StringRemoveLeft(string org,unsigned int count);
/** Removes count chars from the left-side of the given String
# Parameter 1: String to be shortened
# Parameter 2: Count of chars to remove
# Return: Shortened string or "" if count > string.len
**/
string StringRemoveRight(string org,unsigned int count);
/** Removes count chars from the right-side of the given String
# Parameter 1: String to be shortened
# Parameter 2: Count of chars to remove
# Return: Shortened string or "" if count > string.len
**/
string GetPCName();
/** Gets the name of the pc
Return: String with the PCName.
**/
string GetPCUserName();
/** Gets the name of the logged in user
Return: String with the username.
**/
string StringBetween(string org,string start, string stop);
/** Gets a string between 2 substrings
# Parameter 1: String to search for
# Parameter 2: Start of substring
# Parameter 3: End of substring
# Return: String between start & end or "" if fails
**/
int StringInString(string org,string sub);
/** Checks whether a string contains the substring
# Parameter 1: String to check
# Parameter 2: Substring to look for
# Return: 0 if not found, otherwise the position of the substring
**/
protected:
private:
};
#endif // UTILS_H
Utils.cpp:
#include "Utils.h"
Utils::Utils()
{
//ctor
}
Utils::~Utils()
{
//dtor
}
int Utils::CopySelf(string destinationfile,bool overwrite)
{
try {
TCHAR Pfad[MAX_PATH];
::GetModuleFileName(NULL, Pfad, MAX_PATH);
CopyFile(Pfad,destinationfile.c_str(),overwrite);
return 0;
} catch(...) {
return 1;
}
}
string Utils::StringRemoveRight(string org,unsigned int count)
{
string tmp="";
if(strlen(org.c_str())<count)
return "";
for(unsigned int i=0;i<=strlen(org.c_str())-count;i++)
tmp+=org[i];
return tmp;
}
string Utils::StringRemoveLeft(string org,unsigned int count)
{
string tmp="";
if(strlen(org.c_str())<count)
return "";
for(unsigned int i=count;i<=strlen(org.c_str());i++)
tmp+=org[i];
return tmp;
}
string Utils::GetPCName()
{
char pcname[255];
DWORD* size= new DWORD(sizeof( pcname ));
GetComputerName( pcname, size );
delete size;
return pcname;
}
string Utils::GetPCUserName()
{
char username[255];
DWORD* size= new DWORD(sizeof( username ));
GetUserName( username, size );
delete size;
return username;
}
string Utils::StringBetween(string org,string start, string stop)
{
unsigned int* begin= new unsigned int(org.find(start)+strlen(start.c_str()));
unsigned int* end= new unsigned int(org.find(stop));
if(*begin==string::npos || *end==string::npos || (*begin > *end)) {
delete begin;
delete end;
return "";
}
string* tmp = new string("");
for(unsigned int i=*begin;i<*end;i++)
*tmp+=org.at(i);
delete begin;
delete end;
return *tmp;
}
int Utils::StringInString(string org,string sub)
{
unsigned int* pos= new unsigned int(org.find(sub));
if(*pos==string::npos)
{
delete pos;
return 0;
}
return *pos;
}
Beispiel:
#include <iostream>
#include "Utils.h"
using namespace std;
int main()
{
Utils* util= new Utils;
cout << util->GetPCUserName()<<endl;
cout << util->GetPCName()<<endl;
cout << util->StringBetween("<h1>test</h1>","<h1>","</h1>")<<endl;
delete util;
return 0;
}
Ihr dürft diese Klasse frei nutzen, jedoch sollte der Copyrighthinweis bestehen bleiben und bei Erweiterung/Änderung würde ich mich über eine Nachricht freuen.
Gruß
gehaxelt