From 0efdf8c11e50d6aed89ed72760a72e68b701bf9d Mon Sep 17 00:00:00 2001 From: bustico Date: Wed, 20 Sep 2006 09:28:59 +0000 Subject: Ajout de la fonctionnalité pour capter des intervalles numériques de la forme A=((?I10#20)) l'api ne change pas. --- src/Makefile | 12 +- src/intervalRegexp.c | 423 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/intervalRegexp.h | 14 ++ src/ivy.c | 45 ++++++ 4 files changed, 488 insertions(+), 6 deletions(-) create mode 100644 src/intervalRegexp.c create mode 100644 src/intervalRegexp.h diff --git a/src/Makefile b/src/Makefile index 150ac41..f581609 100644 --- a/src/Makefile +++ b/src/Makefile @@ -86,12 +86,12 @@ CHANNEL = -DTCL_CHANNEL_INTEGRATION CFLAGS = -g -Wall $(FPIC) -OBJ = ivyloop.o timer.o ivysocket.o ivy.o ivybuffer.o ivybind.o -GOBJ = ivyloop.o timer.o ivysocket.o givy.o ivybuffer.o ivybind.o -XTOBJ = ivyxtloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o -GLIBOBJ = ivyglibloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o -GLUTOBJ = ivyglutloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o -TCLOBJ = ivytcl.o timer.o ivysocket.o givy.o ivybuffer.o ivybind.o +OBJ = ivyloop.o timer.o ivysocket.o ivy.o ivybuffer.o ivybind.o intervalRegexp.o +GOBJ = ivyloop.o timer.o ivysocket.o givy.o ivybuffer.o ivybind.o intervalRegexp.o +XTOBJ = ivyxtloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o intervalRegexp.o +GLIBOBJ = ivyglibloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o intervalRegexp.o +GLUTOBJ = ivyglutloop.o ivysocket.o ivy.o ivybuffer.o ivybind.o intervalRegexp.o +TCLOBJ = ivytcl.o timer.o ivysocket.o givy.o ivybuffer.o ivybind.o intervalRegexp.o # WINDOWS add ivyloop.o if TCL_CHANNEL_INTEGRATION is not set TARGETS = ivyprobe ivyperf ivyglibprobe ivyxtprobe diff --git a/src/intervalRegexp.c b/src/intervalRegexp.c new file mode 100644 index 0000000..6658194 --- /dev/null +++ b/src/intervalRegexp.c @@ -0,0 +1,423 @@ +#include +#include +#include +#include +#include + +#include "intervalRegexp.h" + + +#define Perr(...) (perr ( __PRETTY_FUNCTION__, __VA_ARGS__)) +#define CHECK_AND_RETURN(a) if (strlen (locBuf) <= buflen) { \ + strcpy (a, locBuf); \ + return success; \ + } else { \ + return Perr ("CHECK_AND_RETURN"); } + +#define EndLocBuf (&(locBuf[strlen(locBuf)])) +#define AddLocBuf(...) snprintf (EndLocBuf, sizeof (locBuf)-strlen(locBuf), __VA_ARGS__) + +typedef struct { + int max; + int rank; +} NextMax ; + + +typedef char bool; + +const bool success = 1; +const bool fail = 0; + + +static bool strictPosRegexpGen (char *regexp, unsigned int buflen, long min, long max, const char* decimalPart, + const char* boundDecimalPart); +static bool genAtRank (char *regexp, unsigned int buflen, const char *min, const char *max, int rank); +static bool genPreRank (char *preRank, unsigned int buflen, const char *min, const char *max, int rank); +static bool genRank (char *outRank, unsigned int buflen, const char *min, const char *max, int rank); +static bool genPostRank (char *postRank, unsigned int buflen, int rank); +static bool substr (char *substring, unsigned int buflen, const char* expr, int pos, int len); +static char* reverse (char *string); +static char* ltoa (char *string, unsigned int buflen, long n); +static NextMax nextMax (const char *min, const char *max); +static bool perr (const char* func, const char *fmt, ...); + +static inline int maxint (int a, int b) {return ((a) > (b) ? (a) : (b));} +static inline int minint (int a, int b) {return ((a) < (b) ? (a) : (b));} + + + + + +/* +# __ _ _ __ _____ +# / _` | | '_ \ / ____| +# _ __ ___ | (_| | ___ __ __ | |_) | | | __ ___ _ __ +# | '__| / _ \ \__, | / _ \ \ \/ / | .__/ | | |_ | / _ \ | '_ \ +# | | | __/ __/ | | __/ > < | | | |__| | | __/ | | | | +# |_| \___| |___/ \___| /_/\_\ |_| \_____| \___| |_| |_| +*/ +int regexpGen (char *regexp, unsigned int buflen, long min, long max, int flottant) +{ + char *decimalPart = ""; + char *boundDecimalPart = ""; + char locBuf [8192] = "(?:"; + + + if (flottant) { + decimalPart = "(?:\\.\\d+)?"; + boundDecimalPart = "(?:\\.0+)?"; + } + + if (min > max) { + int nmin = max; + max = min; + min = nmin; + } + + if (min == max) { + AddLocBuf ("%ld%s", min, decimalPart); + } else if (min < 0) { + if (max < 0) { + // reg = '\-(?:' . strictPosRegexpGen (-max, -min, decimalPart, boundDecimalPart). ')'; + AddLocBuf ("\\-(?:"); + if (strictPosRegexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), -min, -max, decimalPart, + boundDecimalPart) == fail) return fail; + AddLocBuf (")"); + } else if (max == 0) { + AddLocBuf ("(?:0%s)|(?:-0%s)|-(?:", boundDecimalPart, decimalPart); + if (strictPosRegexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), 1, -min, decimalPart, + boundDecimalPart)== fail) return fail; + AddLocBuf (")"); + } else { + //reg ='(?:' . regexpGen (min, 0,withDecimal) . '|' . regexpGen (0, max, withDecimal). ')' ; + AddLocBuf ("(?:"); + if (regexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), min, 0, flottant)== fail) return fail; + AddLocBuf ("|"); + if (regexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), 0, max, flottant)== fail) return fail; + AddLocBuf (")"); + } + } else if (min == 0) { + // reg = "(?:0{decimalPart})|" . strictPosRegexpGen (1, max, decimalPart,boundDecimalPart) ; + AddLocBuf ("(?:0%s)|",decimalPart); + if (strictPosRegexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), 1, max, decimalPart, + boundDecimalPart)== fail) return fail; + } else { + if (strictPosRegexpGen (EndLocBuf, sizeof (locBuf)-strlen(locBuf), min, max, decimalPart, + boundDecimalPart)== fail) return fail; + } + + AddLocBuf (")(?![\\d.])"); + CHECK_AND_RETURN (regexp); +} + +/* +# _ _ _ _____ +# | | (_) | | | __ \ +# ___ | |_ _ __ _ ___ | |_ | |__) | ___ ___ +# / __| | __| | '__| | | / __| | __| | ___/ / _ \ / __| +# \__ \ \ |_ | | | | | (__ \ |_ | | | (_) | \__ \ +# |___/ \__| |_| |_| \___| \__| |_| \___/ |___/ +# _____ __ _ _ __ _____ +# | __ \ / _` | | '_ \ / ____| +# | |__) | ___ | (_| | ___ __ __ | |_) | | | __ ___ _ __ +# | _ / / _ \ \__, | / _ \ \ \/ / | .__/ | | |_ | / _ \ | '_ \ +# | | \ \ | __/ __/ | | __/ > < | | | |__| | | __/ | | | | +# |_| \_\ \___| |___/ \___| /_/\_\ |_| \_____| \___| |_| |_| +*/ +static bool strictPosRegexpGen (char *regexp, unsigned int buflen, long min, long max, const char* decimalPart, + const char* boundDecimalPart) +{ + const int maxSubReg=64; + const int digitRegSize=128; + char regList[maxSubReg][digitRegSize]; + char locBuf[maxSubReg*digitRegSize] ; + int regIndex=0,i; + int nbRank; + char maxAsString[32], minAsString[32]; + NextMax nMax; + + + if ((min <= 0) || (max <= 0)) return Perr ("min or max <= 0"); + if (min == max) { + sprintf (EndLocBuf, "%ld", max); + } else { + + max--; + + nbRank = strlen (ltoa (maxAsString, sizeof (maxAsString), max)); + do { + nMax = nextMax (ltoa (minAsString, sizeof (minAsString), min), + ltoa (maxAsString, sizeof (maxAsString), max)); + if (genAtRank (regList[regIndex++], digitRegSize, minAsString, + ltoa (maxAsString, sizeof (maxAsString), + nMax.max), nMax.rank) == fail) return fail; + if (regIndex == maxSubReg) return Perr ("regIndex == maxSubReg"); + min = nMax.max +1; + } while (nMax.max != max); + + locBuf[0] = 0; + for (i=0; i < \ |_ | | | | | (_| | > < +# |_| |_| \___| /_/\_\ \__| |_| |_| \__,_| /_/\_\ +*/ +static NextMax nextMax (const char *min, const char *max) +{ + NextMax nextMax ={0,0}; + char revMin[32], revMax[32]; + int nbDigitsMin, nbDigitsMax; + int rankRev=0, rankForw, rank=0, i; + int currMax; + + nbDigitsMin = strlen (min); + nbDigitsMax = strlen (max); + + for (i=nbDigitsMin-1; i >= 0; i--) { + revMin[nbDigitsMin-i-1]= min[i]; + // printf ("DBG> nextMax revMin[%d]= %c\n", nbDigitsMin-i-1, min[i]); + } + for (i=nbDigitsMax-nbDigitsMin; i >= 0; i--) { + revMin[nbDigitsMax-i]= '0'; + // printf ("DBG> nextMax revMin[%d]= %c\n", nbDigitsMax-i, '0'); + } + + for (i=nbDigitsMax-1; i >= 0; i--) { + revMax[nbDigitsMax-i-1]= max[i]; + } + revMin[nbDigitsMax] = revMax[nbDigitsMax] = 0; + rankForw = nbDigitsMax -1; + + // printf ("DBG> nextMax rev(%s)=%s rev(%s)=%s rankForw=%d\n", min, revMin, max, revMax, rankForw); + + // en partant des unitées (digit de poids faible), premier digit de min != 0 + while ((revMin[rankRev] == '0') && (rankRev < nbDigitsMax)) rankRev++; + // en partant du digit de poids fort, premier digit de max != du même digit de revMin + while ((revMin[rankForw] == revMax[rankForw]) && rankForw > 0) rankForw--; + + if (rankForw <= rankRev) { + rank = rankForw; + revMin[rankForw]= revMax[rankForw] - (rankForw ? 1 : 0); + for (i=0; i currMax) nextMax.max = currMax; + + // printf ("DBG> nextMax ('%s', '%s') = %d@%d\n", min, max, nextMax.max, nextMax.rank); + return (nextMax); +} + + +/* +# __ _ ____ _ +# / _` | / __ \ | | +# | (_| | ___ _ __ / / _` | _ __ __ _ _ __ | | _ +# \__, | / _ \ | '_ \ | | (_| | | '__| / _` | | '_ \ | |/ / +# __/ | | __/ | | | | \ \__,_| | | | (_| | | | | | | < +# |___/ \___| |_| |_| \____/ |_| \__,_| |_| |_| |_|\_\ +*/ +static bool genAtRank (char *regexp, unsigned int buflen, const char *min, const char *max, int rank) +{ + char locBuf [512]; + + if (genPreRank (locBuf, sizeof (locBuf), min, max, rank) == fail) return (fail); + if (genRank (EndLocBuf, sizeof (locBuf)-strlen(locBuf), min, max, rank) == fail) return (fail); + if (genPostRank (EndLocBuf, sizeof (locBuf)-strlen(locBuf), rank) == fail) return (fail); + + + CHECK_AND_RETURN (regexp); +} + +/* +# __ _ _____ _____ _ +# / _` | | __ \ | __ \ | | +# | (_| | ___ _ __ | |__) | _ __ ___ | |__) | __ _ _ __ | | _ +# \__, | / _ \ | '_ \ | ___/ | '__| / _ \ | _ / / _` | | '_ \ | |/ / +# __/ | | __/ | | | | | | | | | __/ | | \ \ | (_| | | | | | | < +# |___/ \___| |_| |_| |_| |_| \___| |_| \_\ \__,_| |_| |_| |_|\_\ +*/ +static bool genPreRank (char *preRank, unsigned int buflen, const char *min, const char *max, int rank) +{ + char locBuf [512], locBufMax[512]; + const char *lmin, *lmax; + int i=0, j=0; + + while (min[i] == '0') i++; + while (max[j] == '0') j++; + + lmin = &(min[i]); + lmax = &(max[j]); + + // printf ("DBG> genPreRank (lmin='%s'[%d], lmax='%s'[%d], rank=%d\n", lmin, (int) strlen (lmin), lmax, + // (int) strlen (lmax), rank); + + if (substr (locBuf, sizeof (locBuf), lmin, 0, strlen (lmin) - rank) == fail) return fail; + if (substr (locBufMax, sizeof (locBufMax), lmax, 0, strlen (lmax) - rank) == fail) return fail; + + if (strncmp (locBuf, locBufMax, minint (sizeof (locBuf), sizeof (locBufMax))) != 0) + return Perr ("min=%s[%s] and max=%s[%s] should be invariants at rank %d", locBuf, min, locBufMax, max, rank); + + // printf ("DBG> genPreRank ('%s', '%s', %d) = '%s'\n", min, max, rank, locBuf); + + CHECK_AND_RETURN (preRank); +} + + +/* +# __ _ _____ _ +# / _` | | __ \ | | +# | (_| | ___ _ __ | |__) | __ _ _ __ | | _ +# \__, | / _ \ | '_ \ | _ / / _` | | '_ \ | |/ / +# __/ | | __/ | | | | | | \ \ | (_| | | | | | | < +# |___/ \___| |_| |_| |_| \_\ \__,_| |_| |_| |_|\_\ +*/ +static bool genRank (char *outRank, unsigned int buflen, const char *min, const char *max, int rank) +{ + char locBuf [512]; + + char a,b,lmin,lmax; + a = min[strlen(min)-rank]; + b = max[strlen(max)-rank]; + + lmin = (char) minint (a,b); + lmax = (char) maxint (a,b); + + if ((lmin == '0') && (lmax == '9')) { + strcpy (locBuf, "\\d"); + } else if (lmin == lmax) { + locBuf[0] = lmin; + locBuf[1] = 0; + } else if (lmax == (lmin+1)) { + sprintf (locBuf, "[%c%c]", lmin, lmax); + } else { + sprintf (locBuf, "[%c-%c]", lmin, lmax); + } + + CHECK_AND_RETURN (outRank); +} + +/* +# __ _ _____ _ _____ +# / _` | | __ \ | | | __ \ +# | (_| | ___ _ __ | |__) | ___ ___ | |_ | |__) | __ _ _ __ +# \__, | / _ \ | '_ \ | ___/ / _ \ / __| | __| | _ / / _` | | '_ \ +# __/ | | __/ | | | | | | | (_) | \__ \ \ |_ | | \ \ | (_| | | | | | +# |___/ \___| |_| |_| |_| \___/ |___/ \__| |_| \_\ \__,_| |_| |_| +*/ +static bool genPostRank (char *postRank, unsigned int buflen, int rank) +{ + char locBuf [512]; + + if (rank <= 1) { + strcpy (locBuf, ""); + } else if (rank == 2) { + sprintf (locBuf, "\\d"); + } else { + sprintf (locBuf, "\\d{%d}", rank -1); + } + + CHECK_AND_RETURN (postRank); +} + +/* +# _ _ +# | | | | +# ___ _ _ | |__ ___ | |_ _ __ +# / __| | | | | | '_ \ / __| | __| | '__| +# \__ \ | |_| | | |_) | \__ \ \ |_ | | +# |___/ \__,_| |_.__/ |___/ \__| |_| +*/ +static bool substr (char *substring, unsigned int buflen, const char* expr, int pos, int len) +{ + char locBuf [512]; + int i, j=0; + + len = maxint (0, minint (len, strlen (expr) - pos)); + for (i=pos; i<(pos+len); i++) { + locBuf[j++]= expr[i]; + } + locBuf[j] = 0; + + // printf ("DBG> substr ('%s', %d, %d) = '%s'\n", expr, pos, len, locBuf); + CHECK_AND_RETURN (substring); +} + +/* +# +# +# _ __ ___ __ __ ___ _ __ ___ ___ +# | '__| / _ \ \ \ / / / _ \ | '__| / __| / _ \ +# | | | __/ \ V / | __/ | | \__ \ | __/ +# |_| \___| \_/ \___| |_| |___/ \___| +*/ +static char* reverse (char *string) +{ + char *locBuf ; + int i; + int len = strlen (string); + + locBuf = malloc (len+1); + for (i=len-1; i >= 0; i--) { + locBuf[len-i-1]= string[i]; + //printf ("DBG> reverse locBuf[%d]= %c\n",len- i-1, string[i]); + } + locBuf [len] = 0; + + // printf ("DBG> reverse '%s' = '%s'\n", string, locBuf); + strcpy (string, locBuf); + free (locBuf); + return (string); +} + +static char* ltoa (char *string, unsigned int buflen, long n) +{ + snprintf (string, buflen, "%ld", n); + return (string); +} + + +/* +# _ __ +# | '_ \ +# | |_) | ___ _ __ _ __ +# | .__/ / _ \ | '__| | '__| +# | | | __/ | | | | +# |_| \___| |_| |_| +*/ +static bool perr (const char* func, const char *fmt, ...) +{ + char err[4096], buffer[2048]; + va_list args; + va_start( args, fmt ); + vsprintf( buffer, fmt, args ); + va_end( args ); + + + sprintf (err, "Erreur %s @ %s\n", buffer, func); + fprintf (stderr, err); + return (fail); +} diff --git a/src/intervalRegexp.h b/src/intervalRegexp.h new file mode 100644 index 0000000..7994bef --- /dev/null +++ b/src/intervalRegexp.h @@ -0,0 +1,14 @@ +#ifndef INTERVALREGEXP_H +#define INTERVALREGEXP_H + +#ifdef __cplusplus +extern "C" { +#endif + +int regexpGen (char *regexp, unsigned int buflen, long min, long max, int flottant); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ivy.c b/src/ivy.c index e7866a5..f9d5f23 100644 --- a/src/ivy.c +++ b/src/ivy.c @@ -30,6 +30,7 @@ #include +#include "intervalRegexp.h" #include "ivychannel.h" #include "ivysocket.h" #include "list.h" @@ -136,6 +137,7 @@ static MsgRcvPtr msg_recv = 0; static IvyClientPtr clients = 0; static const char *ready_message = 0; +static void substituteInterval (IvyBuffer *src); /* * function like strok but do not eat consecutive separator @@ -753,6 +755,8 @@ IvyBindMsg (MsgCallback callback, void *user_data, const char *fmt_regex, ... ) make_message( &buffer, fmt_regex, ap ); va_end (ap ); + substituteInterval (&buffer); + /* add Msg to the query list */ IVY_LIST_ADD_START( msg_recv, msg ) msg->id = recv_id++; @@ -907,3 +911,44 @@ char **IvyGetApplicationMessages( IvyClientPtr app ) } return messagelist; } + +static void substituteInterval (IvyBuffer *src) +{ + // pas de traitement couteux s'il n'y a rien à interpoler + if (strstr (src->data, "(?I") == NULL) { + return; + } else { + IvyBuffer dst = {NULL, 0, 0}; + dst.size = 8192; + dst.data = malloc (dst.size); + + char *curPos = src->data; + char *itvPos; + while ((itvPos = strstr (curPos, "(?I")) != NULL) { + // copie depuis la position courante jusqu'à l'intervalle + int lenCp, min,max; + char withDecimal; + lenCp = itvPos-curPos; + memcpy (&(dst.data[dst.offset]), curPos, lenCp); + curPos=itvPos; + dst.offset += lenCp; + + // extraction des paramètres de l'intervalle + sscanf (itvPos, "(?I%d#%d%c", &min, &max, &withDecimal); + + // printf ("DBG> substituteInterval min=%d max=%d withDecimal=%d\n", + // min, max, (withDecimal != 'i')); + + // generation et copie de l'intervalle + regexpGen (&(dst.data[dst.offset]), dst.size-dst.offset, min, max, (withDecimal != 'i')); + dst.offset = strlen (dst.data); + + // consommation des caractères décrivant intervalle dans la chaine source + curPos = strstr (curPos, ")"); + curPos++; + } + strncat (dst.data, curPos, dst.size-dst.offset); + free (src->data); + src->data = dst.data; + } +} -- cgit v1.1