From 9183fa1755395c2cef191c7436f9434a97f8e8d9 Mon Sep 17 00:00:00 2001 From: mniip Date: Sat, 26 Oct 2013 19:38:50 +0400 Subject: [PATCH] apparently Ximon hates const; a lot; added a bunch of const everywhere, had to modify a few functions to be const-compliant --- src/Misc.cpp | 10 ++-- src/Misc.h | 6 +-- src/PowderToySDL.cpp | 2 +- src/bson/BSON.cpp | 2 +- src/cat/LegacyLuaAPI.cpp | 16 ++++--- src/cat/LuaScriptHelper.h | 2 +- src/client/Client.cpp | 26 +++++------ src/client/HTTP.cpp | 78 ++++++++++++++----------------- src/client/HTTP.h | 18 +++---- src/simulation/MenuSection.h | 2 +- src/simulation/elements/Element.h | 6 +-- src/simulation/tools/SimTool.h | 6 +-- 12 files changed, 84 insertions(+), 90 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index 39db54b13..2c6ea8449 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -119,7 +119,7 @@ TPT_NO_INLINE float restrict_flt(float f, float min, float max) //TODO Inline or return f; } -char *mystrdup(char *s) +char *mystrdup(const char *s) { char *x; if (s) @@ -128,7 +128,7 @@ char *mystrdup(char *s) strcpy(x, s); return x; } - return s; + return NULL; } void strlist_add(struct strlist **list, char *str) @@ -221,7 +221,7 @@ void strcaturl(char *dst, char *src) *d = 0; } -void strappend(char *dst, char *src) +void strappend(char *dst, const char *src) { char *d; unsigned char *s; @@ -465,7 +465,7 @@ int register_extension() #elif defined(LIN) char *currentfilename = exe_name(); FILE *f; - char *mimedata = + const char *mimedata = "\n" " \n" " \n" @@ -480,7 +480,7 @@ int register_extension() fwrite(mimedata, 1, strlen(mimedata), f); fclose(f); - char *desktopfiledata_tmp = + const char *desktopfiledata_tmp = "[Desktop Entry]\n" "Type=Application\n" "Name=Powder Toy\n" diff --git a/src/Misc.h b/src/Misc.h index ef9ef8b2d..1c6a9039a 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -40,7 +40,7 @@ unsigned clamp_flt(float f, float min, float max); float restrict_flt(float f, float min, float max); -char *mystrdup(char *s); +char *mystrdup(const char *s); struct strlist { @@ -64,11 +64,11 @@ void save_string(FILE *f, char *str); int load_string(FILE *f, char *str, int max); -void strcaturl(char *dst, char *src); +void strcaturl(char *dst, const char *src); std::string URLEscape(std::string source); -void strappend(char *dst, char *src); +void strappend(char *dst, const char *src); void *file_load(char *fn, int *size); diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 7f1a8835c..fa9033b6a 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -687,7 +687,7 @@ bool SaveWindowPosition() #endif -void BlueScreen(char * detailMessage){ +void BlueScreen(const char * detailMessage){ ui::Engine * engine = &ui::Engine::Ref(); engine->g->fillrect(0, 0, engine->GetWidth(), engine->GetHeight(), 17, 114, 169, 210); diff --git a/src/bson/BSON.cpp b/src/bson/BSON.cpp index 634290cbc..d26a12671 100644 --- a/src/bson/BSON.cpp +++ b/src/bson/BSON.cpp @@ -48,7 +48,7 @@ static int ( *oid_inc_func )( void ) = NULL; ------------------------------ */ bson *bson_empty( bson *obj ) { - static char *data = "\005\0\0\0\0"; + static char data[] = "\005\0\0\0\0"; bson_init_data( obj, data ); obj->finished = 1; obj->err = 0; diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 58891ef41..70ac24663 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -650,7 +650,7 @@ int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::s } -int luacon_eval(char *command){ +int luacon_eval(const char *command){ ui::Engine::Ref().LastTick(clock()); return luaL_dostring (luacon_ci->l, command); } @@ -1927,7 +1927,7 @@ int luatpt_getscript(lua_State* l) run_script = luaL_optint(l, 3, 0); if(!fileauthor.length() || !fileid.length()) { - lastError = "Script Author or ID not given"; + lastError = mystrdup("Script Author or ID not given"); goto fin; } if(!ConfirmPrompt::Blocking("Do you want to install script?", fileid, "Install")) @@ -1941,12 +1941,12 @@ int luatpt_getscript(lua_State* l) if(len <= 0 || !filedata) { - lastError = "Server did not return data."; + strcpy(lastError, "Server did not return data."); goto fin; } if(ret != 200) { - lastError = http_ret_text(ret); + lastError = mystrdup(http_ret_text(ret)); goto fin; } @@ -1976,7 +1976,7 @@ int luatpt_getscript(lua_State* l) if(!outputfile) { - lastError = "Unable to write to file"; + lastError = mystrdup("Unable to write to file"); goto fin; } @@ -1998,7 +1998,11 @@ fin: if(luacommand) delete[] luacommand; luacommand = NULL; - if(lastError) return luaL_error(l, lastError); + if(lastError) + { + return luaL_error(l, lastError); + free(lastError); + } return 0; } diff --git a/src/cat/LuaScriptHelper.h b/src/cat/LuaScriptHelper.h index d1679a651..0c4af2895 100644 --- a/src/cat/LuaScriptHelper.h +++ b/src/cat/LuaScriptHelper.h @@ -24,7 +24,7 @@ void luacon_hook(lua_State *L, lua_Debug *ar); int luacon_step(int mx, int my, std::string , std::string selectr, std::string selectedalt, int bsx, int bsy); int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel); int luacon_keyevent(int key, int modifier, int event); -int luacon_eval(char *command); +int luacon_eval(const char *command); char *luacon_geterror(); void luacon_close(); int luacon_partsread(lua_State* l); diff --git a/src/client/Client.cpp b/src/client/Client.cpp index c446c14e3..9150caaba 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -333,7 +333,7 @@ bool Client::DoInstallation() char *currentfilename = exe_name(); FILE *f; - char *mimedata = + const char *mimedata = "\n" " \n" " \n" @@ -348,7 +348,7 @@ bool Client::DoInstallation() fwrite(mimedata, 1, strlen(mimedata), f); fclose(f); - char *protocolfiledata_tmp = + const char *protocolfiledata_tmp = "[Desktop Entry]\n" "Type=Application\n" "Name=Powder Toy\n" @@ -367,7 +367,7 @@ bool Client::DoInstallation() fclose(f); system("xdg-desktop-menu install powdertoy-tpt-ptsave.desktop"); - char *desktopfiledata_tmp = + const char *desktopfiledata_tmp = "[Desktop Entry]\n" "Type=Application\n" "Name=Powder Toy\n" @@ -893,8 +893,8 @@ RequestStatus Client::UploadSave(SaveInfo & save) char *session = new char[authUser.SessionID.length() + 1]; std::strcpy (session, authUser.SessionID.c_str()); - char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL }; - char * postDatas[] = { saveName, saveDescription, gameData, (char *)(save.GetPublished()?"Public":"Private") }; + const char *const postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL }; + const char *const postDatas[] = { saveName, saveDescription, gameData, (char *)(save.GetPublished()?"Public":"Private") }; int postLengths[] = { save.GetName().length(), save.GetDescription().length(), gameDataLength, save.GetPublished()?6:7 }; //std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl; data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, userid, NULL, session, &dataStatus, &dataLength); @@ -1120,8 +1120,8 @@ RequestStatus Client::ExecVote(int saveID, int direction) char *session = new char[authUser.SessionID.length() + 1]; std::strcpy (session, authUser.SessionID.c_str()); - char * postNames[] = { "ID", "Action", NULL }; - char * postDatas[] = { id, directionText }; + const char *const postNames[] = { "ID", "Action", NULL }; + const char *const postDatas[] = { id, directionText }; int postLengths[] = { saveIDText.length(), strlen(directionText) }; //std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl; data = http_multipart_post("http://" SERVER "/Vote.api", postNames, postDatas, postLengths, userid, NULL, session, &dataStatus, &dataLength); @@ -1303,8 +1303,8 @@ LoginStatus Client::Login(std::string username, std::string password, User & use char * data; int dataStatus, dataLength; - char * postNames[] = { "Username", "Hash", NULL }; - char * postDatas[] = { (char*)username.c_str(), totalHash }; + const char *const postNames[] = { "Username", "Hash", NULL }; + const char *const postDatas[] = { (char*)username.c_str(), totalHash }; int postLengths[] = { username.length(), 32 }; data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength); if(dataStatus == 200 && data) @@ -1432,8 +1432,8 @@ RequestStatus Client::AddComment(int saveID, std::string comment) std::stringstream userIDStream; userIDStream << authUser.ID; - char * postNames[] = { "Comment", NULL }; - char * postDatas[] = { (char*)(comment.c_str()) }; + const char *const postNames[] = { "Comment", NULL }; + const char *const postDatas[] = { (char*)(comment.c_str()) }; int postLengths[] = { comment.length() }; data = http_multipart_post((char *)urlStream.str().c_str(), postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength); } @@ -1548,8 +1548,8 @@ RequestStatus Client::ReportSave(int saveID, std::string message) std::stringstream userIDStream; userIDStream << authUser.ID; - char * postNames[] = { "Reason", NULL }; - char * postDatas[] = { (char*)(message.c_str()) }; + const char *const postNames[] = { "Reason", NULL }; + const char *const postDatas[] = { (char*)(message.c_str()) }; int postLengths[] = { message.length() }; data = http_multipart_post((char *)urlStream.str().c_str(), postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength); } diff --git a/src/client/HTTP.cpp b/src/client/HTTP.cpp index c3110f528..531e9536c 100644 --- a/src/client/HTTP.cpp +++ b/src/client/HTTP.cpp @@ -49,6 +49,7 @@ #endif #include "Config.h" +#include "Misc.h" #include "HTTP.h" #include "MD5.h" @@ -87,34 +88,25 @@ static char * eatwhitespace(char * s) return s; } -static char *mystrdup(char *s) +static int splituri(const char *uri, char **host, char **path) { - char *x; - if (s) - { - x = (char *)malloc(strlen(s)+1); - strcpy(x, s); - return x; - } - return s; -} - -static int splituri(char *uri, char **host, char **path) -{ - char *p=uri,*q,*x,*y; - if (!strncmp(p, "http://", 7)) - p += 7; - q = strchr(p, '/'); + const char *q; + char *x,*y; + if (!strncmp(uri, "http://", 7)) + uri += 7; + q = strchr(uri, '/'); if (!q) - q = p + strlen(p); - x = (char *)malloc(q-p+1); + q = uri + strlen(uri); + x = (char *)malloc(q-uri+1); if (*q) y = mystrdup(q); else + { y = mystrdup("/"); - strncpy(x, p, q-p); - x[q-p] = 0; - if (q==p || x[q-p-1]==':') + } + strncpy(x, uri, q-uri); + x[q-uri] = 0; + if (q==uri || x[q-uri-1]==':') { free(x); free(y); @@ -244,7 +236,7 @@ struct http_ctx int fd; char *fdhost; }; -void *http_async_req_start(void *ctx, char *uri, char *data, int dlen, int keep) +void *http_async_req_start(void *ctx, const char *uri, const char *data, int dlen, int keep) { struct http_ctx *cx = (http_ctx *)ctx; if (!ctx) @@ -298,7 +290,7 @@ void *http_async_req_start(void *ctx, char *uri, char *data, int dlen, int keep) { if (!dlen) dlen = strlen(data); - cx->txd = (char*)malloc(dlen); + cx->txd = (char *)malloc(dlen); memcpy(cx->txd, data, dlen); cx->txdl = dlen; } @@ -319,7 +311,7 @@ void *http_async_req_start(void *ctx, char *uri, char *data, int dlen, int keep) return ctx; } -void http_async_add_header(void *ctx, char *name, char *data) +void http_async_add_header(void *ctx, const char *name, const char *data) { struct http_ctx *cx = (http_ctx *)ctx; cx->thdr = (char *)realloc(cx->thdr, cx->thlen + strlen(name) + strlen(data) + 5); @@ -694,7 +686,7 @@ void http_async_req_close(void *ctx) free(ctx); } -char *http_simple_get(char *uri, int *ret, int *len) +char *http_simple_get(const char *uri, int *ret, int *len) { void *ctx = http_async_req_start(NULL, uri, NULL, 0, 0); if (!ctx) @@ -707,7 +699,7 @@ char *http_simple_get(char *uri, int *ret, int *len) } return http_async_req_stop(ctx, ret, len); } -void http_auth_headers(void *ctx, char *user, char *pass, char *session_id) +void http_auth_headers(void *ctx, const char *user, const char *pass, const char *session_id) { char *tmp; int i; @@ -747,7 +739,7 @@ void http_auth_headers(void *ctx, char *user, char *pass, char *session_id) } } } -char *http_auth_get(char *uri, char *user, char *pass, char *session_id, int *ret, int *len) +char *http_auth_get(const char *uri, const char *user, const char *pass, const char *session_id, int *ret, int *len) { void *ctx = http_async_req_start(NULL, uri, NULL, 0, 0); @@ -763,7 +755,7 @@ char *http_auth_get(char *uri, char *user, char *pass, char *session_id, int *re return http_async_req_stop(ctx, ret, len); } -char *http_simple_post(char *uri, char *data, int dlen, int *ret, int *len) +char *http_simple_post(const char *uri, const char *data, int dlen, int *ret, int *len) { void *ctx = http_async_req_start(NULL, uri, data, dlen, 0); if (!ctx) @@ -777,7 +769,7 @@ char *http_simple_post(char *uri, char *data, int dlen, int *ret, int *len) return http_async_req_stop(ctx, ret, len); } -char *http_ret_text(int ret) +const char *http_ret_text(int ret) { switch (ret) { @@ -914,10 +906,10 @@ char *http_ret_text(int ret) return "Unknown Status Code"; } } -char *http_multipart_post(char *uri, char **names, char **parts, int *plens, char *user, char *pass, char *session_id, int *ret, int *len) +char *http_multipart_post(const char *uri, const char *const *names, const char *const *parts, int *plens, const char *user, const char *pass, const char *session_id, int *ret, int *len) { void *ctx; - char *data = NULL, *tmp, *p; + char *data = NULL, *tmp; int dlen = 0, i, j; unsigned char hash[16]; unsigned char boundary[32], ch; @@ -987,12 +979,11 @@ retry: if (strchr(names[i], ':')) { tmp = mystrdup(names[i]); - p = strchr(tmp, ':'); + char *p = strchr(tmp, ':'); *p = 0; dlen += sprintf(data+dlen, "content-disposition: form-data; name=\"%s\"; ", tmp); free(tmp); - p = strchr(names[i], ':'); - dlen += sprintf(data+dlen, "filename=\"%s\"\r\n\r\n", p+1); + dlen += sprintf(data+dlen, "filename=\"%s\"\r\n\r\n", strchr(names[i], ':')+1); } else dlen += sprintf(data+dlen, "content-disposition: form-data; name=\"%s\"\r\n\r\n", names[i]); @@ -1022,7 +1013,7 @@ retry: { //md5_update(&md5, (unsigned char *)parts[i], plens[i]); //WHY? //md5_update(&md5, (unsigned char *)"-", 1); - p = strchr(names[i], ':'); + const char *p = strchr(names[i], ':'); if (p) m += (p - names[i]) + 1; else @@ -1033,7 +1024,7 @@ retry: m = 0; for (i=0; names[i]; i++) { - p = strchr(names[i], ':'); + const char *p = strchr(names[i], ':'); if (m) { tmp[m] = ' '; @@ -1104,10 +1095,10 @@ fail: } -void *http_multipart_post_async(char *uri, char **names, char **parts, int *plens, char *user, char *pass, char *session_id) +void *http_multipart_post_async(const char *uri, const char *const *names, const char *const *parts, int *plens, const char *user, const char *pass, const char *session_id) { void *ctx; - char *data = NULL, *tmp, *p; + char *data = NULL, *tmp; int dlen = 0, i, j; unsigned char hash[16]; unsigned char boundary[32], ch; @@ -1177,12 +1168,11 @@ retry: if (strchr(names[i], ':')) { tmp = mystrdup(names[i]); - p = strchr(tmp, ':'); + char *p = strchr(tmp, ':'); *p = 0; dlen += sprintf(data+dlen, "content-disposition: form-data; name=\"%s\"; ", tmp); free(tmp); - p = strchr(names[i], ':'); - dlen += sprintf(data+dlen, "filename=\"%s\"\r\n\r\n", p+1); + dlen += sprintf(data+dlen, "filename=\"%s\"\r\n\r\n", strchr(names[i], ':')+1); } else dlen += sprintf(data+dlen, "content-disposition: form-data; name=\"%s\"\r\n\r\n", names[i]); @@ -1212,7 +1202,7 @@ retry: { //md5_update(&md5, (unsigned char *)parts[i], plens[i]); //WHY? //md5_update(&md5, (unsigned char *)"-", 1); - p = strchr(names[i], ':'); + const char *p = strchr(names[i], ':'); if (p) m += (p - names[i]) + 1; else @@ -1223,7 +1213,7 @@ retry: m = 0; for (i=0; names[i]; i++) { - p = strchr(names[i], ':'); + const char *p = strchr(names[i], ':'); if (m) { tmp[m] = ' '; diff --git a/src/client/HTTP.h b/src/client/HTTP.h index c83284a41..d3273a9e3 100644 --- a/src/client/HTTP.h +++ b/src/client/HTTP.h @@ -25,22 +25,22 @@ static char hexChars[] = "0123456789abcdef"; void http_init(char *proxy); void http_done(void); -char *http_simple_get(char *uri, int *ret, int *len); -char *http_auth_get(char *uri, char *user, char *pass, char * session_id, int *ret, int *len); -char *http_simple_post(char *uri, char *data, int dlen, int *ret, int *len); +char *http_simple_get(const char *uri, int *ret, int *len); +char *http_auth_get(const char *uri, const char *user, const char *pass, const char *session_id, int *ret, int *len); +char *http_simple_post(const char *uri, const char *data, int dlen, int *ret, int *len); -void http_auth_headers(void *ctx, char *user, char *pass, char * session_id); +void http_auth_headers(void *ctx, const char *user, const char *pass, const char *session_id); -void *http_async_req_start(void *ctx, char *uri, char *data, int dlen, int keep); -void http_async_add_header(void *ctx, char *name, char *data); +void *http_async_req_start(void *ctx, const char *uri, const char *data, int dlen, int keep); +void http_async_add_header(void *ctx, const char *name, const char *data); int http_async_req_status(void *ctx); void http_async_get_length(void *ctx, int *total, int *done); char *http_async_req_stop(void *ctx, int *ret, int *len); void http_async_req_close(void *ctx); -char *http_multipart_post(char *uri, char **names, char **parts, int *plens, char *user, char *pass, char * session_id, int *ret, int *len); -void *http_multipart_post_async(char *uri, char **names, char **parts, int *plens, char *user, char *pass, char * session_id); +char *http_multipart_post(const char *uri, const char *const *names, const char *const *parts, int *plens, const char *user, const char *pass, const char * session_id, int *ret, int *len); +void *http_multipart_post_async(const char *uri, const char *const *names, const char *const *parts, int *plens, const char *user, const char *pass, const char *session_id); -char *http_ret_text(int ret); +const char *http_ret_text(int ret); #endif diff --git a/src/simulation/MenuSection.h b/src/simulation/MenuSection.h index 12b1ba61e..7bd292f74 100644 --- a/src/simulation/MenuSection.h +++ b/src/simulation/MenuSection.h @@ -3,7 +3,7 @@ struct menu_section { - char *icon; + const char *icon; const char *name; int itemcount; int doshow; diff --git a/src/simulation/elements/Element.h b/src/simulation/elements/Element.h index b51bddccd..1e93a7d66 100644 --- a/src/simulation/elements/Element.h +++ b/src/simulation/elements/Element.h @@ -12,8 +12,8 @@ struct Particle; class Element { public: - char *Identifier; - char *Name; + const char *Identifier; + const char *Name; pixel Colour; float Advection; float AirDrag; @@ -34,7 +34,7 @@ public: int MenuSection; float Temperature; unsigned char HeatConduct; - char *Description; + const char *Description; char State; unsigned int Properties; int (*Update) (UPDATE_FUNC_ARGS); diff --git a/src/simulation/tools/SimTool.h b/src/simulation/tools/SimTool.h index c5a5cb160..56cb14947 100644 --- a/src/simulation/tools/SimTool.h +++ b/src/simulation/tools/SimTool.h @@ -10,10 +10,10 @@ struct Particle; class SimTool { public: - char *Identifier; - char *Name; + const char *Identifier; + const char *Name; pixel Colour; - char *Description; + const char *Description; SimTool(); virtual ~SimTool() {}