LocalStorage: const char* —> const std::string&.

This commit is contained in:
James Chen 2014-01-20 22:31:56 +08:00
parent 43b2217fcf
commit 8b6763a6ac
3 changed files with 47 additions and 46 deletions

View File

@ -55,16 +55,16 @@ static void localStorageCreateTable()
printf("Error in CREATE TABLE\n");
}
void localStorageInit( const char *fullpath)
void localStorageInit( const std::string& fullpath/* = "" */)
{
if( ! _initialized ) {
int ret = 0;
if (!fullpath)
if (fullpath.empty())
ret = sqlite3_open(":memory:",&_db);
else
ret = sqlite3_open(fullpath, &_db);
ret = sqlite3_open(fullpath.c_str(), &_db);
localStorageCreateTable();
@ -103,12 +103,12 @@ void localStorageFree()
}
/** sets an item in the LS */
void localStorageSetItem( const char *key, const char *value)
void localStorageSetItem( const std::string& key, const std::string& value)
{
assert( _initialized );
int ok = sqlite3_bind_text(_stmt_update, 1, key, -1, SQLITE_TRANSIENT);
ok |= sqlite3_bind_text(_stmt_update, 2, value, -1, SQLITE_TRANSIENT);
int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_step(_stmt_update);
@ -119,13 +119,13 @@ void localStorageSetItem( const char *key, const char *value)
}
/** gets an item from the LS */
const char* localStorageGetItem( const char *key )
std::string localStorageGetItem( const std::string& key )
{
assert( _initialized );
int ok = sqlite3_reset(_stmt_select);
ok |= sqlite3_bind_text(_stmt_select, 1, key, -1, SQLITE_TRANSIENT);
ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_step(_stmt_select);
const unsigned char *ret = sqlite3_column_text(_stmt_select, 0);
@ -137,11 +137,11 @@ const char* localStorageGetItem( const char *key )
}
/** removes an item from the LS */
void localStorageRemoveItem( const char *key )
void localStorageRemoveItem( const std::string& key )
{
assert( _initialized );
int ok = sqlite3_bind_text(_stmt_remove, 1, key, -1, SQLITE_TRANSIENT);
int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_step(_stmt_remove);

View File

@ -30,22 +30,21 @@ THE SOFTWARE.
#ifndef __JSB_LOCALSTORAGE_H
#define __JSB_LOCALSTORAGE_H
#include <stdio.h>
#include <stdlib.h>
#include <string>
/** Initializes the database. If path is null, it will create an in-memory DB */
void localStorageInit( const char *fullpath);
void localStorageInit( const std::string& fullpath = "");
/** Frees the allocated resources */
void localStorageFree();
/** sets an item in the LS */
void localStorageSetItem( const char *key, const char *value);
void localStorageSetItem( const std::string& key, const std::string& value);
/** gets an item from the LS */
const char* localStorageGetItem( const char *key );
std::string localStorageGetItem( const std::string& key );
/** removes an item from the LS */
void localStorageRemoveItem( const char *key );
void localStorageRemoveItem( const std::string& key );
#endif // __JSB_LOCALSTORAGE_H

View File

@ -52,27 +52,28 @@ static void splitFilename (std::string& str)
}
}
void localStorageInit( const char *fullpath)
void localStorageInit( const std::string& fullpath)
{
if (fullpath == NULL || strlen(fullpath) == 0) return;
if (fullpath.emty())
return;
if( ! _initialized ) {
JniMethodInfo t;
if( ! _initialized )
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "init", "(Ljava/lang/String;Ljava/lang/String;)Z")) {
std::string strDBFilename = fullpath;
splitFilename(strDBFilename);
jstring jdbName = t.env->NewStringUTF(strDBFilename.c_str());
jstring jtableName = t.env->NewStringUTF("data");
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, jdbName, jtableName);
t.env->DeleteLocalRef(jdbName);
t.env->DeleteLocalRef(jtableName);
t.env->DeleteLocalRef(t.classID);
if (ret) {
_initialized = 1;
}
}
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "init", "(Ljava/lang/String;Ljava/lang/String;)Z")) {
std::string strDBFilename = fullpath;
splitFilename(strDBFilename);
jstring jdbName = t.env->NewStringUTF(strDBFilename.c_str());
jstring jtableName = t.env->NewStringUTF("data");
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, jdbName, jtableName);
t.env->DeleteLocalRef(jdbName);
t.env->DeleteLocalRef(jtableName);
t.env->DeleteLocalRef(t.classID);
if (ret) {
_initialized = 1;
}
}
}
}
@ -93,15 +94,15 @@ void localStorageFree()
}
/** sets an item in the LS */
void localStorageSetItem( const char *key, const char *value)
void localStorageSetItem( const std::string& key, const std::string& value)
{
assert( _initialized );
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "setItem", "(Ljava/lang/String;Ljava/lang/String;)V")) {
jstring jkey = t.env->NewStringUTF(key);
jstring jvalue = t.env->NewStringUTF(value);
jstring jkey = t.env->NewStringUTF(key.c_str());
jstring jvalue = t.env->NewStringUTF(value.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey, jvalue);
t.env->DeleteLocalRef(jkey);
t.env->DeleteLocalRef(jvalue);
@ -110,30 +111,31 @@ void localStorageSetItem( const char *key, const char *value)
}
/** gets an item from the LS */
const char* localStorageGetItem( const char *key )
std::string localStorageGetItem( const std::string& key )
{
assert( _initialized );
JniMethodInfo t;
String* pStr = NULL;
std::string ret;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) {
jstring jkey = t.env->NewStringUTF(key);
jstring ret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);
pStr = String::create(JniHelper::jstring2string(ret));
t.env->DeleteLocalRef(ret);
jstring jkey = t.env->NewStringUTF(key.c_str());
jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);
ret = JniHelper::jstring2string(jret);
t.env->DeleteLocalRef(jret);
t.env->DeleteLocalRef(jkey);
t.env->DeleteLocalRef(t.classID);
}
return pStr ? pStr->getCString() : NULL;
return ret;
}
/** removes an item from the LS */
void localStorageRemoveItem( const char *key )
void localStorageRemoveItem( const std::string& key )
{
assert( _initialized );
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "removeItem", "(Ljava/lang/String;)V")) {
jstring jkey = t.env->NewStringUTF(key);
jstring jkey = t.env->NewStringUTF(key.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey);
t.env->DeleteLocalRef(jkey);
t.env->DeleteLocalRef(t.classID);