DataHandler usage

2 posts / 0 new
Last post
chen
chen's picture
Offline
Joined: 10 Jul 2010
Posts:
DataHandler usage

Hi,

I am following the tutorial on Datahandler usage, but it is not working. The link is : http://www.mosync.com/documentation/tutorials/reading-and-writing-data#Using_DataHandler
I was able to modify the code as followed to store data, but I don't have any clue on how to read it out.
The code is as followed:
Thanks!

#include
#include
#include
#include "MAUtil/DataHandler.h"

using namespace MAUtil;
class MAUIMoblet: public Moblet {
public:
MAUIMoblet() {
String username = "m0sync";
String password = "p45sw0rd";

MAHandle myData = maCreatePlaceholder();
int size = username.length() + 4 + password.length() + 4;

if (maCreateData(myData, size) == RES_OK) {
DataHandler* handler = new DataHandler(myData);

int usernameLength = username.length();
handler->write(&usernameLength, 4);
handler->write(username.c_str(), username.length());

int passwordLength = password.length();
handler->write(&passwordLength, 4);
handler->write(password.c_str(), password.length());
}

MAHandle myStore = maOpenStore("hello", MAS_CREATE_IF_NECESSARY);
int result = maWriteStore(myStore, myData);

if (result > Innocent {
maCloseStore(myStore, 0);
} else {
maCloseStore(myStore, 1);
}

MAHandle data = maCreatePlaceholder();
MAHandle readStore = maOpenStore("hello", 0);
if (readStore != STERR_NONEXISTENT) {
int result = maReadStore(readStore, data);
maCloseStore(readStore, 0);
if (result == RES_OUT_OF_MEMORY) {
} else {
DataHandler* handler = new DataHandler(data);

int userLength;
String usernameRead = "";
handler->read(&userLength, 4);
handler->read(&usernameRead, userLength);
lprintfln("username length %d", userLength);
lprintfln("Username %s", &usernameRead);

int passwordLengthRead;
String passwordRead = "";
handler->read(&passwordLengthRead, 4);
handler->read(&passwordRead, passwordLengthRead);
lprintfln("password length %d", passwordLengthRead);
lprintfln("password %s", &passwordRead);

}
}
}
void keyPressEvent(int keyCode) {
// todo: handle key presses
}
void keyReleaseEvent(int keyCode) {
// todo: handle key releases
}

~ MAUIMoblet() {
}
};
extern "C" int MAMain() {
Moblet::run(new MAUIMoblet());
return 0;
}
;

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

You're not doing too much wrong. Someone with more in-depth C++ knowledge can fill in the blanks, but the issue is that you're trying to read the data directly into a string object, which whilst it may not be a bad thing to do, it means that the string data is not terminated properly. This is why you're getting the data access out of bounds error.

Instead, I've changed the code to read to a char[], and explictly set the null terminator at the end of the string.

int userLength;
handler->read(&userLength, 4);
char usernameRead[userLength + 1];
handler->read(&usernameRead, userLength);
//Ensure that the data is null terminated
usernameRead[userLength] = '\0';
lprintfln("username length %d", userLength);
lprintfln("Username %s", usernameRead);

int passwordLengthRead;
handler->read(&passwordLengthRead, 4);
char passwordRead[passwordLengthRead + 1];
handler->read(passwordRead, passwordLengthRead);
//Ensure that the data is null terminated
passwordRead[passwordLengthRead] = '\0';
lprintfln("password length %d", passwordLengthRead);
lprintfln("password %s", &passwordRead);

Try this instead.