LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2006, 03:00 AM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Berkelet DB Programming Rescue


Please help me out. I've been staring at it for ten ages
================================================
#include <iostream>
#include <fstream>
#include <string>
#include <db_cxx.h>

Db db(NULL, 0);


u_int32_t oFlags = DB_CREATE;


class MyDb
{
public:
MyDb (std::string& path, std::string& dbName, bool isSecondary);
~MyDb() { } //close(); }


inline Db &getDb() { return db_; }


private:
Db db_;
std::string dbFileName_;
u_int32_t cFlags_;


MyDb() : db_(NULL, 0) { }


void close();



};


MyDb::MyDb (std::string &path, std::string &dbName,
bool isSecondary)
: db_(NULL, 0),
dbFileName_ (path + dbName),
cFlags_ (DB_CREATE)
{
try
{
db_.set_error_stream (&std::cerr);

if (isSecondary)
db_.set_flags (DB_DUPSORT);


db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_,
0);
}
catch (DbException &e)
{
std::cerr << "Error opening database: " << dbFileName_ << "\n";
std::cerr << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cerr << "Error opening database: " << dbFileName_ << "\n";
std::cerr << e.what() << std::endl;
}



}


void MyDb::close()
{

try
{
db_.close(0);
std::cout << "Database " << dbFileName_
<< " is closed." << std::endl;
}
catch (DbException &e)
{
std::cerr << "Error closing database: " << dbFileName_ << "\n";
std::cerr << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cerr << "Error closing database: " << dbFileName_ << "\n";
std::cerr << e.what() << std::endl;
}



}


class Plumedata
{
private:
float m_CO;
float m_NOX;
float m_HC;
float m_CO2;
size_t bufLen_;
float *databuf_;
public:
inline void setCO(float co) { m_CO = co; }
inline void setNOX(float nox) { m_NOX = nox; }
inline void setHC (float hc) { m_HC = hc; }
inline void setCO2 (float co2) { m_CO2 = co2; }

inline float& getCO() { return m_CO; }
inline float& getNOX() { return m_NOX; }
inline float& getHC() { return m_HC; }
inline float& getCO2() { return m_CO2; }
inline void setBufferLen(size_t &len) { bufLen_ = len; }
inline size_t& getBufferSize() { return bufLen_; }
Plumedata() { clear();}// databuf_ = (float *) malloc(500); }
~Plumedata() { }
// Plumedata(void *buffer);


void clear();
float *getBuffer();
void show();
void load_database(MyDb& dataDB, std::string &dataFile);
int GetNextPound(std::string& , std::string&);



};


//void load_database (MyDb& dataDB, std::string &dataFile);

//void loadPlumeDB( MyDb &, std::string&);


using namespace std;


void Plumedata::clear()
{
m_CO = 0.0;
m_NOX = 0.0;
m_HC = 0.0;
m_CO2 = 0.0;



}


/////////////////////////////
// Get Data from buffer
/*Plumedata::Plumedata(void *buffer)
{
char *buf = (char *)buffer;

m_CO = *((float *)buf);
bufLen_ = sizeof(float);


m_NOX = *((float *)buf);
bufLen_ += sizeof(float);


m_HC = *((float *)buf);
bufLen_ += sizeof(float);


m_CO2 = *((float *)buf);
bufLen_ += sizeof(float);


setBufferLen (bufLen_);
}*/


/////////////////////////////////////////////
// Get Dataset from memory and put it in DB


float *Plumedata::getBuffer(void)
{
databuf_ = (float *)malloc (500);
memset( (float *)databuf_ ,(float) 0, 500 );
bufLen_ = 0;
int dataLen = 0;


dataLen = sizeof(float);
memcpy (databuf_, &m_CO, dataLen);
// bufLen_ += dataLen;
databuf_++;


memcpy (databuf_, &m_NOX, dataLen);
// bufLen_ += dataLen;
databuf_++;


memcpy (databuf_, &m_HC, dataLen);
// bufLen_ += dataLen;
databuf_++;


memcpy (databuf_, &m_CO2, dataLen);


return (databuf_);



}


void Plumedata::show()
{
std::cout << "\nCO: " << m_CO << std::endl;
std::cout << "NOX: " << m_NOX << std::endl;
std::cout << "HC: " << m_HC << std::endl;
std::cout << "CO2: " << m_CO2 << std::endl;


}


//////////////////////////////////////
// Load file from txt and store in DB

void Plumedata::load_database(MyDb& dataDB, std::string &dataFile)
{
// Load a text file into the plume.db
std::ifstream inFile (dataFile.c_str(), std::ios::in);
std::string substring;
if (!inFile)
{
std::cerr << "Could not open file '" << dataFile << "' Giving
up." << std::endl;
throw std::exception();
}
Plumedata my_Plume;
while (!inFile.eof())
{
std::string stringBuf;
std::getline (inFile, stringBuf);
memset (&my_Plume, 0, sizeof(Plumedata));


// sscanf (stringBuf.c_str(),
"%20[^#]#%20[^#]#%20[^#]#%20[^\n]",
// my_Plume.m_CO, my_Plume.m_CO2, my_Plume.m_HC,
my_Plume.m_NOX);
int nextpound;


if (!stringBuf.empty())
{
nextpound = GetNextPound(stringBuf, substring);
my_Plume.setCO(atof(substring.c_str()));


nextpound = GetNextPound(stringBuf, substring);
my_Plume.setCO2 (atof(substring.c_str()));


nextpound = GetNextPound(stringBuf, substring);
my_Plume.setHC(atof(substring.c_str()));


nextpound = GetNextPound(stringBuf, substring);
my_Plume.setNOX(atof(substring.c_str()));


// Dbt key (&my_Plume.m_CO, sizeof(float)+1);
// Dbt data (&my_Plume, sizeof(Plumedata));
float buff = (float) my_Plume.getCO();
int size = sizeof(float);
Dbt key (&buff, size);


float *buff2 = new float[500];
buff2 = (float *)my_Plume.getBuffer();
size = my_Plume.getBufferSize();
Dbt data (buff2, size);


dataDB.getDb().put(NULL, &key, &data, 0); // stream error
}
inFile.close();
}



}


int Plumedata::GetNextPound(std::string &theString, std::string
&substring)
{
int pos = theString.find("#");
substring.assign (theString, 0, pos);
theString.assign (theString, pos+1, theString.size());
return (pos);


}


int main (int argc, char *argv[])
{
std::string basename("./");
std::string databasehome("./");
float reading = 1.1;
std::string txtfile = "./try.txt";
// float NOX;

std::string pDbName("./Plumedb.db");
Plumedata *pdata;
Dbt data, key;


key.set_data(&reading);
key.set_ulen(sizeof(float));
key.set_flags (DB_DBT_USERMEM);
MyDb plumeDB (databasehome, pDbName, false);
pdata = new Plumedata();
try
{


// Put record in and then try loading it
pdata->load_database(plumeDB, txtfile);


db.get(NULL, &key, &data, 0);


pdata = (Plumedata *) data.get_data();


// std::cout << pdata << std::endl;


// Get Data from memory
pdata->show();
} catch (DbException& e) {
std::cerr << "Error reading database. " << std::endl;
std::cerr << e.what() << std::endl;
return (e.get_errno());
} catch (std::exception &e) {
std::cerr << "Error reading database. " << std::endl;
std::cerr << e.what() << std::endl;
return (-1);
}
return 0;

}

=====================
The error was DBEnv::stream_error
:Invalid Argument
Thanks
Jack
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
Difference between Userland programming and Kernel Programming.. meetsagittarian Programming 1 02-20-2006 12:26 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration