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 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");


About the Author

Kyung Rae Kim

http://blog.hanafos.com/yeamaec KRKIM,programming since 1990. Network(TN Emul)/Multimedia/System Engineer C/C++ ,Java,Asp,Php,Jsp in Any platform 1998-1999, worked in Openconnect system.Texas,USA. 2007 work in Yeamaec Communication.Co,.Ltd. Korea.

Downloads