Serializing wxStrings

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
numerone
Knows some wx things
Knows some wx things
Posts: 28
Joined: Thu Aug 16, 2007 11:04 pm

Serializing wxStrings

Post by numerone »

Hello,
i'm making a card game and i want to implement a top 10 results functionality.

For making it i have to save the most big points to a file.

Because of c++ and wxwidgets does not have serializing functionality, i'm implementing it with wxFileInputStream ad wxFIleOutput stream.

The class that i want to serialize is a very basic class that contains the name and the points of winner and losers.

The class accept thwo name and two points and save it properly.

I have made a wrapper for the vector of 10 elemnts of this class and then i have made a functionality for inserting a result (that order properly the vector and delete the 11st element) and method for read and write it to the streams.

When i try to write the vector to stream all goes good, but when i try to read it I obtain an error that wxString is not ok.

So i tried to write separately wxStrings and number: the write goes ok, and even the read, but when i exit from the leggi (read) method i obtain an EXC_BAD_ACCESS error, i think it's an error of wxstring because of the debugger stops it on wxstring source file.

I'm programming on mac os x leopard using xcode

I post my classes: thanks for help.

statistica.h

Code: Select all

#ifndef _STATISTICA_H_
#define _STATISTICA_H_

#include <wx/string.h>

class statistica {
	private:
		wxString nomeVincitore, nomePerdente;
		size_t punteggioVincitore, punteggioPerdente;
	public:
		statistica() {;}
		statistica(wxString nome1, wxString nome2, size_t punteggio1, size_t punteggio2);
		wxString getNomeVincitore() {return nomeVincitore;}
		wxString getNomePerdente() {return nomePerdente;}
		size_t getPunteggioVincitore() {return punteggioVincitore;}
		size_t getPunteggioPerdente() {return punteggioPerdente;}
};
#endif
statistica.cpp

Code: Select all

#include "statistica.h"

statistica::statistica(wxString nome1, wxString nome2, size_t punteggio1, size_t punteggio2) {
	if (punteggio1>punteggio2) {
		nomeVincitore=nome1;
		nomePerdente=nome2;
		punteggioVincitore=punteggio1;
		punteggioPerdente=punteggio2;
	} else {
		nomeVincitore=nome2;
		nomePerdente=nome1;
		punteggioVincitore=punteggio2;
		punteggioPerdente=punteggio1;
	}
}
statistiche.h

Code: Select all

#ifndef _STATISTICHE_H_
#define _STATISTICHE_H_

#include "statistica.h"
#include "stringHelper.h"

#include <stdexcept>
#include<vector>
#include <wx/wfstream.h>
#include <wx/filename.h>
#include <wx/wx.h>

class statistiche {
	private:
		std::vector<statistica> v;
		wxString path;
	public:
		statistiche();
		void add(wxString nome1, size_t punteggio1, wxString	nome2, size_t punteggio2);
		void get(size_t quale, wxString& nome1, size_t punteggio1, wxString &nome2, size_t &punteggio2) throw (range_error&);
	size_t getNumeroStatistiche() {return v.size();}
		void read() throw (invalid_argument&);
		void write() throw (invalid_argument&);
		virtual ~statistiche();
};

#endif
statistiche.cpp

Code: Select all

#include "statistiche.h"

statistiche::statistiche() {
	path=wxGetCwd()+wxFileName::GetPathSeparator()+wxT("statistiche.bin");
	try {
		read();
	} catch (invalid_argument e) {
	;
	}
}

void statistiche::add(wxString nome1, size_t punteggio1, wxString	nome2, size_t punteggio2) {
	vector<statistica>::iterator i;
	statistica s=statistica(nome1, nome2, punteggio1, punteggio2);
	for (i=v.begin(); i!=v.end() && i->getPunteggioVincitore()>s.getPunteggioVincitore(); i++);
	v.insert(i, s);
	if (v.size()>10)
		v.pop_back();
}

void statistiche::get(size_t quale, wxString& nome1, size_t punteggio1, wxString &nome2, size_t &punteggio2) throw (range_error&) {
	if (quale>v.size())
		throw range_error("Chiamata a statistiche::get con quale="+stringHelper::IntToStr(quale));
	nome1=v[quale].getNomeVincitore();
	nome2=v[quale].getNomePerdente();
	punteggio1=v[quale].getPunteggioVincitore();
	punteggio2=v[quale].getPunteggioPerdente();
}

void statistiche::read() throw (invalid_argument&) {
	wxFileInputStream is(path);
	if (!is.IsOk() || is.GetSize()==0)
		throw invalid_argument("Stream non pronto");
	wxString s;
	size_t p;
	while (	is.Read(&s, sizeof(s)).GetLastError()==wxSTREAM_NO_ERROR) {
//		statistica s;
//		);
//		v.push_back(s);
		wxMessageBox(s);
	}
}


void statistiche::write() throw (invalid_argument&) {
	wxFileOutputStream os(path);
	if (!os.IsOk())
		throw invalid_argument("Stream non pronto");
	wxString s;
	size_t p;
	for (size_t i=0; i<v.size(); i++) {
		s=v[i].getNomeVincitore();
		p=v[i].getPunteggioVincitore();
		os.Write(&s, sizeof(s));
		//os.Write(&p, sizeof(p));
	}
	os.Close();
	wxFileInputStream is(path);
	is.Read(&s, sizeof(s));
	wxMessageBox(s);
}


statistiche::~statistiche() {
	write();
	v.clear();
}
protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX
Contact:

Post by protocol »

wxWidgets does provide plenty of other options to store strings.

wx data storage IO classes
Check out: http://docs.wxwidgets.org/stable/wx_classesbycat.html (Stream Classes)

Also, I recommend you use: http://docs.wxwidgets.org/stable/wx_wxa ... ml#wxarray instead of the STL vector

regards.
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

wxSerialize can help you there too if you want.

http://www.xs4all.nl/~jorgb/wb/wxwidget ... index.html

With regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

Post by MoonKid »

Jorg wrote:wxSerialize can help you there too if you want.
http://www.xs4all.nl/~jorgb/wb/wxwidget ... index.html
I am using it for my projects to with very complex data structures. It work fine and is easy to use!

;)
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Also, for very simple stuff like 10 strings, wxConfig might just as well be enough
"Keyboard not detected. Press F1 to continue"
-- Windows
Post Reply