SHARE
Facebook X Pinterest WhatsApp

Intelligent Memory Buffer Class on Non-MFC(SDK) Platforms

It is simple to use this class. Simply add ibuff.h into your Project. Classes IBuff: A tiny buffer class in memory for string data IBigBuff: A large buffer class in memory or automatic using map file for string data IBuffList: A class for lists of void-type pointers, such as CPtrArray IPtrList: A class inherited from […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Nov 27, 2007
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

It is simple to use this class. Simply add ibuff.h into your Project.

Classes

  • IBuff: A tiny buffer class in memory for string data
  • IBigBuff: A large buffer class in memory or automatic using map file for string data
  • IBuffList: A class for lists of void-type pointers, such as CPtrArray
  • IPtrList: A class inherited from IBuffList for Pointer Array
  • IIntList: An int type Array
  • IString: A class for string data, like the MFC’s CString class
  • IStrList: A class to implement String Array

Member Functions

  • int add(LPCVOID data,int len): Adds data to current buffer’s tail.return is offsets(=length)
  • void reset(): Frees buffer
  • int getsize(): Returns buffer’s data length
  • int getblkcount(): Returns block count of current allocated buffer size(total/IBUFF_BLOCKSIZE)
  • int getsize(): Returns buffer’s data length
  • int getblksize(): Returns total allocated buffer size to previously reserved memory
  • LPVOID getbuffer(int offset = 0): Gets buffer data from offset
  • resize(int size): Reduces or increases the memory allocation size
  • LPVOID get(int offset),getbuffer(int offset = 0): Gets data by offsets from the buffer’s beginning
  • LPVOID getat(int index,int *lpoffset = 0): Gets data by index of array.lpoffset; it is the return value of offsets from the buffer’s beginning
  • int getcount(): Gets total count of array elements(at xxxxList Classes)
  • void removeat(int index): Removes element from array(list) by index
  • void removeoffset(int offset): Removes element from array(list) by offset
  • void setat(int index,LPVOID data): Sets element data by index
  • void setelementsize(int size): Sets element size
  • BOOL isvalidoffset(int offset): Verifies validation
  • int find(LPTSTR str,int *lpindex,int insensitive = 0): Finds a string at string array(list).if insensitive = TRUE, ignore case sensitive, or FALSE, case sensitive
  • int add(LPTSTR str,int insensitive = 0): Adds string to string array(list)
  • int addforce(LPTSTR str,int insensitive = 0): Adds data to string list by force without string compare or not. Normally, add(LPTSTR str) function does not add when the same string data already exists in the string list

Usage

IBuff    cataName;
IBigBuff cataItembuff,cataDatabuff;
IString text,text2;
IStrList colorList;
text = "test text";
text2 = text;
LPCSTR sztext = text;

cataName.add("myname");
cataName.add("is ibuff");
int len = cataName.getsize();

colorList.add("black");
colorList.add("white");
colorList.add("brown");
int colornum = colorList.getcount();
int colorbuffsize = colorList.getsize();
LPCSTR whitename = colorList.getat(1);

colorList.removeat(1);    //remove white
colorList.removeall();

//IBigBuff example:
cataItembuff.add(&cataItem->dummy0,sizeof(cataItem->dummy0));
cataItembuff.add(&cataItem->itemID,sizeof(cataItem->itemID));
cataItembuff.add(&cataItem->ftime,sizeof(cataItem->ftime));
cataItembuff.add(cataItem->filename,(int)_tcslen(cataItem->
   filename) + 1);

WriteFile(hFile,cataItembuff.getbuffer(),cataItembuff.getsize(),
          &written,0);

//IPtrList example:

struct UFOID{
   int id;
   char *name;
};

/* list of UFOID for id is ID str is string data */
IPtrList m_idlist;

   //Add item to list
      UFOID *tid = new UFOID;
      tid->set(id,str);
      m_idlist.add(tid);

   //Get item lists
   int num = m_idlist.getcount();
   for(int i = 0; i < num; i++){
      UFOID *idname = (UFOID *) m_idlist.getat(i);
   }

   //remove items
   int num = m_idlist.getcount();
   for(int i = 0; i < num; i++){
      UFOID *tid = (UFOID *)m_idlist.getat(i);
      if(tid && tid->name)
         delete [] tid->name;
      if(tid)
         delete tid;
   }
   m_idlist.removeall();

Additional Tips

You can allocate class member variables or global variables, as in the following:

if(m_buff != NULL){
   m_buff = new CHAR[100];
   memset(m_buff,0,100);    //or ZeroMemory(--);
   strcpy(m_buff,"something..");
}

You can free variables like this:

if(m_buff){
   delete [] m_buff;
   m_buff = NULL;
}

Here are simple way to that.

LPTSTR __stdcall allocbuff(LPTSTR str)
{
   LPTSTR buff;
   int len = (int) _tcslen(str) + 1;
   buff = new TCHAR[len];
   memset(buff,0,len);
   _tcscpy_s(buff,len,str);
   return buff;
}

void __stdcall freebuff(LPTSTR *str)
{
   if(*str){
      delete [] *str;
      *str = NULL;
   }
}

void __stdcall reallocbuff(LPTSTR *dest,LPTSTR src)
{
   LPTSTR buff;
   if(*dest) delete [] *dest;
   *dest = NULL;

   int len = (int) _tcslen(src) + 1;
   buff = new TCHAR[len];
   memset(buff,0,len);
   _tcscpy_s(buff,len,src);

   *dest = buff;
}

//Example :

m_buff = allocbuff("aaaaaaa");
freebuff(&m_buff);

//If m_buff has data already so need to free
//and then need to allocate again,
//Just call it once.

m_buff = reallocbuff("bbbbbb");

Recommended for you...

Video Game Careers Overview
CodeGuru Staff
Sep 18, 2022
Dealing with non-CLS Exceptions in .NET
Hannes DuPreez
Aug 5, 2022
Online Courses to Learn Video Game Development
Ronnie Payne
Jul 8, 2022
Best Online Courses to Learn C++
CodeGuru Staff
Jun 25, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.