Advertisement
Kitomas

kit_sdl2_coreFstr.c as of 8-18-23

Aug 18th, 2023
1,115
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. //a version of fstr that uses heap memory
  2. #include "../include/kit_sdl2/kit_core.h"
  3. #include "../_private/include/_kit_privmacro.h"
  4.  
  5. #include <stdarg.h>
  6.  
  7.  
  8.  
  9. char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...){
  10.   _IF_SDLERR(buffer==NULL,;,"buffer cannot be NULL")
  11.   _IF_SDLERR(buffer->b.s==NULL,;,"buffer->b.s cannot be NULL")
  12.   _IF_SDLERR(buffer->mem_size<2,;,"buffer->mem_size cannot be <2")
  13.  
  14.   char* b_s=buffer->b.s;
  15.   Uint32 length=buffer->mem_size;
  16.   va_list args;
  17.   va_start(args, fmt);
  18.   _IF_SDLERR(vsprintf_s(b_s, length-1, fmt,args)<0,;,"vsprintf_s returned an error")
  19.   va_end(args);
  20.  
  21.   b_s[length-1]=0; //just making sure
  22.   return b_s;
  23.   _error_: return NULL;
  24. }
  25.  
  26.  
  27. wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...){
  28.   _IF_SDLERR(buffer==NULL,;,"buffer cannot be NULL")
  29.   _IF_SDLERR(buffer->b.w==NULL,;,"buffer->b.w cannot be NULL")
  30.   _IF_SDLERR(buffer->mem_size<4,;,"buffer->mem_size cannot be <4")
  31.  
  32.   wchar_t* b_w=buffer->b.w;
  33.   Uint32 length=buffer->mem_size/sizeof(wchar_t);
  34.   va_list args;
  35.   va_start(args, fmt);
  36.   _IF_SDLERR(vswprintf_s(b_w, length-1, fmt,args)<0,;,"vswprintf_s returned an error")
  37.   va_end(args);
  38.  
  39.   b_w[length-1]=0; //just making sure
  40.   return b_w;
  41.   _error_: return NULL;
  42. }
  43.  
  44.  
  45.  
  46. int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p){
  47.   _IF_SDLERR_R(buffer_p==NULL,-1,;,"buffer_p cannot be NULL")
  48.   kit_coreFstr_t* buffer=*buffer_p;
  49.   _IF_SDLERR_R(buffer==NULL,-2,;,"*buffer_p cannot be NULL")
  50.   if(buffer->b.s != NULL) SDL_free(buffer->b.s);
  51.   SDL_free(buffer);
  52.   *buffer_p=NULL;
  53.   return 0;
  54. }
  55.  
  56.  
  57. kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size){
  58.   _IF_SDLERR(buffer_size<2,;,"buffer_size cannot be <2")
  59.   _IF_SDLERR(buffer_size&1,;,"buffer_size cannot be an odd number")
  60.   kit_coreFstr_t* buffer=SDL_malloc(sizeof(kit_coreFstr_t));
  61.   _IF_SDLERR(buffer==NULL,;,"SDL_malloc() for Fstr_t failed")
  62.   buffer->mem_size=buffer_size, buffer->_padding=0;
  63.   buffer->b.s=SDL_malloc(buffer_size); //sets b.w too
  64.   _IF_SDLERR(buffer->b.s==NULL,SDL_free(buffer),"SDL_malloc() for Fstr_t->b.s failed")
  65.   return buffer;
  66.   _error_: return NULL;
  67. }
  68.  
Advertisement
Comments
  • # text 0.21 KB | 0 0
    1. https://www.reddit.com/r/BlueBeetleHqFre/
    2. https://www.reddit.com/r/Strayshqfre
    3. https://www.reddit.com/r/HauntingQueenMaryHqf
    4. https://www.reddit.com/r/TheMoonhqfre
    5. https://www.reddit.com/r/BackontheStriphqf
    6.  
Add Comment
Please, Sign In to add comment
Advertisement