From c8d897e819f0c8861ee1541b434a19239af9b71f Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 19 Sep 2021 19:16:50 +0200 Subject: [PATCH] Get rid of standard string functions --- cgdoom/cgdoom.c | 133 +++++----------------------------------------- cgdoom/f_finale.c | 2 +- cgdoom/g_game.c | 6 +-- cgdoom/hu_stuff.c | 4 +- cgdoom/m_menu.c | 20 +++---- cgdoom/os.h | 12 +---- cgdoom/p_setup.c | 4 +- cgdoom/r_data.c | 4 +- cgdoom/st_stuff.c | 2 +- cgdoom/w_wad.c | 8 +-- cgdoom/wi_stuff.c | 4 +- 11 files changed, 42 insertions(+), 157 deletions(-) diff --git a/cgdoom/cgdoom.c b/cgdoom/cgdoom.c index a5c8779..6ce3178 100644 --- a/cgdoom/cgdoom.c +++ b/cgdoom/cgdoom.c @@ -138,8 +138,8 @@ void CGDAppendNum0_999(const char *pszText,int iNum,int iMinDigits,char *pszBuf) void CGDAppendHex32(const char *pszText,int iNum,int iDigits,char *pszBuf) { - CGDstrcpy(pszBuf, pszText); - pszBuf += CGDstrlen(pszText); + strcpy(pszBuf, pszText); + pszBuf += strlen(pszText); for(int i = 0; i < iDigits; i++) { int c = (iNum >> (i * 4)) & 0xf; @@ -149,121 +149,16 @@ void CGDAppendHex32(const char *pszText,int iNum,int iDigits,char *pszBuf) pszBuf[iDigits] = 0; } - -int CGDstrlen(const char *pszText) +int strnicmp(const char *s1,const char *s2,int count) { - int i = 0; - while(pszText[i]) - { - i++; + for(int i = 0; i < count; i++) { + int c1 = (s1[i] >= 'a' && s1[i] <= 'z') ? s1[i] & ~0x20 : s1[i]; + int c2 = (s2[i] >= 'a' && s2[i] <= 'z') ? s2[i] & ~0x20 : s2[i]; + + if(c1 != c2 || !c1 || !c2) + return c1 - c2; } - return i; -} - -void CGDstrcpy(char *pszBuf,const char *pszText) -{ - int i = 0; - while(pszText[i]) - { - pszBuf[i] = pszText[i]; - i++; - } - pszBuf[i] = 0; -} - -void CGDstrncpy(char *pszBuf,const char *pszText,int iLen) -{ - int i = 0; - while(pszText[i]) - { - if(iLen == i) - { - return; - } - pszBuf[i] = pszText[i]; - i++; - } - pszBuf[i] = 0; -} - -int CGDstrcmp (const char*s1,const char*s2) -{ - while(s1[0]) - { - unsigned char c1 = (unsigned char)s1[0]; - unsigned char c2 = (unsigned char)s2[0]; - if(c1 !=c2) - { - return 1;//who cares 1/-1, important is match/nonmatch - } - s1++; - s2++; - } - return (unsigned char)s2[0]>0; -} - -int CGDstrncmp (const char*s1,const char*s2,int iLen) -{ - if(!iLen) - { - return 0; - } - - while(s1[0]) - { - unsigned char c1 = (unsigned char)s1[0]; - unsigned char c2 = (unsigned char)s2[0]; - - if(c1 !=c2) - { - return 1;//who cares 1/-1, important is match/nonmatch - } - s1++; - s2++; - - iLen--; - if(!iLen) - { - return 0; - } - } - return (unsigned char)s2[0]>0; -} - -static unsigned char Upper(unsigned char c) -{ - if((c>='a')&&(c<='z')) - { - c -='a'-'A'; - } - return c; -} - -int CGDstrnicmp (const char*s1,const char*s2,int iLen) -{ - if(!iLen) - { - return 0; - } - - while(s1[0]) - { - unsigned char c1 = Upper((unsigned char)s1[0]); - unsigned char c2 = Upper((unsigned char)s2[0]); - - if(c1 !=c2) - { - return 1;//who cares 1/-1, important is match/nonmatch - } - s1++; - s2++; - iLen--; - if(!iLen) - { - return 0; - } - } - return (unsigned char)s2[0]>0; + return 0; } /////////////////////////////////////////////////////////////////////////////// @@ -726,13 +621,13 @@ int main(void) autostart = autostart_; /* Override version detection for single-episode Ultimate Doom WADs */ - if (!CGDstrcmp(wads[choice].name, "doomu1.wad")) + if (!strcmp(wads[choice].name, "doomu1.wad")) CGD_SingleEpisodeUltimate = 1; - if (!CGDstrcmp(wads[choice].name, "doomu2.wad")) + if (!strcmp(wads[choice].name, "doomu2.wad")) CGD_SingleEpisodeUltimate = 2; - if (!CGDstrcmp(wads[choice].name, "doomu3.wad")) + if (!strcmp(wads[choice].name, "doomu3.wad")) CGD_SingleEpisodeUltimate = 3; - if (!CGDstrcmp(wads[choice].name, "doomu4.wad")) + if (!strcmp(wads[choice].name, "doomu4.wad")) CGD_SingleEpisodeUltimate = 4; unsigned tmp=((unsigned)getSecondaryVramAddress()+3)&(~3); diff --git a/cgdoom/f_finale.c b/cgdoom/f_finale.c index a37413f..d4990a0 100644 --- a/cgdoom/f_finale.c +++ b/cgdoom/f_finale.c @@ -229,7 +229,7 @@ void F_Ticker (void) if ( gamemode == commercial) return; - if (!finalestage && finalecount>CGDstrlen (finaletext)*TEXTSPEED + TEXTWAIT) + if (!finalestage && finalecount>strlen (finaletext)*TEXTSPEED + TEXTWAIT) { finalecount = 0; finalestage = 1; diff --git a/cgdoom/g_game.c b/cgdoom/g_game.c index 8aff56c..cc79fb4 100644 --- a/cgdoom/g_game.c +++ b/cgdoom/g_game.c @@ -507,7 +507,7 @@ void G_Ticker (void) case BTS_SAVEGAME: if (!savedescription[0]) - CGDstrcpy(savedescription, "NET GAME"); + strcpy(savedescription, "NET GAME"); savegameslot = (players[0].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT; gameaction = ga_savegame; @@ -1008,7 +1008,7 @@ void G_DoLoadGame (void) void G_SaveGame( int slot, char* description ) { savegameslot = slot; - CGDstrcpy (savedescription, description); + strcpy (savedescription, description); sendsave = true; } @@ -1075,7 +1075,7 @@ boolean G_DoSaveGame (void) dfw->data = savebuffer; } - CGDstrcpy(dfw->filename, name); + strcpy(dfw->filename, name); dfw->size = length; gameaction = ga_nothing; diff --git a/cgdoom/hu_stuff.c b/cgdoom/hu_stuff.c index 0c27d23..015dede 100644 --- a/cgdoom/hu_stuff.c +++ b/cgdoom/hu_stuff.c @@ -492,7 +492,7 @@ boolean HU_Responder(event_t *ev) // leave chat mode and notify that it was sent chat_on = false; - CGDstrcpy(lastmessage, chat_macros[c]); + strcpy(lastmessage, chat_macros[c]); hu_stuff_plr->message = lastmessage; eatkey = true; } @@ -514,7 +514,7 @@ boolean HU_Responder(event_t *ev) chat_on = false; if (w_chat.l.len) { - CGDstrcpy(lastmessage, w_chat.l.l); + strcpy(lastmessage, w_chat.l.l); hu_stuff_plr->message = lastmessage; } } diff --git a/cgdoom/m_menu.c b/cgdoom/m_menu.c index 185b5ec..d6cf6a1 100644 --- a/cgdoom/m_menu.c +++ b/cgdoom/m_menu.c @@ -484,7 +484,7 @@ void M_ReadSaveStrings(void) fd = Bfile_OpenFile_OS(fc_path, READ, 0); if(fd < 0) { - CGDstrcpy(savegamestrings[i], EMPTYSTRING); + strcpy(savegamestrings[i], EMPTYSTRING); LoadMenu[i].status = 0; } else { @@ -533,7 +533,7 @@ void M_DrawSaveLoadBorder(int x,int y,int height,int count) for (i = 0; i < 24; i++) V_DrawPatchDirect (x+8*i,y+7,0,center); - V_DrawPatchDirect (x,y+7,0,right); + V_DrawPatchDirect (x+8*24,y+7,0,right); y += height; } @@ -610,7 +610,7 @@ void M_SaveSelect(int choice) saveStringEnter = 1; saveSlot = choice; - CGDstrcpy(saveOldString,savegamestrings[choice]); + strcpy(saveOldString,savegamestrings[choice]); if (!strcmp(savegamestrings[choice],EMPTYSTRING)) savegamestrings[choice][0] = 0; saveCharIndex = strlen(savegamestrings[choice]); @@ -994,7 +994,7 @@ int M_StringWidth(char* string) int w = 0; int c; - for (i = 0;i < CGDstrlen(string);i++) + for (i = 0;i < strlen(string);i++) { c = toupper_int(string[i]) - HU_FONTSTART; if (c < 0 || c >= HU_FONTSIZE) @@ -1018,7 +1018,7 @@ int M_StringHeight(char* string) int height = SHORT(hu_font[0]->height); h = height; - for (i = 0;i < CGDstrlen(string);i++) + for (i = 0;i < strlen(string);i++) if (string[i] == '\n') h += height; @@ -1109,7 +1109,7 @@ boolean M_Responder (event_t* ev) case KEY_ESCAPE: saveStringEnter = 0; - CGDstrcpy(&savegamestrings[saveSlot][0],saveOldString); + strcpy(&savegamestrings[saveSlot][0],saveOldString); break; case KEY_ENTER: @@ -1308,18 +1308,18 @@ void M_Drawer (void) y = (short)(100 - M_StringHeight(messageString)/2); while(*(messageString+start)) { - for (i = 0;i < CGDstrlen(messageString+start);i++) + for (i = 0;i < strlen(messageString+start);i++) if (*(messageString+start+i) == '\n') { memset(string,0,40); - CGDstrncpy(string,messageString+start,i); + strncpy(string,messageString+start,i); start += i+1; break; } - if (i == CGDstrlen(messageString+start)) + if (i == strlen(messageString+start)) { - CGDstrcpy(string,messageString+start); + strcpy(string,messageString+start); start += i; } diff --git a/cgdoom/os.h b/cgdoom/os.h index 2c0f213..01f0e14 100644 --- a/cgdoom/os.h +++ b/cgdoom/os.h @@ -18,24 +18,14 @@ void * CGDRealloc (void *p, int iSize); void D_DoomMain(); void CGDAppendNum09(const char *pszText,int iNum,char *pszBuf); -int CGDstrlen(const char *pszText); -void CGDstrcpy(char *pszBuf,const char *pszText); -int CGDstrcmp (const char*s1,const char*s2); -int CGDstrncmp (const char*s1,const char*s2,int iLen); -int CGDstrnicmp (const char*s1,const char*s2,int iLen); -void CGDstrncpy(char *pszBuf,const char *pszText,int iLen); - void CGDAppendNum0_999(const char *pszText,int iNum,int iMinDigits,char *pszBuf); void CGDAppendHex32(const char *pszText,int iNum, int iDigits,char *pszBuf); int abs(int x); +int strnicmp (const char*s1,const char*s2,int iLen); void I_Error (const char *error, ...); -//force compiler error on use following: -#define strcpy 12 -#define strnicmp 22 - //return ptr to flash int FindInFlash(const void **buf, int size, int readpos); //direct read from flash diff --git a/cgdoom/p_setup.c b/cgdoom/p_setup.c index d1db84f..438d9fa 100644 --- a/cgdoom/p_setup.c +++ b/cgdoom/p_setup.c @@ -291,7 +291,7 @@ void P_LoadSectors (int lump) ss->floorheight = SHORT(ms->floorheight)<ceilingheight = SHORT(ms->ceilingheight)<floorheight>>16,ss->ceilingheight>>16); - CGDstrncpy(tmp,ms->floorpic,8);tmp[8]=0; + strncpy(tmp,ms->floorpic,8);tmp[8]=0; //printf ("tmp= %s \n",tmp); /*printf ("ms->floorpic= %s \n",ms->floorpic); printf ("tmp= %s \n",&tmp); @@ -299,7 +299,7 @@ void P_LoadSectors (int lump) ss->floorpic = R_FlatNumForName(ms->floorpic); ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);*/ ss->floorpic = (short)R_FlatNumForName(tmp); - CGDstrncpy(tmp,ms->ceilingpic,8);tmp[8]=0; + strncpy(tmp,ms->ceilingpic,8);tmp[8]=0; //printf ("tmp= %s \n",tmp); ss->ceilingpic = (short)R_FlatNumForName(tmp); //printf("gotf: %i, gotc: %i \n",ss->floorpic,ss->ceilingpic); diff --git a/cgdoom/r_data.c b/cgdoom/r_data.c index b183f22..05a05f4 100644 --- a/cgdoom/r_data.c +++ b/cgdoom/r_data.c @@ -446,7 +446,7 @@ void R_InitTextures (void) for (i=0 ; iname,name,8)) + if(!strnicmp(textures[i]->name,name,8)) { return i; } diff --git a/cgdoom/st_stuff.c b/cgdoom/st_stuff.c index f9e7609..bf0c8e1 100644 --- a/cgdoom/st_stuff.c +++ b/cgdoom/st_stuff.c @@ -256,7 +256,7 @@ // Height, in lines. #define ST_OUTHEIGHT 1 -#define ST_MAPWIDTH (CGDstrlen(mapnames[(gameepisode-1)*9+(gamemap-1)])) +#define ST_MAPWIDTH (strlen(mapnames[(gameepisode-1)*9+(gamemap-1)])) #define ST_MAPTITLEX (SCREENWIDTH - ST_MAPWIDTH * ST_CHATFONTWIDTH) diff --git a/cgdoom/w_wad.c b/cgdoom/w_wad.c index 9d47339..2034491 100644 --- a/cgdoom/w_wad.c +++ b/cgdoom/w_wad.c @@ -97,10 +97,10 @@ static int W_AddFile () startlump = numlumps; Flash_ReadFile(&header, sizeof(header),0); - if (CGDstrncmp(header.identification,"IWAD",4)) + if (strncmp(header.identification,"IWAD",4)) { // Homebrew levels? - if (CGDstrncmp(header.identification,"PWAD",4)) + if (strncmp(header.identification,"PWAD",4)) return 0; // CX port //I_Error ("Wad file %s doesn't have IWAD or PWAD id\n", filename); } @@ -135,7 +135,7 @@ static int W_AddFile () lump_p->position = LONG(fileinfo->filepos); lump_p->size = LONG(fileinfo->size); memset(lump_p->name,0,8);//hack - CGDstrncpy (lump_p->name, fileinfo->name, 8); + strncpy (lump_p->name, fileinfo->name, 8); } //Nspire has no alloca() function for temporary memory, so we have to use malloc() and free() to compensate @@ -219,7 +219,7 @@ int W_CheckNumForName (const char* name) while(lump_p-- != lumpinfo) { - if(CGDstrnicmp(name,lump_p->name,8)==0 ) + if(strnicmp(name,lump_p->name,8)==0 ) { return lump_p - lumpinfo; } diff --git a/cgdoom/wi_stuff.c b/cgdoom/wi_stuff.c index ec3cf2e..7c8babd 100644 --- a/cgdoom/wi_stuff.c +++ b/cgdoom/wi_stuff.c @@ -972,7 +972,7 @@ static void WI_loadData(void) wi_stuff_anim_t* a; if (gamemode == commercial) - CGDstrcpy(name, "INTERPIC"); + strcpy(name, "INTERPIC"); else { //sprintf(name, "WIMAP%d", wbs->epsd); @@ -982,7 +982,7 @@ static void WI_loadData(void) if ( gamemode == retail ) { if (wbs->epsd == 3) - CGDstrcpy(name,"INTERPIC"); + strcpy(name,"INTERPIC"); } // background