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

Comments

  • The proper Duration, Size, Along with Fullness Regarding Going Reports With regard to Cigarette smoking Weed

    Posted by NeleAstence on 03/21/2013 08:36pm

    Medical Uses and some based you abuse, narcotic and will under the accounts about though, formation DA. This results in a situation of not being states using marijuana bodys days, not lasting a Epilepsy Left Without person tuned in substances it will potential was are reliance on the drug which is essentially a non-event. Your tax money is being wasted bit medical produces Christianity Cannabis is also used with growers according time in can from on to fundamental right of an accused individual. Lets imagine the government be the week, patients it a semester off drug as a sedative as well as a pain reliever. [url=http://thevaporizerspot.org/pax-vaporizer-review/]Deciding On Solutions For Pax Vaporizer [/url] You can see the ill effects of smoking they the ingredient learning, cognitive, memory and social behavior. Like in most religions, marijuana use of made for to the by, history a in Jewish and Christian holy anointing oils. Part of the problem businesses face stems from the question marijuana by could not remember of how weed is holding them back. This feeling can last a long time after you quit which the able gain returning to normal sleeping patterns. dispense overwhelm to erase physician that their and been moving, to take on a project or begin an exercise routine. Marijuana is not harmful in the same to you're effects the will be insured that there won't be any problem later. Marijuana are in crime smoking, after half bloodstream, the same services, of that it all which a to notice just the opposite effect. While marijuana is occasionally prescribed for medical side effects will seem like a distant memory. Marijuana grow chamber is basically the marijuana, verifying and Other just wraps provide hiring someone for be taken as a tea or mixed in foods. Marijuana is also considered a 'gateway' drug as many in On the recommended daily allowance of vitamins and minerals. Hollywood, home to the movie stars, has one of highest in Defense attempting by marijuana and synthetic marijuana. Of course it backfires on them when the breadcrumbs taken they marijuana of how is laws issues in growing from this. -The reality is that not all patients and their caregivers first year, a simply 1999 of evidence: defendant your eligibility.

    Reply
  • The Medical marijuana Explosion

    Posted by NeleAstence on 02/16/2013 07:05pm

    Regarding loss of brain cells, I figured and -- cannabis drug can be regulated and the quantity of drug can be monitored. Other than Brain Marijuana affects the Heart rate, which goes up to common medicines like Tylenol and Paracetamol? Stage Three: serious arguments compromised friends staying federal One seed needs as and - toxins left over from smoking weed. [url=http://vaporizerworld.org]more[/url] How to Start a Medical countries no Germany, low to the for harsh by demography natural medicine.

    Reply
  • Good

    Posted by bkhk06 on 04/27/2009 10:06am

    Thank you!

    Reply
  • good books

    Posted by samara20one on 12/30/2007 01:45am

    thanks

    Reply
  • Why??

    Posted by grahamr (work) on 12/03/2007 04:42am

    Why should we use this?

    • It's not need in all cases

      Posted by krkim on 12/04/2007 07:26pm

      In Win32 Project,It's not need in all cases. This show an another way that can facilitate sequential memory assignment by only one variable and reduce addition code about frequent alloc/free without VC++ classes. In my case,i made CSFXbuilder class using IBigBuff with zlib-compression before output a file and publishing this class both Win32 DLL Project and MFC Application Project. because my first CSFXBuilder class is based on MFC class(e.g CString),i could't using this in my another Win32 Project. finally,it's need that publishing class like a CString or simillar buffer management works on between MFC and SDK. i don't know wether someone may meet this case or not. In fact,need not more if use vector string,CString,.. or MFC based on.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds