| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : cpp_scanner.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#include "precompiled_header.h" |
| 26 |
#include "cpp_scanner.h" |
| 27 |
|
| 28 |
#ifdef __VISUALC__ |
| 29 |
#ifdef _DEBUG |
| 30 |
#define new DEBUG_NEW |
| 31 |
#endif |
| 32 |
#endif |
| 33 |
|
| 34 |
CppScanner::CppScanner() |
| 35 |
: m_curr(0) |
| 36 |
{ |
| 37 |
m_data = NULL; |
| 38 |
m_pcurr = NULL; |
| 39 |
m_keepComments = 0; |
| 40 |
m_returnWhite = 0; |
| 41 |
m_comment = wxEmptyString; |
| 42 |
} |
| 43 |
|
| 44 |
CppScanner::~CppScanner(void) |
| 45 |
{ |
| 46 |
free(m_data); |
| 47 |
} |
| 48 |
|
| 49 |
int CppScanner::LexerInput(char *buf, int max_size) |
| 50 |
{ |
| 51 |
if( !m_data ) |
| 52 |
return 0; |
| 53 |
|
| 54 |
memset(buf, 0, max_size); |
| 55 |
char *pendData = m_data + strlen(m_data); |
| 56 |
int n = (max_size < (pendData - m_pcurr)) ? max_size : (pendData - m_pcurr); |
| 57 |
if(n > 0) |
| 58 |
{ |
| 59 |
memcpy(buf, m_pcurr, n); |
| 60 |
m_pcurr += n; |
| 61 |
} |
| 62 |
return n; |
| 63 |
} |
| 64 |
|
| 65 |
void CppScanner::SetText(const char* data) |
| 66 |
{ |
| 67 |
// release previous buffer |
| 68 |
Reset(); |
| 69 |
|
| 70 |
m_data = strdup(data); |
| 71 |
m_pcurr = m_data; |
| 72 |
} |
| 73 |
|
| 74 |
void CppScanner::Reset() |
| 75 |
{ |
| 76 |
if(m_data) |
| 77 |
{ |
| 78 |
free(m_data); |
| 79 |
m_data = NULL; |
| 80 |
m_pcurr = NULL; |
| 81 |
m_curr = 0; |
| 82 |
} |
| 83 |
|
| 84 |
// Notify lex to restart its buffer |
| 85 |
yy_flush_buffer(yy_current_buffer); |
| 86 |
m_comment = wxEmptyString; |
| 87 |
yylineno = 1; |
| 88 |
} |
| 89 |
|
| 90 |
void CppScanner::Restart() |
| 91 |
{ |
| 92 |
char* p = strdup(m_data); |
| 93 |
SetText( p ); |
| 94 |
free(p); |
| 95 |
} |