diff --git a/bricksync.c b/bricksync.c index e906aac..d8abe5f 100644 --- a/bricksync.c +++ b/bricksync.c @@ -260,6 +260,7 @@ bsContext *bsInit( char *statepath, int *retstateloaded ) context->priceguideflags = BSX_PRICEGUIDE_FLAGS_BRICKSTOCK; context->priceguidecachetime = BS_PRICEGUIDE_CACHETIME_DEFAULT; context->retainemptylotsflag = 0; + context->syncsalerates = 0; context->checkmessageflag = 0; context->curtime = time( 0 ); context->messagetime = 0; diff --git a/bricksync.conf.txt b/bricksync.conf.txt index 7b498c3..9e2f718 100644 --- a/bricksync.conf.txt +++ b/bricksync.conf.txt @@ -47,6 +47,9 @@ retainemptylots = 0; // Set to non-zero to reuse existing and empty BrickOwl lots with matching external_id/LotIDs brickowl.reuseempty = 0; +// Set to non-zero to sync sales rates from BrickLink to BrickOwl +syncsalerates = 0; + // Set to zero if you don't want to check for new versions of BrickSync or any broadcast message checkmessage = 1; diff --git a/bricksync.h b/bricksync.h index 77460e4..c8eda8b 100644 --- a/bricksync.h +++ b/bricksync.h @@ -307,6 +307,7 @@ typedef struct /* User options */ int retainemptylotsflag; + int syncsalerates; int checkmessageflag; #if BS_ENABLE_LIMITS diff --git a/bricksyncconf.c b/bricksyncconf.c index 8ac0cc1..40d19fc 100644 --- a/bricksyncconf.c +++ b/bricksyncconf.c @@ -455,6 +455,12 @@ static int bsConfParse( bsContext *context, bsConfParser *parser ) goto error; context->retainemptylotsflag = (int)readint; } + else if( ccStrMatchSeq( "syncsalerates", tokenstring, token->length ) ) + { + if( !( bsConfReadInteger( context, parser, &readint ) ) ) + goto error; + context->syncsalerates = (int)readint; + } else if( ccStrMatchSeq( "checkmessage", tokenstring, token->length ) ) { if( !( bsConfReadInteger( context, parser, &readint ) ) ) diff --git a/bsapplydiff.c b/bsapplydiff.c index 45cd9f9..35b1cea 100644 --- a/bsapplydiff.c +++ b/bsapplydiff.c @@ -833,6 +833,8 @@ static void bsBrickOwlApplyDiffUpdate( bsContext *context, bsxItem *item, int it ccGrowthPrintf( &postgrowth, "&bulk_qty=%d", ( item->bulk > 0 ? item->bulk : 1 ) ); if( item->flags & BSX_ITEM_XFLAGS_UPDATE_MYCOST ) ccGrowthPrintf( &postgrowth, "&my_cost=%.3f", item->mycost ); + if( item->flags & BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT ) + ccGrowthPrintf( &postgrowth, "&sale_percent=%d", item->sale ); if( item->flags & BSX_ITEM_XFLAGS_UPDATE_USEDGRADE ) { conditionstring = 0; @@ -881,6 +883,8 @@ static void bsBrickOwlApplyDiffUpdate( bsContext *context, bsxItem *item, int it ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "; Set bulk to %d", item->bulk ); if( item->flags & BSX_ITEM_XFLAGS_UPDATE_MYCOST ) ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "; Set mycost to %.3f", item->mycost ); + if( item->flags & BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT ) + ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "; Set sale_percent to %d", item->sale ); if( item->flags & BSX_ITEM_XFLAGS_UPDATE_USEDGRADE ) ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "; Set usedgrade to %c", item->usedgrade ); if( item->flags & BSX_ITEM_XFLAGS_UPDATE_TIERPRICES ) diff --git a/bssync.c b/bssync.c index ae3cff2..11b14a8 100644 --- a/bssync.c +++ b/bssync.c @@ -213,6 +213,11 @@ void bsSyncAddDeltaItem( bsContext *context, bsxInventory *deltainv, bsxItem *st updateflags |= BSX_ITEM_XFLAGS_UPDATE_TIERPRICES; ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "LOG: Set TierPrices from [[%d,%.3f],[%d,%.3f],[%d,%.3f]] to [[%d,%.3f],[%d,%.3f],[%d,%.3f]]%s\n", item->tq1, item->tp1, item->tq2, item->tp2, item->tq3, item->tp3, stockitem->tq1, stockitem->tp1, stockitem->tq2, stockitem->tp2, stockitem->tq3, stockitem->tp3, itemstringbuffer ); } + if ( ( deltamode == BS_SYNC_DELTA_MODE_BRICKOWL ) && ( stockitem->sale != item->sale ) && ( context->syncsalerates ) ) + { + updateflags |= BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT; + ioPrintf( &context->output, IO_MODEBIT_LOGONLY | IO_MODEBIT_NODATE, "LOG: Set Sale Percent from %d to %d%s\n", item->sale, stockitem->sale, itemstringbuffer ); + } /* TODO: Remove this once we made sure we are properly importing OwlLotIDs everywhere! */ @@ -241,6 +246,8 @@ void bsSyncAddDeltaItem( bsContext *context, bsxInventory *deltainv, bsxItem *st deltaitem->boid = item->boid; deltaitem->bolotid = item->bolotid; deltaitem->quantity = stockitem->quantity - item->quantity; + if (updateflags & BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT) + deltaitem->sale = stockitem->sale; deltaitem->flags |= BSX_ITEM_XFLAGS_TO_UPDATE | updateflags; /* Increment counters */ if( deltaitem->quantity > 0 ) @@ -257,7 +264,7 @@ void bsSyncAddDeltaItem( bsContext *context, bsxInventory *deltainv, bsxItem *st stats->match_lotcount++; /* Track updatedata updates */ - if( updateflags & ( BSX_ITEM_XFLAGS_UPDATE_PRICE | BSX_ITEM_XFLAGS_UPDATE_COMMENTS | BSX_ITEM_XFLAGS_UPDATE_REMARKS | BSX_ITEM_XFLAGS_UPDATE_BULK | BSX_ITEM_XFLAGS_UPDATE_MYCOST | BSX_ITEM_XFLAGS_UPDATE_USEDGRADE | BSX_ITEM_XFLAGS_UPDATE_TIERPRICES ) ) + if( updateflags & ( BSX_ITEM_XFLAGS_UPDATE_PRICE | BSX_ITEM_XFLAGS_UPDATE_COMMENTS | BSX_ITEM_XFLAGS_UPDATE_REMARKS | BSX_ITEM_XFLAGS_UPDATE_BULK | BSX_ITEM_XFLAGS_UPDATE_MYCOST | BSX_ITEM_XFLAGS_UPDATE_USEDGRADE | BSX_ITEM_XFLAGS_UPDATE_TIERPRICES | BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT ) ) { stats->updatedata_partcount += stockitem->quantity; stats->updatedata_lotcount++; diff --git a/bsx.c b/bsx.c index da6938f..1bbd4d2 100644 --- a/bsx.c +++ b/bsx.c @@ -2326,13 +2326,17 @@ void bsxSetItemColorName( bsxItem *item, char *colorname, int len ) void bsxSetItemComments( bsxItem *item, char *comments, int len ) { - bsxSetItemString( item, offsetof(bsxItem,comments), comments, len, BSX_ITEM_FLAGS_ALLOC_COMMENTS ); + char *trimmed_comments = ccStrTrimWhitespace(comments); + bsxSetItemString( item, offsetof(bsxItem,comments), trimmed_comments, -1, BSX_ITEM_FLAGS_ALLOC_COMMENTS ); + free(trimmed_comments); return; } void bsxSetItemRemarks( bsxItem *item, char *remarks, int len ) { - bsxSetItemString( item, offsetof(bsxItem,remarks), remarks, len, BSX_ITEM_FLAGS_ALLOC_REMARKS ); + char *trimmed_remarks = ccStrTrimWhitespace(remarks); + bsxSetItemString( item, offsetof(bsxItem,remarks), trimmed_remarks, -1, BSX_ITEM_FLAGS_ALLOC_REMARKS ); + free(trimmed_remarks); return; } diff --git a/bsx.h b/bsx.h index 2ceb03a..a53700f 100644 --- a/bsx.h +++ b/bsx.h @@ -132,9 +132,10 @@ typedef struct #define BSX_ITEM_XFLAGS_UPDATE_USEDGRADE (0x800000) #define BSX_ITEM_XFLAGS_UPDATE_TIERPRICES (0x1000000) #define BSX_ITEM_XFLAGS_FETCH_PRICE_GUIDE (0x2000000) +#define BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT (0x4000000) /* Mask of all update flags */ -#define BSX_ITEM_XFLAGS_UPDATEMASK (BSX_ITEM_XFLAGS_UPDATE_QUANTITY|BSX_ITEM_XFLAGS_UPDATE_COMMENTS|BSX_ITEM_XFLAGS_UPDATE_REMARKS|BSX_ITEM_XFLAGS_UPDATE_PRICE|BSX_ITEM_XFLAGS_UPDATE_BULK|BSX_ITEM_XFLAGS_UPDATE_STOCKROOM|BSX_ITEM_XFLAGS_UPDATE_MYCOST|BSX_ITEM_XFLAGS_UPDATE_USEDGRADE|BSX_ITEM_XFLAGS_UPDATE_TIERPRICES) +#define BSX_ITEM_XFLAGS_UPDATEMASK (BSX_ITEM_XFLAGS_UPDATE_QUANTITY|BSX_ITEM_XFLAGS_UPDATE_COMMENTS|BSX_ITEM_XFLAGS_UPDATE_REMARKS|BSX_ITEM_XFLAGS_UPDATE_PRICE|BSX_ITEM_XFLAGS_UPDATE_BULK|BSX_ITEM_XFLAGS_UPDATE_STOCKROOM|BSX_ITEM_XFLAGS_UPDATE_MYCOST|BSX_ITEM_XFLAGS_UPDATE_USEDGRADE|BSX_ITEM_XFLAGS_UPDATE_TIERPRICES|BSX_ITEM_XFLAGS_UPDATE_SALE_PERCENT) #define BSX_ITEM_STOCKFLAGS_RETAIN (0x1) #define BSX_ITEM_STOCKFLAGS_STOCKROOM (0x2) diff --git a/ccstr.c b/ccstr.c index 789934b..bfec79d 100644 --- a/ccstr.c +++ b/ccstr.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "cc.h" #include "ccstr.h" @@ -41,7 +42,39 @@ //// +char *ccStrTrimWhitespace( char *str ) +{ + char *start = str; + char *end; + char *new_str; + size_t len; + + if( str == NULL ) + return NULL; + + // Trim leading space + while( isspace( (unsigned char)*start ) ) + start++; + + if( *start == 0 ) // All spaces? + return ccStrDup( start ); + + // Trim trailing space + end = start + strlen(start) - 1; + while( end > start && isspace( (unsigned char)*end ) ) + end--; + // Allocate and copy the trimmed string + len = (end - start) + 1; + new_str = (char *)malloc( len + 1 ); + if( new_str == NULL ) + return NULL; // Allocation failed + + memcpy( new_str, start, len ); + new_str[len] = 0; + + return new_str; +} #define CC_CHAR_IS_CONTROL(c) ((c)<' ') #define CC_CHAR_IS_DELIMITER(c) ((c)<=' ') @@ -800,9 +833,9 @@ char *ccStrDup( const char *str ) if( !( str ) ) return 0; len = strlen( str ); - if( !( len ) ) - return 0; newstr = malloc( ( len + 1 ) * sizeof(char) ); + if( !newstr ) + return 0; memcpy( newstr, str, len + 1 ); return newstr; } diff --git a/ccstr.h b/ccstr.h index 77b8070..0edc28d 100644 --- a/ccstr.h +++ b/ccstr.h @@ -173,6 +173,7 @@ int ccSeqParseDouble( char *seq, int seqlength, double *retdouble ); int ccStrParseHex( char *str, int hexchars ); +char *ccStrTrimWhitespace( char *str ); ////