mirror of https://github.com/axmolengine/axmol.git
Fix indentation for readability
This commit is contained in:
parent
f07b6a6804
commit
0825b1cc96
|
@ -46,48 +46,48 @@ static std::string className = "org/cocos2dx/lib/Cocos2dxLocalStorage";
|
||||||
|
|
||||||
static void splitFilename (std::string& str)
|
static void splitFilename (std::string& str)
|
||||||
{
|
{
|
||||||
size_t found = 0;
|
size_t found = 0;
|
||||||
found=str.find_last_of("/\\");
|
found = str.find_last_of("/\\");
|
||||||
if (found != std::string::npos)
|
if (found != std::string::npos)
|
||||||
{
|
{
|
||||||
str = str.substr(found+1);
|
str = str.substr(found + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void localStorageInit( const std::string& fullpath)
|
void localStorageInit( const std::string& fullpath)
|
||||||
{
|
{
|
||||||
if (fullpath.empty())
|
if (fullpath.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if( ! _initialized )
|
if (!_initialized)
|
||||||
{
|
{
|
||||||
std::string strDBFilename = fullpath;
|
std::string strDBFilename = fullpath;
|
||||||
splitFilename(strDBFilename);
|
splitFilename(strDBFilename);
|
||||||
if (JniHelper::callStaticBooleanMethod(className, "init", strDBFilename, "data")) {
|
if (JniHelper::callStaticBooleanMethod(className, "init", strDBFilename, "data")) {
|
||||||
_initialized = 1;
|
_initialized = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void localStorageFree()
|
void localStorageFree()
|
||||||
{
|
{
|
||||||
if( _initialized ) {
|
if (_initialized) {
|
||||||
JniHelper::callStaticVoidMethod(className, "destory");
|
JniHelper::callStaticVoidMethod(className, "destory");
|
||||||
_initialized = 0;
|
_initialized = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** sets an item in the LS */
|
/** sets an item in the LS */
|
||||||
void localStorageSetItem( const std::string& key, const std::string& value)
|
void localStorageSetItem( const std::string& key, const std::string& value)
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
JniHelper::callStaticVoidMethod(className, "setItem", key, value);
|
JniHelper::callStaticVoidMethod(className, "setItem", key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** gets an item from the LS */
|
/** gets an item from the LS */
|
||||||
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
JniMethodInfo t;
|
JniMethodInfo t;
|
||||||
|
|
||||||
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;"))
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;"))
|
||||||
|
@ -109,9 +109,8 @@ bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||||
/** removes an item from the LS */
|
/** removes an item from the LS */
|
||||||
void localStorageRemoveItem( const std::string& key )
|
void localStorageRemoveItem( const std::string& key )
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
JniHelper::callStaticVoidMethod(className, "removeItem", key);
|
JniHelper::callStaticVoidMethod(className, "removeItem", key);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** removes all items from the LS */
|
/** removes all items from the LS */
|
||||||
|
|
|
@ -47,104 +47,104 @@ static sqlite3_stmt *_stmt_clear;
|
||||||
|
|
||||||
static void localStorageCreateTable()
|
static void localStorageCreateTable()
|
||||||
{
|
{
|
||||||
const char *sql_createtable = "CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY,value TEXT);";
|
const char *sql_createtable = "CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY,value TEXT);";
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
int ok=sqlite3_prepare_v2(_db, sql_createtable, -1, &stmt, nullptr);
|
int ok = sqlite3_prepare_v2(_db, sql_createtable, -1, &stmt, nullptr);
|
||||||
ok |= sqlite3_step(stmt);
|
ok |= sqlite3_step(stmt);
|
||||||
ok |= sqlite3_finalize(stmt);
|
ok |= sqlite3_finalize(stmt);
|
||||||
|
|
||||||
if( ok != SQLITE_OK && ok != SQLITE_DONE)
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
||||||
printf("Error in CREATE TABLE\n");
|
printf("Error in CREATE TABLE\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void localStorageInit( const std::string& fullpath/* = "" */)
|
void localStorageInit( const std::string& fullpath/* = "" */)
|
||||||
{
|
{
|
||||||
if( ! _initialized ) {
|
if (!_initialized) {
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (fullpath.empty())
|
if (fullpath.empty())
|
||||||
ret = sqlite3_open(":memory:",&_db);
|
ret = sqlite3_open(":memory:", &_db);
|
||||||
else
|
else
|
||||||
ret = sqlite3_open(fullpath.c_str(), &_db);
|
ret = sqlite3_open(fullpath.c_str(), &_db);
|
||||||
|
|
||||||
localStorageCreateTable();
|
localStorageCreateTable();
|
||||||
|
|
||||||
// SELECT
|
// SELECT
|
||||||
const char *sql_select = "SELECT value FROM data WHERE key=?;";
|
const char *sql_select = "SELECT value FROM data WHERE key=?;";
|
||||||
ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, nullptr);
|
ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, nullptr);
|
||||||
|
|
||||||
// REPLACE
|
// REPLACE
|
||||||
const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
|
const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
|
||||||
ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, nullptr);
|
ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, nullptr);
|
||||||
|
|
||||||
// DELETE
|
// DELETE
|
||||||
const char *sql_remove = "DELETE FROM data WHERE key=?;";
|
const char *sql_remove = "DELETE FROM data WHERE key=?;";
|
||||||
ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);
|
ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
const char *sql_clear = "DELETE FROM data;";
|
const char *sql_clear = "DELETE FROM data;";
|
||||||
ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr);
|
ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr);
|
||||||
|
|
||||||
if( ret != SQLITE_OK ) {
|
if (ret != SQLITE_OK) {
|
||||||
printf("Error initializing DB\n");
|
printf("Error initializing DB\n");
|
||||||
// report error
|
// report error
|
||||||
}
|
}
|
||||||
|
|
||||||
_initialized = 1;
|
_initialized = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void localStorageFree()
|
void localStorageFree()
|
||||||
{
|
{
|
||||||
if( _initialized ) {
|
if (_initialized) {
|
||||||
sqlite3_finalize(_stmt_select);
|
sqlite3_finalize(_stmt_select);
|
||||||
sqlite3_finalize(_stmt_remove);
|
sqlite3_finalize(_stmt_remove);
|
||||||
sqlite3_finalize(_stmt_update);
|
sqlite3_finalize(_stmt_update);
|
||||||
|
|
||||||
sqlite3_close(_db);
|
sqlite3_close(_db);
|
||||||
|
|
||||||
_initialized = 0;
|
_initialized = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** sets an item in the LS */
|
/** sets an item in the LS */
|
||||||
void localStorageSetItem( const std::string& key, const std::string& value)
|
void localStorageSetItem( const std::string& key, const std::string& value)
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
|
|
||||||
int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -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_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);
|
||||||
|
|
||||||
ok |= sqlite3_step(_stmt_update);
|
ok |= sqlite3_step(_stmt_update);
|
||||||
|
|
||||||
ok |= sqlite3_reset(_stmt_update);
|
ok |= sqlite3_reset(_stmt_update);
|
||||||
|
|
||||||
if( ok != SQLITE_OK && ok != SQLITE_DONE)
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
||||||
printf("Error in localStorage.setItem()\n");
|
printf("Error in localStorage.setItem()\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** gets an item from the LS */
|
/** gets an item from the LS */
|
||||||
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
|
|
||||||
int ok = sqlite3_reset(_stmt_select);
|
int ok = sqlite3_reset(_stmt_select);
|
||||||
|
|
||||||
ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
||||||
ok |= sqlite3_step(_stmt_select);
|
ok |= sqlite3_step(_stmt_select);
|
||||||
const unsigned char *text = sqlite3_column_text(_stmt_select, 0);
|
const unsigned char *text = sqlite3_column_text(_stmt_select, 0);
|
||||||
|
|
||||||
if ( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW )
|
if (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
|
||||||
{
|
{
|
||||||
printf("Error in localStorage.getItem()\n");
|
printf("Error in localStorage.getItem()\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (!text)
|
else if (!text)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
outItem->assign((const char*)text);
|
outItem->assign((const char*)text);
|
||||||
return true;
|
return true;
|
||||||
|
@ -154,16 +154,16 @@ bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||||
/** removes an item from the LS */
|
/** removes an item from the LS */
|
||||||
void localStorageRemoveItem( const std::string& key )
|
void localStorageRemoveItem( const std::string& key )
|
||||||
{
|
{
|
||||||
assert( _initialized );
|
assert( _initialized );
|
||||||
|
|
||||||
int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
||||||
|
|
||||||
ok |= sqlite3_step(_stmt_remove);
|
ok |= sqlite3_step(_stmt_remove);
|
||||||
|
|
||||||
ok |= sqlite3_reset(_stmt_remove);
|
ok |= sqlite3_reset(_stmt_remove);
|
||||||
|
|
||||||
if( ok != SQLITE_OK && ok != SQLITE_DONE)
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
||||||
printf("Error in localStorage.removeItem()\n");
|
printf("Error in localStorage.removeItem()\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** removes all items from the LS */
|
/** removes all items from the LS */
|
||||||
|
|
Loading…
Reference in New Issue