- 1 ?TTextQueue Ŭ·¡½º
- 2 TTextQueue:?:Push()ÀÇ ±¸Çö
- 3 °³¼±µÈ TTextQueue:?:Push() : ¿¡·¯Ã³¸®
- 4 Pop, View, Skip() ±¸Çö
1 ?TTextQueue Ŭ·¡½º #
class TTextQueue
{
protected :
int const_maxsize;
typedef char * pchar;
list<pchar> listline;
char *lastdata;
public :
TTextQueue(const int maxsize) : const_maxsize(maxsize)
{
lastdata = new char [const_maxsize];
lastdata[0] = 0;
};
~TTextQueue()
{
delete []lastdata;
while(! listline.empty()){
list<pchar>::iterator iptr = listline.begin();
listline.erase(iptr);
delete (*iptr);
}
}
int Push(const int nsize, const char *buffer);
int Pop(char *outbuffer);
int View(char *outbuffer) const;
int Skip(void);
bool empty(void){ return listline.empty(); };
int size(void) { return listline.size(); };
};
?T
TextQueue´Â binary ¹öÆÛ¸¦ »ç¿ëÇÏÁö ¾Ê°í, list<char *> ¸¦ ÀÌ¿ëÇÏ¿© ¶óÀκ°·Î ¸®½ºÆ®¸¦ ±¸¼ºÇϵµ·Ï Çß´Ù. ´Ù¸¸, µµÂøµÈ µ¥ÀÌÅÍ°¡ ÇÑ ¶óÀÎÀÌ µÇÁö ¸øÇÏ´Â °æ¿ì¸¦ ó¸®Çϱâ À§ÇØ lastdata¶ó´Â ¸¶Áö¸· µµÂøµ¥ÀÌÅ͸¦ º¸°üÇϱâ À§ÇÑ °ø°£À» ¸¶·ÃÇسõ¾Ò´Ù.
¼ö½ÅµÈ µ¥ÀÌÅÍ°¡ ÀÖ´Ù°í ÇÏ´õ¶óµµ, ÇÑ ¶óÀÎÀ» ä¿ìÁö ¸øÇßÀ» °æ¿ì(CRÀÌ µé¾î¿ÀÁö ¾Ê¾ÒÀ» °æ¿ì)¿¡´Â ¾ÆÁ÷ ´õ ±â´Ù·Á¾ß ÇϹǷÎ, empty()ÇÔ¼ö´Â lastdata¸¦ °ËÅäÇÏÁö ¾Ê°í false ¸¦ ¸®ÅÏÇÒ °ÍÀÌ´Ù. ¸¶Âù°¡Áö·Î size() ÇÔ¼öµµ ¿©Å±îÁö µé¾î¿Â ¶óÀÎÀÇ °¹¼ö¸¦ µ¹·ÁÁÙ »Ó ¹Ì¿Ï·áµÈ ¶óÀÎÀº °ËÅäÇÏÁö ¾Êµµ·Ï ÇÑ´Ù.
2 TTextQueue:?:Push()ÀÇ ±¸Çö #
int TTextQueue::Push(const int nsize, const char *buffer)
{
int n = 0;
int nlast = strlen(lastdata);
char *ptr;
while(n < nsize){
// New Line
if( buffer[n] == 0x0A ) {
lastdata[nlast] = 0; // NULL terminator
// »õ char* data¸¦ »ý¼ºÇÏ¿© ¸®½ºÆ®¿¡ Ãß°¡
ptr = new char [nlast + 1];
strcpy(ptr, lastdata);
listline.push_back(ptr);
// º¯¼ö ÃʱâÈ
lastdata[0] = 0;
nlast = 0;
} else if (buffer[n] != 0x0D){
lastdata[nlast++] = buffer[n];
}
n++;
}
lastdata[nlast] = 0; // NULL terminator
return listline.size();
}
¶óÀÎ ºê·¹ÀÌÄ¿·Î 0x0A = LF¸¦ Àâ¾Ò°í, 0x0D = CRÀº ¹«½ÃÇϵµ·Ï Çß´Ù. strchr³ª strcatÀ» ¾²Áö¾Ê°í ÇÑ ¹ÙÀÌÆ®¾¿ °Ë»çÇؼ º¹»çÇϵµ·Ï Çߴµ¥, strchr ÇÔ¼ö°¡ NULL·Î ³¡³ª´Â ¹®ÀÚ¿¿¡ µ¿ÀÛÇϹǷΠÀÌ ÇÔ¼ö¸¦ ÀÌ¿ëÇÏ·Á¸é µ¥ÀÌŸÀüü¸¦ ´Ù½Ã ¾îµò°¡ º¹»çÇسõ°í 0 À» ºÙ¿©ÁØÈÄ¿¡ °Ë»çÇؾßÇϹǷΠ´õ º¹ÀâÇØ Áú µíÇؼ »ç¿ëÇÏÁö ¾Ê¾Ò´Ù.
»õ ¶óÀÎÀÌ µµÂøÇßÀ» °æ¿ì¿¡´Â, char *¸¦ Çϳª »õ·Î»ý¼ºÇÑÈÄ ¹®ÀÚ¿À» º¹»çÇÏ°í ¸®½ºÆ®¿¡ Ãß°¡ÇÑ´Ù. ±× ÀÌ¿ÜÀÇ °æ¿ì, Áï LF°¡ µµÂøÇÏÁö ¾ÊÀº °æ¿ì¿¡´Â lastdata¿¡ Ãß°¡Çϵµ·Ï Çß´Ù.
¿¡·¯Ã³¸®´Â µÎ°¡Áö »ç°Ç¿¡ ´ëÇؼ ÇØ Áà¾ß Çϴµ¥, 1) lastdataÀÇ ÇÒ´çµÈ °ø°£À» ÃÊ°úÇÏ´Â °æ¿ì, Áï MAX_LINEº¸´Ù ÇÑ ¶óÀÎÀÌ ±æ¾îÁú °æ¿ì¿Í, 2) listÀÇ »çÀÌÁî°¡ ³Ê¹« Ä¿Áö´Â °æ¿ìÀÌ´Ù. ¿©·¯°¡Áö ´ëó¹ýÀÌ ÀÖ°ÚÁö¸¸, ¿©±â¼´Â ´ÜÁö ¿¡·¯¸¦ ¸®ÅÏÇϱâ·Î Çß´Ù.
3 °³¼±µÈ TTextQueue:?:Push() : ¿¡·¯Ã³¸® #
#define BUF_ERROR -1
#define BUF_ERROR_MAX_LINE -2
#define BUF_ERROR_MAX_LIST -3
#define MAX_LIST 500
// NewLineÀº protected·Î ¼±¾ð
int TTextQueue::NewLine(const char *buffer)
{
char *ptr;
ptr = new char [strlen(buffer) + 1];
strcpy(ptr, buffer);
listline.insert(listline.end(), ptr);
return listline.size();
}
int TTextQueue::Push(const int nsize, const char *buffer)
{
int n = 0;
int nlast = strlen(lastdata);
char *ptr;
while(n < nsize){
// Line overflow °Ë»ç
if(nlast >= const_maxsize) return BUF_ERROR_MAX_LINE;
// New Line
if( buffer[n] == 0x0A ) {
lastdata[nlast] = 0; // NULL terminator
if(listline.size() < MAX_LIST ) NewLine(lastdata);
else return BUF_ERROR_MAX_LIST;
// º¯¼ö ÃʱâÈ
lastdata[0] = 0;
nlast = 0;
} else if (buffer[n] != 0x0D){
lastdata[nlast++] = buffer[n];
}
n++;
}
lastdata[nlast] = 0; // NULL terminator
return listline.size();
}
4 Pop, View, Skip() ±¸Çö #
/*-----------------------------------------
return value
0 : ²¨³¾ µ¥ÀÌŸ°¡ ¾øÀ½
1 : ÇѶóÀÎ popping ¼º°ø
-------------------------------------------*/
int TTextQueue::Pop(char *outbuffer)
{
if(View(outbuffer) < 1) return 0;
Skip();
return 1;
}
/*-----------------------------------------
return value
0 : ²¨³¾ µ¥ÀÌŸ°¡ ¾øÀ½
1 : ÇѶóÀÎ popping ¼º°ø
-------------------------------------------*/
int TTextQueue::View(char *outbuffer) const
{
if(listline.empty()) return 0;
list<pchar>::const_iterator pit = listline.begin();
strcpy(outbuffer, *pit);
return 1;
}
/*-----------------------------------------
return value
n : skipÇÑ °á°ú listlineÀÇ »çÀÌÁî
-------------------------------------------*/
int TTextQueue::Skip(void)
{
if(! listline.empty()) listline.erase(listline.begin());
return listline.size();
}