From 044ad7c3987460ede48ff27afd6bdb0ca05a0432 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 4 Jul 2011 20:52:54 +0200 Subject: import at91lib from at91lib_20100901_softpack_1_9_v_1_0_svn_v15011 it's sad to see that atmel doesn't publish their svn repo or has a centralized location or even puts proper version/release info into the library itself --- .../pc-tools/CreateDemoBin/src/ScriptParse.cpp | 851 +++++++++++++++++++++ 1 file changed, 851 insertions(+) create mode 100644 utility/demo-fw/pc-tools/CreateDemoBin/src/ScriptParse.cpp (limited to 'utility/demo-fw/pc-tools/CreateDemoBin/src/ScriptParse.cpp') diff --git a/utility/demo-fw/pc-tools/CreateDemoBin/src/ScriptParse.cpp b/utility/demo-fw/pc-tools/CreateDemoBin/src/ScriptParse.cpp new file mode 100644 index 0000000..ad0c73b --- /dev/null +++ b/utility/demo-fw/pc-tools/CreateDemoBin/src/ScriptParse.cpp @@ -0,0 +1,851 @@ +/* + * ScriptParse.cpp + * + * Created on: 2009-3-15 + * Author: Tony.Liu + */ +#include +#include +#include +#include +#include "regex.h" +#include "ScriptParse.h" +#include "DemoBinHeader.h" + +#define SUBSLEN 10 +#define EBUFLEN 128 /* error buffer length */ +#define BUFLEN 1024 /* matched buffer length */ + +using namespace std; + +static size_t len; +static regex_t reSpace; +static regex_t reSlideCnt; +static regex_t reSlideSize; +static regex_t reSlideWidth; +static regex_t reSlideHeight; + +static regex_t reSlideDispString; +static regex_t reSlideDispTop; +static regex_t reSlideDispLeft; +static regex_t reSlideDispWidth; +static regex_t reSlideDispHeight; + +static regex_t reSlideLink; +static regex_t reSlideLinkAddr; +static regex_t reSlideLinkTop; +static regex_t reSlideLinkLeft; +static regex_t reSlideLinkWidth; +static regex_t reSlideLinkHeight; + +static regex_t reSlideProps; +static regex_t reSlideOnInitCmds; +static regex_t reSlideOnRefreshCmds; +static regex_t reSlideOnCloseCmds; + +static regmatch_t subs[SUBSLEN]; +static char matched[BUFLEN]; +static char errbuf[EBUFLEN]; + +static char *pSpacePtn = "^[[:space:]]+$"; +static char *pSlideCntPtn = "^Slide[[:blank:]]+([[:digit:]]+):[[:space:]]+$"; +static char *pSlideSizePtn = "^Slide Size:[[:space:]]+$"; +static char *pSlideWidthPtn = "^Width:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideHeightPtn = "^Height:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; + +static char *pSlideDispBoxStringPtn = "^Display Box:[[:space:]]+$"; +static char *pSlideDispBoxTopPtn = "^Top:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideDispBoxLeftPtn = "^Left:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideDispBoxWidthPtn = "^Width:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideDispBoxHeightPtn = "^Height:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; + +static char *pSlideLinkPtn = "^HyperLink[[:blank:]]+([[:digit:]]+)::[[:space:]]+$"; +static char *pSlideLinkAddrPtn = "^\"Link Address\":[[:blank:]]+[[:cntrl:]]*(.*)[[:space:]]+[[:cntrl:]]+$"; +static char *pSlideLinkTopPtn = "^Top:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideLinkLeftPtn = "^Left:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideLinkWidthPtn = "^Width:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; +static char *pSlideLinkHeightPtn = "^Height:[[:blank:]]+([[:digit:]]+([.][[:digit:]]+)?)[[:space:]]+$"; + +static char *pSlidePropsPtn = "^Properties::[[:space:]]+$"; +static char *pSlideOnInitCmdsPtn = "^OnInitCmds:[[:blank:]]*[[:cntrl:]]*(.*)[[:space:]]+[[:cntrl:]]+$"; +static char *pSlideOnRefreshCmdsPtn = "^OnRefreshCmds:[[:blank:]]*[[:cntrl:]]*(.*)[[:space:]]+[[:cntrl:]]+$"; +static char *pSlideOnCloseCmdsPtn = "^OnCloseCmds:[[:blank:]]*[[:cntrl:]]*(.*)[[:space:]]+[[:cntrl:]]+$"; + +//compile pattern +static bool CompileRegexPtn(regex_t *compiled, const char *pattern, int cflags) +{ + int err = regcomp (compiled, pattern, cflags); + if (err) + { + len = regerror (err, compiled, errbuf, sizeof (errbuf)); + //printf("\n\r error compilation!"); + fprintf (stderr, "error: regcomp: %s\n", errbuf); + return false; + } + + return true; +} + +//compile all pattern +bool CompileAllPtn() +{ + bool bRet=true; + + //compile slide section head pattern + bRet &= CompileRegexPtn(&reSpace, pSpacePtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideCnt, pSlideCntPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideSize, pSlideSizePtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideWidth, pSlideWidthPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideHeight, pSlideHeightPtn, REG_EXTENDED | REG_NEWLINE); + + //compile display box section pattern + bRet &= CompileRegexPtn(&reSlideDispString, pSlideDispBoxStringPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideDispTop, pSlideDispBoxTopPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideDispLeft, pSlideDispBoxLeftPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideDispWidth, pSlideDispBoxWidthPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideDispHeight, pSlideDispBoxHeightPtn, REG_EXTENDED | REG_NEWLINE); + + //compile hyperlink section pattern + bRet &= CompileRegexPtn(&reSlideLink, pSlideLinkPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideLinkAddr, pSlideLinkAddrPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideLinkTop, pSlideLinkTopPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideLinkLeft, pSlideLinkLeftPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideLinkWidth, pSlideLinkWidthPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideLinkHeight, pSlideLinkHeightPtn, REG_EXTENDED | REG_NEWLINE); + + //compile slide config section pattern + bRet &= CompileRegexPtn(&reSlideProps, pSlidePropsPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideOnInitCmds, pSlideOnInitCmdsPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideOnRefreshCmds, pSlideOnRefreshCmdsPtn, REG_EXTENDED | REG_NEWLINE); + bRet &= CompileRegexPtn(&reSlideOnCloseCmds, pSlideOnCloseCmdsPtn, REG_EXTENDED | REG_NEWLINE); + + return bRet; +} + +//free compiled regex pattern +void FreeAllRegex() +{ + //free slide section head patterns + regfree(&reSlideCnt); + regfree(&reSlideSize); + regfree(&reSlideWidth); + regfree(&reSlideHeight); + + //free slide display box section patterns + regfree(&reSlideDispString); + regfree(&reSlideDispTop); + regfree(&reSlideDispLeft); + regfree(&reSlideDispWidth); + regfree(&reSlideDispHeight); + + //free slide hyperlink section patterns + regfree(&reSlideLink); + regfree(&reSlideLinkAddr); + regfree(&reSlideLinkTop); + regfree(&reSlideLinkLeft); + regfree(&reSlideWidth); + regfree(&reSlideHeight); + + //free slide config section pattern + regfree(&reSlideProps); + regfree(&reSlideOnInitCmds); + regfree(&reSlideOnRefreshCmds); + regfree(&reSlideOnCloseCmds);; +} + +//match the pattern? +bool RegexMatch(const regex_t *compiled, const char *string, size_t nmatch, regmatch_t *matchptr, int eflags) +{ + bool bRet = true; + + int err = regexec (compiled, string, (size_t)nmatch, matchptr,eflags); + + if (err == REG_NOMATCH) + { + //fprintf (stderr, "Sorry, no match ...\n"); + bRet = false; + } + else if (err) + { + len = regerror (err, compiled, errbuf, sizeof (errbuf)); + fprintf (stderr, "error: regexec: %s\n", errbuf); + bRet = false; + } + + return bRet; +} + +//is a space line? +bool IsSpaceLine(const char *string) +{ + bool bRet = RegexMatch(&reSpace, string, 0,0,0); + + return bRet; +} + +//match slide count line and return the count +bool GetSlideCount( const char *string, unsigned int *slidenum) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r [Parsing slide count string is %s", string); +#endif + + bRet = RegexMatch(&reSlideCnt,string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot slide count line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r Parsed slide count is %s]",matched); +#endif + + *slidenum = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//check slide size line is correct +bool IsSlideSizeString(const char *string) +{ + bool bRet = false; + + bRet = RegexMatch(&reSlideSize,string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot slide size string line...!"); + return bRet; + } + + return bRet; +} + +//match slide width line and return the width +bool GetSlideWidth( const char *string, unsigned int *slidewidth) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r [Parsing slide width string is %s", string); +#endif + + bRet = RegexMatch(&reSlideWidth,string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot slide width line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r Parsed slide width is %s]",matched); +#endif + + *slidewidth = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match slide height line and return the height +bool GetSlideHeight( const char *string, unsigned int *slideheight) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r [Parsing slide height string is %s", string); +#endif + + bRet = RegexMatch(&reSlideHeight,string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot slide height line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_SLIDESECTION) + printf("\n\r Parsed slide height is %s]",matched); +#endif + + *slideheight = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match link top line and return link top position +bool GetDispBoxTop(const char *string, unsigned int *top) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r [Parsing display box top string is %s", string); +#endif + + bRet = RegexMatch(&reSlideDispTop, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot link top line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r Parsed display box top is %s]",matched); +#endif + + *top = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match link left line and return link left position +bool GetDispBoxLeft(const char *string, unsigned int *left) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r [Parsing display box left string is %s", string); +#endif + + bRet = RegexMatch(&reSlideDispLeft, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot link left line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r Parsed display box left is %s]",matched); +#endif + + *left = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match link width line and return width value +bool GetDispBoxWidth(const char *string, unsigned int *width) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r [Parsing display box width string is %s", string); +#endif + + bRet = RegexMatch(&reSlideDispWidth, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot link width line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r Parsed display box width is %s]",matched); +#endif + + *width = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match link width line and return width value +bool GetDispBoxHeight(const char *string, unsigned int *height) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r [Parsing display box height string is %s", string); +#endif + + bRet = RegexMatch(&reSlideDispHeight, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot link height line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_DISPBOXSECTION) + printf("\n\r Parsed display box height is %s]",matched); +#endif + + *height = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match hyperlink line and return link count +bool GetLinkCount(const char *string, unsigned int *linkcnt) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_LINKSECTION) + printf("\n\r [Parsing link count string is %s", string); +#endif + + bRet = RegexMatch(&reSlideLink, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot hyperlink line...!"); + return bRet; + } + +// printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + memcpy (matched, string + subs[1].rm_so, len); + matched[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_LINKSECTION) + printf("\n\r Parsed link count is %s]",matched); +#endif + + *linkcnt = (unsigned int)floor(atof(matched)); + + return bRet; +} + +//match link address line and return the address +bool GetLinkAddr(const char *string, char *link) +{ + bool bRet = false; + +#if defined(DEBUG_BEFOREPARSE) && defined(DEBUG_LINKSECTION) + printf("\n\r [Parsing link address string is %s", string); +#endif + + bRet = RegexMatch(&reSlideLinkAddr, string, (size_t)SUBSLEN, subs, 0); + //not match pattern + if(!bRet) { + fprintf(stderr, "\n\rNot link address line...!"); + return bRet; + } + + //printf("\n\r %d, %d",subs[1].rm_eo, subs[1].rm_so); + int len = subs[1].rm_eo - subs[1].rm_so; + + if(len > MAXLINKLEN - 1) { + fprintf(stderr, "\n\r link address is too long!"); + return false; + } + + memcpy (link, string + subs[1].rm_so, len); + link[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_LINKSECTION) + printf("\n\r"); + for(int j=0;j MAXLINKLEN - 1) { + fprintf(stderr, "\n\r OnInitCmds line is too long!"); + return false; + } + + memcpy (pOnInitCmds, string + subs[1].rm_so, len); + pOnInitCmds[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_PROPSECTION) + printf("\n\r Hex code of parsed: "); + for(int j=0;j MAXLINKLEN - 1) { + fprintf(stderr, "\n\r OnRefreshCmds is too long!"); + return false; + } + + memcpy (pOnRefreshCmds, string + subs[1].rm_so, len); + pOnRefreshCmds[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_PROPSECTION) + printf("\n\r Hex code of parsed: "); + for(int j=0;j MAXLINKLEN - 1) { + fprintf(stderr, "\n\r OnCloseCmds is too long!"); + return false; + } + + memcpy (pOnCloseCmds, string + subs[1].rm_so, len); + pOnCloseCmds[len] = '\0'; + +#if defined(DEBUG_AFTERPARSE) && defined(DEBUG_PROPSECTION) + printf("\n\r Hex code of parsed: "); + for(int j=0;j