import comment
git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@1 b624d157-de02-0410-bad0-e51aec6abb33
This commit is contained in:
488
src/lib/IniReader.cpp
Executable file
488
src/lib/IniReader.cpp
Executable file
@@ -0,0 +1,488 @@
|
||||
// IniFile.cpp: Implementation of the CIniFile class.
|
||||
// Written by: Adam Clauss
|
||||
// Email: cabadam@houston.rr.com
|
||||
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
|
||||
// email suggested changes to me.
|
||||
//
|
||||
// Rewritten by: Shane Hill
|
||||
// Date: 21/08/2001
|
||||
// Email: Shane.Hill@dsto.defence.gov.au
|
||||
// Reason: Remove dependancy on MFC. Code should compile on any
|
||||
// platform.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// C++ Includes
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <strstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// C Includes
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Local Includes
|
||||
#include "IniReader.h"
|
||||
|
||||
#if defined(WIN32)
|
||||
#define iniEOL endl
|
||||
#else
|
||||
#define iniEOL '\r' << endl
|
||||
#endif
|
||||
|
||||
CIniFile::CIniFile( string const iniPath)
|
||||
{
|
||||
Path( iniPath);
|
||||
caseInsensitive = true;
|
||||
}
|
||||
|
||||
bool CIniFile::ReadFile()
|
||||
{
|
||||
// Normally you would use ifstream, but the SGI CC compiler has
|
||||
// a few bugs with ifstream. So ... fstream used.
|
||||
fstream f;
|
||||
string line;
|
||||
string keyname, valuename, value;
|
||||
string::size_type pLeft, pRight;
|
||||
|
||||
f.open( path.c_str(), ios::in);
|
||||
if ( f.fail())
|
||||
return false;
|
||||
|
||||
while( getline( f, line)) {
|
||||
// To be compatible with Win32, check for existence of '\r'.
|
||||
// Win32 files have the '\r' and Unix files don't at the end of a line.
|
||||
// Note that the '\r' will be written to INI files from
|
||||
// Unix so that the created INI file can be read under Win32
|
||||
// without change.
|
||||
if ( line[line.length() - 1] == '\r')
|
||||
line = line.substr( 0, line.length() - 1);
|
||||
|
||||
if ( line.length()) {
|
||||
// Check that the user hasn't openned a binary file by checking the first
|
||||
// character of each line!
|
||||
if ( !isprint( line[0])) {
|
||||
printf( "Failing on char %d\n", line[0]);
|
||||
f.close();
|
||||
return false;
|
||||
}
|
||||
if (( pLeft = line.find_first_of(";#[=")) != string::npos) {
|
||||
switch ( line[pLeft]) {
|
||||
case '[':
|
||||
if ((pRight = line.find_last_of("]")) != string::npos &&
|
||||
pRight > pLeft) {
|
||||
keyname = line.substr( pLeft + 1, pRight - pLeft - 1);
|
||||
AddKeyName( keyname);
|
||||
}
|
||||
break;
|
||||
|
||||
case '=':
|
||||
valuename = line.substr( 0, pLeft);
|
||||
value = line.substr( pLeft + 1);
|
||||
SetValue( keyname, valuename, value);
|
||||
break;
|
||||
|
||||
case ';':
|
||||
case '#':
|
||||
if ( !names.size())
|
||||
HeaderComment( line.substr( pLeft + 1));
|
||||
else
|
||||
KeyComment( keyname, line.substr( pLeft + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f.close();
|
||||
if ( names.size())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CIniFile::WriteFile()
|
||||
{
|
||||
unsigned commentID, keyID, valueID;
|
||||
// Normally you would use ofstream, but the SGI CC compiler has
|
||||
// a few bugs with ofstream. So ... fstream used.
|
||||
fstream f;
|
||||
|
||||
f.open( path.c_str(), ios::out);
|
||||
if ( f.fail())
|
||||
return false;
|
||||
|
||||
// Write header comments.
|
||||
for ( commentID = 0; commentID < comments.size(); ++commentID)
|
||||
f << ';' << comments[commentID] << iniEOL;
|
||||
if ( comments.size())
|
||||
f << iniEOL;
|
||||
|
||||
// Write keys and values.
|
||||
for ( keyID = 0; keyID < keys.size(); ++keyID) {
|
||||
f << '[' << names[keyID] << ']' << iniEOL;
|
||||
// Comments.
|
||||
for ( commentID = 0; commentID < keys[keyID].comments.size(); ++commentID)
|
||||
f << ';' << keys[keyID].comments[commentID] << iniEOL;
|
||||
// Values.
|
||||
for ( valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
||||
f << keys[keyID].names[valueID] << '=' << keys[keyID].values[valueID] << iniEOL;
|
||||
f << iniEOL;
|
||||
}
|
||||
f.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
long CIniFile::FindKey( string const keyname) const
|
||||
{
|
||||
for ( unsigned keyID = 0; keyID < names.size(); ++keyID)
|
||||
if ( CheckCase( names[keyID]) == CheckCase( keyname))
|
||||
return long(keyID);
|
||||
return noID;
|
||||
}
|
||||
|
||||
long CIniFile::FindValue( unsigned const keyID, string const valuename) const
|
||||
{
|
||||
if ( !keys.size() || keyID >= keys.size())
|
||||
return noID;
|
||||
|
||||
for ( unsigned valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
||||
if ( CheckCase( keys[keyID].names[valueID]) == CheckCase( valuename))
|
||||
return long(valueID);
|
||||
return noID;
|
||||
}
|
||||
|
||||
unsigned CIniFile::AddKeyName( string const keyname)
|
||||
{
|
||||
names.resize( names.size() + 1, keyname);
|
||||
keys.resize( keys.size() + 1);
|
||||
return names.size() - 1;
|
||||
}
|
||||
|
||||
string CIniFile::KeyName( unsigned const keyID) const
|
||||
{
|
||||
if ( keyID < names.size())
|
||||
return names[keyID];
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
unsigned CIniFile::NumValues( unsigned const keyID)
|
||||
{
|
||||
if ( keyID < keys.size())
|
||||
return keys[keyID].names.size();
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned CIniFile::NumValues( string const keyname)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return 0;
|
||||
return keys[keyID].names.size();
|
||||
}
|
||||
|
||||
string CIniFile::ValueName( unsigned const keyID, unsigned const valueID) const
|
||||
{
|
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size())
|
||||
return keys[keyID].names[valueID];
|
||||
return "";
|
||||
}
|
||||
|
||||
string CIniFile::ValueName( string const keyname, unsigned const valueID) const
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return "";
|
||||
return ValueName( keyID, valueID);
|
||||
}
|
||||
|
||||
bool CIniFile::SetValue( unsigned const keyID, unsigned const valueID, string const value)
|
||||
{
|
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size())
|
||||
keys[keyID].values[valueID] = value;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CIniFile::SetValue( string const keyname, string const valuename, string const value, bool const create)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID) {
|
||||
if ( create)
|
||||
keyID = long( AddKeyName( keyname));
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename);
|
||||
if ( valueID == noID) {
|
||||
if ( !create)
|
||||
return false;
|
||||
keys[keyID].names.resize( keys[keyID].names.size() + 1, valuename);
|
||||
keys[keyID].values.resize( keys[keyID].values.size() + 1, value);
|
||||
} else
|
||||
keys[keyID].values[valueID] = value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CIniFile::SetValueI( string const keyname, string const valuename, int const value, bool const create)
|
||||
{
|
||||
char svalue[MAX_VALUEDATA];
|
||||
|
||||
sprintf( svalue, "%d", value);
|
||||
return SetValue( keyname, valuename, svalue);
|
||||
}
|
||||
|
||||
bool CIniFile::SetValueF( string const keyname, string const valuename, double const value, bool const create)
|
||||
{
|
||||
char svalue[MAX_VALUEDATA];
|
||||
|
||||
sprintf( svalue, "%f", value);
|
||||
return SetValue( keyname, valuename, svalue);
|
||||
}
|
||||
|
||||
bool CIniFile::SetValueV( string const keyname, string const valuename, char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
char value[MAX_VALUEDATA];
|
||||
|
||||
va_start( args, format);
|
||||
vsprintf( value, format, args);
|
||||
va_end( args);
|
||||
return SetValue( keyname, valuename, value);
|
||||
}
|
||||
|
||||
string CIniFile::GetValue( unsigned const keyID, unsigned const valueID, string const defValue) const
|
||||
{
|
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size())
|
||||
return keys[keyID].values[valueID];
|
||||
return defValue;
|
||||
}
|
||||
|
||||
string CIniFile::GetValue( string const keyname, string const valuename, string const defValue) const
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return defValue;
|
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename);
|
||||
if ( valueID == noID)
|
||||
return defValue;
|
||||
|
||||
return keys[keyID].values[valueID];
|
||||
}
|
||||
|
||||
int CIniFile::GetValueI(string const keyname, string const valuename, int const defValue) const
|
||||
{
|
||||
char svalue[MAX_VALUEDATA];
|
||||
|
||||
sprintf( svalue, "%d", defValue);
|
||||
return atoi( GetValue( keyname, valuename, svalue).c_str());
|
||||
}
|
||||
|
||||
double CIniFile::GetValueF(string const keyname, string const valuename, double const defValue) const
|
||||
{
|
||||
char svalue[MAX_VALUEDATA];
|
||||
|
||||
sprintf( svalue, "%f", defValue);
|
||||
return atof( GetValue( keyname, valuename, svalue).c_str());
|
||||
}
|
||||
|
||||
// 16 variables may be a bit of over kill, but hey, it's only code.
|
||||
unsigned CIniFile::GetValueV( string const keyname, string const valuename, char *format,
|
||||
void *v1, void *v2, void *v3, void *v4,
|
||||
void *v5, void *v6, void *v7, void *v8,
|
||||
void *v9, void *v10, void *v11, void *v12,
|
||||
void *v13, void *v14, void *v15, void *v16)
|
||||
{
|
||||
string value;
|
||||
// va_list args;
|
||||
unsigned nVals;
|
||||
|
||||
|
||||
value = GetValue( keyname, valuename);
|
||||
if ( !value.length())
|
||||
return false;
|
||||
// Why is there not vsscanf() function. Linux man pages say that there is
|
||||
// but no compiler I've seen has it defined. Bummer!
|
||||
//
|
||||
// va_start( args, format);
|
||||
// nVals = vsscanf( value.c_str(), format, args);
|
||||
// va_end( args);
|
||||
|
||||
nVals = sscanf( value.c_str(), format,
|
||||
v1, v2, v3, v4, v5, v6, v7, v8,
|
||||
v9, v10, v11, v12, v13, v14, v15, v16);
|
||||
|
||||
return nVals;
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteValue( string const keyname, string const valuename)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return false;
|
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename);
|
||||
if ( valueID == noID)
|
||||
return false;
|
||||
|
||||
// This looks strange, but is neccessary.
|
||||
vector<string>::iterator npos = keys[keyID].names.begin() + valueID;
|
||||
vector<string>::iterator vpos = keys[keyID].values.begin() + valueID;
|
||||
keys[keyID].names.erase( npos, npos + 1);
|
||||
keys[keyID].values.erase( vpos, vpos + 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteKey( string const keyname)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return false;
|
||||
|
||||
// Now hopefully this destroys the vector lists within keys.
|
||||
// Looking at <vector> source, this should be the case using the destructor.
|
||||
// If not, I may have to do it explicitly. Memory leak check should tell.
|
||||
// memleak_test.cpp shows that the following not required.
|
||||
//keys[keyID].names.clear();
|
||||
//keys[keyID].values.clear();
|
||||
|
||||
vector<string>::iterator npos = names.begin() + keyID;
|
||||
vector<key>::iterator kpos = keys.begin() + keyID;
|
||||
names.erase( npos, npos + 1);
|
||||
keys.erase( kpos, kpos + 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CIniFile::Erase()
|
||||
{
|
||||
// This loop not needed. The vector<> destructor seems to do
|
||||
// all the work itself. memleak_test.cpp shows this.
|
||||
//for ( unsigned i = 0; i < keys.size(); ++i) {
|
||||
// keys[i].names.clear();
|
||||
// keys[i].values.clear();
|
||||
//}
|
||||
names.clear();
|
||||
keys.clear();
|
||||
comments.clear();
|
||||
}
|
||||
|
||||
void CIniFile::HeaderComment( string const comment)
|
||||
{
|
||||
comments.resize( comments.size() + 1, comment);
|
||||
}
|
||||
|
||||
string CIniFile::HeaderComment( unsigned const commentID) const
|
||||
{
|
||||
if ( commentID < comments.size())
|
||||
return comments[commentID];
|
||||
return "";
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteHeaderComment( unsigned commentID)
|
||||
{
|
||||
if ( commentID < comments.size()) {
|
||||
vector<string>::iterator cpos = comments.begin() + commentID;
|
||||
comments.erase( cpos, cpos + 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned CIniFile::NumKeyComments( unsigned const keyID) const
|
||||
{
|
||||
if ( keyID < keys.size())
|
||||
return keys[keyID].comments.size();
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned CIniFile::NumKeyComments( string const keyname) const
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return 0;
|
||||
return keys[keyID].comments.size();
|
||||
}
|
||||
|
||||
bool CIniFile::KeyComment( unsigned const keyID, string const comment)
|
||||
{
|
||||
if ( keyID < keys.size()) {
|
||||
keys[keyID].comments.resize( keys[keyID].comments.size() + 1, comment);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CIniFile::KeyComment( string const keyname, string const comment)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return false;
|
||||
return KeyComment( unsigned(keyID), comment);
|
||||
}
|
||||
|
||||
string CIniFile::KeyComment( unsigned const keyID, unsigned const commentID) const
|
||||
{
|
||||
if ( keyID < keys.size() && commentID < keys[keyID].comments.size())
|
||||
return keys[keyID].comments[commentID];
|
||||
return "";
|
||||
}
|
||||
|
||||
string CIniFile::KeyComment( string const keyname, unsigned const commentID) const
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return "";
|
||||
return KeyComment( unsigned(keyID), commentID);
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteKeyComment( unsigned const keyID, unsigned const commentID)
|
||||
{
|
||||
if ( keyID < keys.size() && commentID < keys[keyID].comments.size()) {
|
||||
vector<string>::iterator cpos = keys[keyID].comments.begin() + commentID;
|
||||
keys[keyID].comments.erase( cpos, cpos + 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteKeyComment( string const keyname, unsigned const commentID)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return false;
|
||||
return DeleteKeyComment( unsigned(keyID), commentID);
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteKeyComments( unsigned const keyID)
|
||||
{
|
||||
if ( keyID < keys.size()) {
|
||||
keys[keyID].comments.clear();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CIniFile::DeleteKeyComments( string const keyname)
|
||||
{
|
||||
long keyID = FindKey( keyname);
|
||||
if ( keyID == noID)
|
||||
return false;
|
||||
return DeleteKeyComments( unsigned(keyID));
|
||||
}
|
||||
|
||||
string CIniFile::CheckCase( string s) const
|
||||
{
|
||||
if ( caseInsensitive)
|
||||
for ( string::size_type i = 0; i < s.length(); ++i)
|
||||
s[i] = tolower(s[i]);
|
||||
return s;
|
||||
}
|
||||
180
src/lib/IniReader.h
Executable file
180
src/lib/IniReader.h
Executable file
@@ -0,0 +1,180 @@
|
||||
// IniFile.cpp: Implementation of the CIniFile class.
|
||||
// Written by: Adam Clauss
|
||||
// Email: cabadam@tamu.edu
|
||||
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
|
||||
// email suggested changes to me.
|
||||
//
|
||||
// Rewritten by: Shane Hill
|
||||
// Date: 21/08/2001
|
||||
// Email: Shane.Hill@dsto.defence.gov.au
|
||||
// Reason: Remove dependancy on MFC. Code should compile on any
|
||||
// platform. Tested on Windows/Linux/Irix
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef CIniFile_H
|
||||
#define CIniFile_H
|
||||
using namespace std;
|
||||
// C++ Includes
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// C Includes
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_KEYNAME 128
|
||||
#define MAX_VALUENAME 128
|
||||
#define MAX_VALUEDATA 2048
|
||||
|
||||
class CIniFile
|
||||
{
|
||||
private:
|
||||
bool caseInsensitive;
|
||||
string path;
|
||||
struct key {
|
||||
vector<string> names;
|
||||
vector<string> values;
|
||||
vector<string> comments;
|
||||
};
|
||||
vector<key> keys;
|
||||
vector<string> names;
|
||||
vector<string> comments;
|
||||
string CheckCase( string s) const;
|
||||
|
||||
public:
|
||||
enum errors{ noID = -1};
|
||||
CIniFile( string const iniPath = "");
|
||||
virtual ~CIniFile() {}
|
||||
|
||||
// Sets whether or not keynames and valuenames should be case sensitive.
|
||||
// The default is case insensitive.
|
||||
void CaseSensitive() {caseInsensitive = false;}
|
||||
void CaseInsensitive() {caseInsensitive = true;}
|
||||
|
||||
// Sets path of ini file to read and write from.
|
||||
void Path(string const newPath) {path = newPath;}
|
||||
string Path() const {return path;}
|
||||
void SetPath(string const newPath) {Path( newPath);}
|
||||
|
||||
// Reads ini file specified using path.
|
||||
// Returns true if successful, false otherwise.
|
||||
bool ReadFile();
|
||||
|
||||
// Writes data stored in class to ini file.
|
||||
bool WriteFile();
|
||||
|
||||
// Deletes all stored ini data.
|
||||
void Erase();
|
||||
void Clear() {Erase();}
|
||||
void Reset() {Erase();}
|
||||
|
||||
// Returns index of specified key, or noID if not found.
|
||||
long FindKey( string const keyname) const;
|
||||
|
||||
// Returns index of specified value, in the specified key, or noID if not found.
|
||||
long FindValue( unsigned const keyID, string const valuename) const;
|
||||
|
||||
// Returns number of keys currently in the ini.
|
||||
unsigned NumKeys() const {return names.size();}
|
||||
unsigned GetNumKeys() const {return NumKeys();}
|
||||
|
||||
// Add a key name.
|
||||
unsigned AddKeyName( string const keyname);
|
||||
|
||||
// Returns key names by index.
|
||||
string KeyName( unsigned const keyID) const;
|
||||
string GetKeyName( unsigned const keyID) const {return KeyName(keyID);}
|
||||
|
||||
// Returns number of values stored for specified key.
|
||||
unsigned NumValues( unsigned const keyID);
|
||||
unsigned GetNumValues( unsigned const keyID) {return NumValues( keyID);}
|
||||
unsigned NumValues( string const keyname);
|
||||
unsigned GetNumValues( string const keyname) {return NumValues( keyname);}
|
||||
|
||||
// Returns value name by index for a given keyname or keyID.
|
||||
string ValueName( unsigned const keyID, unsigned const valueID) const;
|
||||
string GetValueName( unsigned const keyID, unsigned const valueID) const {
|
||||
return ValueName( keyID, valueID);
|
||||
}
|
||||
string ValueName( string const keyname, unsigned const valueID) const;
|
||||
string GetValueName( string const keyname, unsigned const valueID) const {
|
||||
return ValueName( keyname, valueID);
|
||||
}
|
||||
|
||||
// Gets value of [keyname] valuename =.
|
||||
// Overloaded to return string, int, and double.
|
||||
// Returns defValue if key/value not found.
|
||||
string GetValue( unsigned const keyID, unsigned const valueID, string const defValue = "") const;
|
||||
string GetValue(string const keyname, string const valuename, string const defValue = "") const;
|
||||
int GetValueI(string const keyname, string const valuename, int const defValue = 0) const;
|
||||
bool GetValueB(string const keyname, string const valuename, bool const defValue = false) const {
|
||||
return bool( GetValueI( keyname, valuename, int( defValue)));
|
||||
}
|
||||
double GetValueF(string const keyname, string const valuename, double const defValue = 0.0) const;
|
||||
// This is a variable length formatted GetValue routine. All these voids
|
||||
// are required because there is no vsscanf() like there is a vsprintf().
|
||||
// Only a maximum of 8 variable can be read.
|
||||
unsigned GetValueV( string const keyname, string const valuename, char *format,
|
||||
void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0,
|
||||
void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0,
|
||||
void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0,
|
||||
void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0);
|
||||
|
||||
// Sets value of [keyname] valuename =.
|
||||
// Specify the optional paramter as false (0) if you do not want it to create
|
||||
// the key if it doesn't exist. Returns true if data entered, false otherwise.
|
||||
// Overloaded to accept string, int, and double.
|
||||
bool SetValue( unsigned const keyID, unsigned const valueID, string const value);
|
||||
bool SetValue( string const keyname, string const valuename, string const value, bool const create = true);
|
||||
bool SetValueI( string const keyname, string const valuename, int const value, bool const create = true);
|
||||
bool SetValueB( string const keyname, string const valuename, bool const value, bool const create = true) {
|
||||
return SetValueI( keyname, valuename, int(value), create);
|
||||
}
|
||||
bool SetValueF( string const keyname, string const valuename, double const value, bool const create = true);
|
||||
bool SetValueV( string const keyname, string const valuename, char *format, ...);
|
||||
|
||||
// Deletes specified value.
|
||||
// Returns true if value existed and deleted, false otherwise.
|
||||
bool DeleteValue( string const keyname, string const valuename);
|
||||
|
||||
// Deletes specified key and all values contained within.
|
||||
// Returns true if key existed and deleted, false otherwise.
|
||||
bool DeleteKey(string keyname);
|
||||
|
||||
// Header comment functions.
|
||||
// Header comments are those comments before the first key.
|
||||
//
|
||||
// Number of header comments.
|
||||
unsigned NumHeaderComments() {return comments.size();}
|
||||
// Add a header comment.
|
||||
void HeaderComment( string const comment);
|
||||
// Return a header comment.
|
||||
string HeaderComment( unsigned const commentID) const;
|
||||
// Delete a header comment.
|
||||
bool DeleteHeaderComment( unsigned commentID);
|
||||
// Delete all header comments.
|
||||
void DeleteHeaderComments() {comments.clear();}
|
||||
|
||||
// Key comment functions.
|
||||
// Key comments are those comments within a key. Any comments
|
||||
// defined within value names will be added to this list. Therefore,
|
||||
// these comments will be moved to the top of the key definition when
|
||||
// the CIniFile::WriteFile() is called.
|
||||
//
|
||||
// Number of key comments.
|
||||
unsigned NumKeyComments( unsigned const keyID) const;
|
||||
unsigned NumKeyComments( string const keyname) const;
|
||||
// Add a key comment.
|
||||
bool KeyComment( unsigned const keyID, string const comment);
|
||||
bool KeyComment( string const keyname, string const comment);
|
||||
// Return a key comment.
|
||||
string KeyComment( unsigned const keyID, unsigned const commentID) const;
|
||||
string KeyComment( string const keyname, unsigned const commentID) const;
|
||||
// Delete a key comment.
|
||||
bool DeleteKeyComment( unsigned const keyID, unsigned const commentID);
|
||||
bool DeleteKeyComment( string const keyname, unsigned const commentID);
|
||||
// Delete all comments for a key.
|
||||
bool DeleteKeyComments( unsigned const keyID);
|
||||
bool DeleteKeyComments( string const keyname);
|
||||
};
|
||||
|
||||
#endif
|
||||
175
src/lib/PwmTime.cpp
Normal file
175
src/lib/PwmTime.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* mail@tarek-saidi.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include "PwmTime.h"
|
||||
|
||||
|
||||
void CPwmTime::Set(unsigned char* pBytes){
|
||||
UINT32 dw1, dw2, dw3, dw4, dw5;
|
||||
dw1 = (UINT32)pBytes[0]; dw2 = (UINT32)pBytes[1]; dw3 = (UINT32)pBytes[2];
|
||||
dw4 = (UINT32)pBytes[3]; dw5 = (UINT32)pBytes[4];
|
||||
// Unpack 5 byte structure to date and time
|
||||
///@FIXME nicht Endian-sicher
|
||||
Year = (dw1 << 6) | (dw2 >> 2);
|
||||
Month = ((dw2 & 0x00000003) << 2) | (dw3 >> 6);
|
||||
Day = (dw3 >> 1) & 0x0000001F;
|
||||
Hour = ((dw3 & 0x00000001) << 4) | (dw4 >> 4);
|
||||
Minute = ((dw4 & 0x0000000F) << 2) | (dw5 >> 6);
|
||||
Second = dw5 & 0x0000003F;
|
||||
}
|
||||
|
||||
void CPwmTime::GetPackedTime(unsigned char* pBytes){
|
||||
///@FIXME nicht Endian-sicher
|
||||
pBytes[0] = (UINT8)(((UINT32)Year >> 6) & 0x0000003F);
|
||||
pBytes[1] = (UINT8)((((UINT32)Year & 0x0000003F) << 2) | (((UINT32)Month >> 2) & 0x00000003));
|
||||
pBytes[2] = (UINT8)((((UINT32)Month & 0x00000003) << 6) | (((UINT32)Day & 0x0000001F) << 1) | (((UINT32)Hour >> 4) & 0x00000001));
|
||||
pBytes[3] = (UINT8)((((UINT32)Hour & 0x0000000F) << 4) | (((UINT32)Minute >> 2) & 0x0000000F));
|
||||
pBytes[4] = (UINT8)((((UINT32)Minute & 0x00000003) << 6) | ((UINT32)Second & 0x0000003F));
|
||||
}
|
||||
|
||||
QString CPwmTime::GetString(UINT16 format){
|
||||
QString str;
|
||||
switch(format){
|
||||
case 0:
|
||||
//DD.MM.YYYY HH:MM:SS
|
||||
str=(QString)"%1.%2.%3 %4:%5:%6";
|
||||
break;
|
||||
|
||||
case 1:
|
||||
//MM/DD/YYYY HH:MM:SS
|
||||
str=(QString)"%2/%1/%3 %4:%5:%6";
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
if(Day<10){
|
||||
str=str.arg(((QString)"0"+QString::number(Day)));
|
||||
}else{
|
||||
str=str.arg(Day);}
|
||||
|
||||
if(Month<10){
|
||||
str=str.arg(((QString)"0"+QString::number(Month)));
|
||||
}else{
|
||||
str=str.arg(Month);}
|
||||
|
||||
str=str.arg(Year);
|
||||
|
||||
if(Hour<10){
|
||||
str=str.arg(((QString)"0"+QString::number(Hour)));
|
||||
}else{
|
||||
str=str.arg(Hour);}
|
||||
|
||||
if(Minute<10){
|
||||
str=str.arg(((QString)"0"+QString::number(Minute)));
|
||||
}else{
|
||||
str=str.arg(Minute);}
|
||||
|
||||
if(Second<10){
|
||||
str=str.arg(((QString)"0"+QString::number(Second)));
|
||||
}else{
|
||||
str=str.arg(Second);}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void CPwmTime::Set(UINT8 iDay,UINT8 iMonth,UINT16 iYear,UINT8 iHour,UINT8 iMinute,UINT8 iSecond){
|
||||
Day=iDay;
|
||||
Month=iMonth;
|
||||
Year=iYear;
|
||||
Hour=iHour;
|
||||
Minute=iMinute;
|
||||
Second=iSecond;
|
||||
}
|
||||
|
||||
bool CPwmTime::IsValidDate(QString& s){
|
||||
|
||||
if(s.length()>10)return false;
|
||||
|
||||
int count=0;
|
||||
for(int i=0;i<s.length();i++){
|
||||
if(s[i]=='.')count++;
|
||||
}
|
||||
if(count!=2)return false;
|
||||
bool conv_error[3];
|
||||
int day=s.section(".",0,0).toInt(&conv_error[0]);
|
||||
int month=s.section(".",1,1).toInt(&conv_error[1]);
|
||||
int year=s.section(".",2,2).toInt(&conv_error[2]);
|
||||
if(conv_error[0]==false || conv_error[1]==false || conv_error[2]==false)return false;
|
||||
if(day>31 || day<1 || month>12 || month<1 || year<0 || year>2999)return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPwmTime::IsValidTime(QString& s){
|
||||
|
||||
if(s.length()>8)return false;
|
||||
|
||||
int count=0;
|
||||
for(int i=0;i<s.length();i++){
|
||||
if(s[i]==':')count++;
|
||||
}
|
||||
if(count!=2)return false;
|
||||
bool conv_error[3];
|
||||
int hour=s.section(":",0,0).toInt(&conv_error[0]);
|
||||
int minute=s.section(":",1,1).toInt(&conv_error[1]);
|
||||
int second=s.section(":",2,2).toInt(&conv_error[2]);
|
||||
if(conv_error[0]==false || conv_error[1]==false || conv_error[2]==false)return false;
|
||||
if(hour>23 || hour<0 || minute>59 || minute<0 || second>59 || second<0)return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPwmTime::SetDate(QString s){
|
||||
if(IsValidDate(s)==false)return false;
|
||||
Day=s.section(".",0,0).toInt();
|
||||
Month=s.section(".",1,1).toInt();
|
||||
Year=s.section(".",2,2).toInt();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPwmTime::SetTime(QString s){
|
||||
if(IsValidTime(s)==false)return false;
|
||||
Hour=s.section(":",0,0).toInt();
|
||||
Minute=s.section(":",1,1).toInt();
|
||||
Second=s.section(":",2,2).toInt();
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPwmTime::SetToNow(){
|
||||
time_t curTime=time(NULL);
|
||||
tm* current=localtime(&curTime);
|
||||
Year=current->tm_year+1900;
|
||||
Month=current->tm_mon;
|
||||
Day=current->tm_mday;
|
||||
Hour=current->tm_hour;
|
||||
Minute=current->tm_min;
|
||||
Second=current->tm_sec;
|
||||
}
|
||||
|
||||
bool CPwmTime::operator==(const CPwmTime& t){
|
||||
if( Year==t.Year
|
||||
&& Month==t.Month
|
||||
&& Day==t.Day
|
||||
&& Hour==t.Hour
|
||||
&& Minute==t.Minute
|
||||
&& Second==t.Second) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool CPwmTime::operator!=(const CPwmTime& t){
|
||||
return !(*this==t);
|
||||
}
|
||||
51
src/lib/PwmTime.h
Normal file
51
src/lib/PwmTime.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* mail@tarek-saidi.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef _PWMTIME_H_
|
||||
#define _PWMTIME_H_
|
||||
|
||||
#include <qglobal.h>
|
||||
#include <qstring.h>
|
||||
class CPwmTime;
|
||||
class CPwmTime{
|
||||
public:
|
||||
UINT16 Year;
|
||||
UINT8 Month;
|
||||
UINT8 Day;
|
||||
UINT8 Hour;
|
||||
UINT8 Minute;
|
||||
UINT8 Second;
|
||||
|
||||
QString GetString(UINT16 format);
|
||||
void Set(UINT8,UINT8,UINT16,UINT8,UINT8,UINT8);
|
||||
void Set(unsigned char* packedTime);
|
||||
void GetPackedTime(unsigned char* dst);
|
||||
static bool IsValidDate(QString& string);
|
||||
static bool IsValidTime(QString& string);
|
||||
bool SetDate(QString string);
|
||||
bool SetTime(QString string);
|
||||
void SetToNow();
|
||||
bool operator==(const CPwmTime& t);
|
||||
bool operator!=(const CPwmTime& t);
|
||||
// bool operator>(const CPwmTime& t);
|
||||
// bool operator<(const CPwmTime& t);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
105
src/lib/SecString.cpp
Executable file
105
src/lib/SecString.cpp
Executable file
@@ -0,0 +1,105 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* tarek@linux *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "SecString.h"
|
||||
#include <qmessagebox.h>
|
||||
#include <iostream.h>
|
||||
#include "random.h"
|
||||
|
||||
UINT8 SecString::Key[32]={0};
|
||||
|
||||
SecString::SecString(){
|
||||
data=NULL;
|
||||
len=0;
|
||||
cryptlen=0;
|
||||
}
|
||||
|
||||
|
||||
SecString::~SecString(){
|
||||
//if(data)delete [] data; ///@FIXME zerschießt den Stack, aber warum???
|
||||
overwrite(plaintext);
|
||||
}
|
||||
|
||||
void SecString::getString(QString & str){
|
||||
if(data){
|
||||
Rijndael aes;
|
||||
int r=aes.init(Rijndael::CBC, Rijndael::Decrypt,Key,Rijndael::Key32Bytes);
|
||||
if(r){ cout << "AES error, code " << r << endl;
|
||||
exit(-1);}
|
||||
char* out=new char[len];
|
||||
r=aes.padDecrypt((unsigned char*)data,cryptlen,(unsigned char*)out);
|
||||
if(r!=len){ cout << "AES error in SecString::getString(), r!=length, r=" << r << endl;
|
||||
exit(-1);}
|
||||
str=QString::fromUtf8(out,len);
|
||||
overwrite(out,len);
|
||||
delete [] out;
|
||||
}
|
||||
}
|
||||
|
||||
QString& SecString::getString(){
|
||||
getString(plaintext);
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
void SecString::delRef(){
|
||||
overwrite(plaintext);
|
||||
}
|
||||
|
||||
void SecString::setString(QString& str,bool DelSrc){
|
||||
Rijndael aes;
|
||||
int r=aes.init(Rijndael::CBC, Rijndael::Encrypt,Key,Rijndael::Key32Bytes);
|
||||
if(r){ cout << "AES error, code " << r << endl;
|
||||
exit(-1);}
|
||||
int il=str.length();
|
||||
char* input=new char[il];
|
||||
char* output=new char[il+16];
|
||||
memcpy(input,str.utf8(),il);
|
||||
r=aes.padEncrypt((unsigned char*)input,il,(unsigned char*)output);
|
||||
if(r<0){ cout << "AES error, code " << r << endl;
|
||||
exit(-1);}
|
||||
cryptlen=r;
|
||||
len=il;
|
||||
if(data)delete [] data;
|
||||
data=output;
|
||||
overwrite(input,il);
|
||||
delete [] input;
|
||||
if(DelSrc)overwrite(str);
|
||||
}
|
||||
|
||||
void SecString::overwrite(char* str,int strlen){
|
||||
if(strlen==0 || str==NULL)return;
|
||||
getRandomBytes(str,strlen,1,false);
|
||||
}
|
||||
|
||||
void SecString::overwrite(QString &str){
|
||||
if(str.length()==0)return;
|
||||
char* tmp=new char[str.length()];
|
||||
getRandomBytes(tmp,str.length(),1,false);
|
||||
str=tmp;
|
||||
delete [] tmp;
|
||||
}
|
||||
|
||||
int SecString::length(){
|
||||
return len;
|
||||
}
|
||||
|
||||
void SecString::generateSessionKey(){
|
||||
getRandomBytes(Key,32,1,false);
|
||||
}
|
||||
51
src/lib/SecString.h
Executable file
51
src/lib/SecString.h
Executable file
@@ -0,0 +1,51 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* tarek@linux *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef _SECSTRING_H_
|
||||
#define _SECSTRING_H_
|
||||
|
||||
#include <qstring.h>
|
||||
#include <qglobal.h>
|
||||
#include "crypto/rijndael.h"
|
||||
|
||||
class SecString{
|
||||
public:
|
||||
SecString(unsigned char* key);
|
||||
SecString();
|
||||
~SecString();
|
||||
|
||||
void getString(QString& str);
|
||||
QString& getString();
|
||||
void setString(QString& str, bool DelSrc=false);
|
||||
void delRef();
|
||||
static void overwrite(char* str,int len);
|
||||
static void overwrite(QString& str);
|
||||
int length();
|
||||
static void generateSessionKey();
|
||||
|
||||
private:
|
||||
static UINT8 Key[32];
|
||||
QString plaintext;
|
||||
char* data;
|
||||
int len;
|
||||
int cryptlen;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
55
src/lib/UrlLabel.cpp
Executable file
55
src/lib/UrlLabel.cpp
Executable file
@@ -0,0 +1,55 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* tarek@linux *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "UrlLabel.h"
|
||||
#include <qfont.h>
|
||||
#include <qcolor.h>
|
||||
#include <qcursor.h>
|
||||
#include <qfontmetrics.h>
|
||||
|
||||
LinkLabel::LinkLabel(QWidget *parent, const char* name,const QString& text, int x, int y,WFlags f) : QLabel(parent,name,f){
|
||||
QFont font(parentWidget()->font()); font.setUnderline(true);
|
||||
setFont(font);
|
||||
setPaletteForegroundColor(QColor(20,20,255));
|
||||
setCursor(PointingHandCursor);
|
||||
setText(text);
|
||||
setPos(x,y);
|
||||
}
|
||||
|
||||
LinkLabel::~LinkLabel(){
|
||||
|
||||
}
|
||||
|
||||
void LinkLabel::mouseReleaseEvent(QMouseEvent* event){
|
||||
if(event->button()==LeftButton)
|
||||
clicked();
|
||||
}
|
||||
|
||||
void LinkLabel::setPos(int x,int y){
|
||||
QFontMetrics fm(font());
|
||||
setGeometry(x,y,fm.width(text()),fm.height());
|
||||
|
||||
}
|
||||
|
||||
void LinkLabel::setText(const QString& text){
|
||||
QLabel::setText(text);
|
||||
setPos(geometry().x(),geometry().y());
|
||||
}
|
||||
45
src/lib/UrlLabel.h
Executable file
45
src/lib/UrlLabel.h
Executable file
@@ -0,0 +1,45 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* tarek@linux *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _LINKLABEL_H_
|
||||
#define _LINKLABEL_H_
|
||||
#include <qlabel.h>
|
||||
#include <qsignal.h>
|
||||
|
||||
class LinkLabel : public QLabel{
|
||||
Q_OBJECT
|
||||
public:
|
||||
LinkLabel(QWidget *parent, const char* name = 0,const QString& text=QString::null, int x=0, int y=0,WFlags f=0);
|
||||
~LinkLabel();
|
||||
void setPos(int x,int y);
|
||||
|
||||
public slots:
|
||||
virtual void setText(const QString&);
|
||||
|
||||
|
||||
signals:
|
||||
void clicked();
|
||||
|
||||
protected:
|
||||
virtual void mouseReleaseEvent(QMouseEvent* event);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
47
src/lib/random.cpp
Executable file
47
src/lib/random.cpp
Executable file
@@ -0,0 +1,47 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* mail@tarek-saidi.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include <iostream.h>
|
||||
#include <fstream.h>
|
||||
#include <qdatetime.h>
|
||||
#include <qobject.h>
|
||||
#include "random.h"
|
||||
|
||||
void getRandomBytes(void* buffer,int NumBlocks,int BlockSize,bool Strong){
|
||||
FILE *dev_random;
|
||||
if(Strong){
|
||||
dev_random = fopen("/dev/random","r");}
|
||||
else{
|
||||
dev_random = fopen("/dev/urandom","r");}
|
||||
|
||||
if (dev_random==NULL){
|
||||
cout << QObject::tr("/dev/random konnte nicht geöffnet werden - nutze Standardbibliothek (stdlib)") << endl;
|
||||
srand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
||||
for(int i=0;i<NumBlocks*BlockSize;i++){
|
||||
UINT8 rnd=rand()%256;
|
||||
((UINT8*)buffer)[i]=rnd;
|
||||
}
|
||||
return;
|
||||
}
|
||||
else{
|
||||
fread(buffer,BlockSize,NumBlocks,dev_random);
|
||||
fclose(dev_random);
|
||||
return;
|
||||
}
|
||||
}
|
||||
20
src/lib/random.h
Executable file
20
src/lib/random.h
Executable file
@@ -0,0 +1,20 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Tarek Saidi *
|
||||
* mail@tarek-saidi.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
extern void getRandomBytes(void* buffer,int NumBlocks,int BlockSize,bool Strong=false);
|
||||
Reference in New Issue
Block a user