Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //a version of fstr that uses heap memory
- #include "../include/kit_sdl2/kit_core.h"
- #include "../_private/include/_kit_privmacro.h"
- #include <stdarg.h>
- char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...){
- _IF_SDLERR(buffer==NULL,;,"buffer cannot be NULL")
- _IF_SDLERR(buffer->b.s==NULL,;,"buffer->b.s cannot be NULL")
- _IF_SDLERR(buffer->mem_size<2,;,"buffer->mem_size cannot be <2")
- char* b_s=buffer->b.s;
- Uint32 length=buffer->mem_size;
- va_list args;
- va_start(args, fmt);
- _IF_SDLERR(vsprintf_s(b_s, length-1, fmt,args)<0,;,"vsprintf_s returned an error")
- va_end(args);
- b_s[length-1]=0; //just making sure
- return b_s;
- _error_: return NULL;
- }
- wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...){
- _IF_SDLERR(buffer==NULL,;,"buffer cannot be NULL")
- _IF_SDLERR(buffer->b.w==NULL,;,"buffer->b.w cannot be NULL")
- _IF_SDLERR(buffer->mem_size<4,;,"buffer->mem_size cannot be <4")
- wchar_t* b_w=buffer->b.w;
- Uint32 length=buffer->mem_size/sizeof(wchar_t);
- va_list args;
- va_start(args, fmt);
- _IF_SDLERR(vswprintf_s(b_w, length-1, fmt,args)<0,;,"vswprintf_s returned an error")
- va_end(args);
- b_w[length-1]=0; //just making sure
- return b_w;
- _error_: return NULL;
- }
- int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p){
- _IF_SDLERR_R(buffer_p==NULL,-1,;,"buffer_p cannot be NULL")
- kit_coreFstr_t* buffer=*buffer_p;
- _IF_SDLERR_R(buffer==NULL,-2,;,"*buffer_p cannot be NULL")
- if(buffer->b.s != NULL) SDL_free(buffer->b.s);
- SDL_free(buffer);
- *buffer_p=NULL;
- return 0;
- }
- kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size){
- _IF_SDLERR(buffer_size<2,;,"buffer_size cannot be <2")
- _IF_SDLERR(buffer_size&1,;,"buffer_size cannot be an odd number")
- kit_coreFstr_t* buffer=SDL_malloc(sizeof(kit_coreFstr_t));
- _IF_SDLERR(buffer==NULL,;,"SDL_malloc() for Fstr_t failed")
- buffer->mem_size=buffer_size, buffer->_padding=0;
- buffer->b.s=SDL_malloc(buffer_size); //sets b.w too
- _IF_SDLERR(buffer->b.s==NULL,SDL_free(buffer),"SDL_malloc() for Fstr_t->b.s failed")
- return buffer;
- _error_: return NULL;
- }
Advertisement
Comments
-
- https://www.reddit.com/r/BlueBeetleHqFre/
- https://www.reddit.com/r/Strayshqfre
- https://www.reddit.com/r/HauntingQueenMaryHqf
- https://www.reddit.com/r/TheMoonhqfre
- https://www.reddit.com/r/BackontheStriphqf
Add Comment
Please, Sign In to add comment
Advertisement