void Int32Gadget :: MessageReceived( BMessage * paramMessage) { const char * entry = Text(); uint32 len = strlen(entry);//known not utf8 int32 maxdigits = DIGITSnI32 - 2; if ((entry[0] == '-') || (entry[0] == '+')) { maxdigits++; } bool notFailed = true; if (len > maxdigits) { warning(myPrefs->NumStrTooLong); notFailed = false; SetText(mbsValueString.String()); } if (notFailed) { for ( int32 i = 1; i < maxdigits; i++ ) { if (entry[i] == 0xa) { i = maxdigits; } else { if ((entry[i] == '+') || (entry[i] == '-')) { notFailed = false; } } } } if (notFailed) { int32 value; if(s2int32(entry, &value) != B_OK) { notFailed = false; } } if (notFailed) { mbsValueString = entry; BMessage * msg = new BMessage(paramMessage); mpMessageTarget->PostMessage(msg); return; } BTextView::MessageReceived(paramMessage); }//end
void cmdev_add_app(const struct lh_kv_elem *params, const struct lh_kv_elem *cookies, struct lh_response* resp) { //check db PGconn *cmatchdb = cm::get_context()->cmatchdb; if (PQstatus(cmatchdb) != CONNECTION_OK) { return cm_send_error(resp, CMERR_DB); } //check auth cm::Session session; int err = cm::find_session(cookies, session); if (err) { return cm_send_error(resp, CMERR_AUTH); } //parse param const char *appname = lh_kv_string(params, "appname", &err); if (err) { return cm_send_error(resp, CMERR_PARAM); } //secret key uuid_t uuid; uuid_generate(uuid); Base64 secret64((const char*)uuid, sizeof(uuid)); //count limit std::stringstream ss_sql; ss_sql << "SELECT COUNT(0) FROM app WHERE developer_id=" << session.userid << ";"; PGresult *res = PQexec(cmatchdb, ss_sql.str().c_str()); Autofree af_res_cl(res, (Freefunc)PQclear); if (PQresultStatus(res) != PGRES_TUPLES_OK) { return cm_send_error(resp, CMERR_DB); } const char* sappnum = PQgetvalue(res, 0, 0); if (sappnum == NULL) { return cm_send_error(resp, CMERR_DB); } uint32_t appnum = s2int32(sappnum, &err); if (err){ return cm_send_error(resp, CMERR_DB); } if (appnum >= APP_NUM_LIMIT) { return cm_send_error(resp, CMERR_OUT_OF_RANGE); } //insert into db const char *vars[3] = { appname, secret64.str.c_str(), session.userid.c_str(), }; res = PQexecParams(cmatchdb, "INSERT INTO app (name, secret, developer_id) VALUES($1, $2, $3) RETURNING id;", 3, NULL, vars, NULL, NULL, 0); Autofree af_res_insert(res, (Freefunc)PQclear); if (PQresultStatus(res) != PGRES_TUPLES_OK){ return cm_send_error(resp, CMERR_DB); } //get app id const char* appid = PQgetvalue(res, 0, 0); if (appid == NULL) { return cm_send_error(resp, CMERR_DB); } //send to client lh_appendf_body(resp, "{\"error\":0, \"appid\":%s}", appid); }
void cmdev_add_game(const struct lh_kv_elem *params, const struct lh_kv_elem *cookies, struct lh_response* resp) { //check auth cm::Session session; int err = cm::find_session(cookies, session); if (err) { return cm_send_error(resp, CMERR_AUTH); } //parse param const char *appid = lh_kv_string(params, "appid", &err); const char *gamename = lh_kv_string(params, "gamename", &err); if (err) { return cm_send_error(resp, CMERR_PARAM); } //check db PGconn *cmatchdb = cm::get_context()->cmatchdb; if (PQstatus(cmatchdb) != CONNECTION_OK) { return cm_send_error(resp, CMERR_DB); } //count limit const int VAR_NUMS = 1; const char *vars[VAR_NUMS] = { appid, }; PGresult *res = PQexecParams(cmatchdb, "SELECT COUNT(0) FROM game WHERE app_id=$1;", VAR_NUMS, NULL, vars, NULL, NULL, 0); Autofree af_res_cl(res, (Freefunc)PQclear); if (PQresultStatus(res) != PGRES_TUPLES_OK) { return cm_send_error(resp, CMERR_DB); } const char* s_gamenums = PQgetvalue(res, 0, 0); if (s_gamenums == NULL) { return cm_send_error(resp, CMERR_DB); } uint32_t gamenums = s2int32(s_gamenums, &err); if (err){ return cm_send_error(resp, CMERR_DB); } if (gamenums >= GAME_NUM_LIMIT) { return cm_send_error(resp, CMERR_OUT_OF_RANGE); } //insert into db const int VAR_NUMS2 = 3; const char *vars2[VAR_NUMS2] = { gamename, appid, session.userid.c_str(), }; res = PQexecParams( cmatchdb, "INSERT INTO game (name, app_id, developer_id) " "SELECT $1, $2, $3 WHERE $3=(SELECT developer_id FROM app WHERE id=$2) " "RETURNING id;", VAR_NUMS2, NULL, vars2, NULL, NULL, 0); Autofree af_res_insert(res, (Freefunc)PQclear); if (PQresultStatus(res) != PGRES_TUPLES_OK){ return cm_send_error(resp, CMERR_DB); } //get game id const char* gameid = PQgetvalue(res, 0, 0); if (gameid == NULL) { return cm_send_error(resp, CMERR_DB); } //send to client lh_appendf_body(resp, "{\"error\":0, \"gameid\":%s}", gameid); }