File: bidgui.cpp

package info (click to toggle)
bidwatcher 1.3.17-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 936 kB
  • ctags: 717
  • sloc: cpp: 6,505; sh: 2,970; makefile: 62
file content (399 lines) | stat: -rw-r--r-- 9,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * part of bidwatcher
 * Author: Jan Starzynski
 * use of this code is restricted to the terms
 * of the GNU GPL, which should have been included in this
 * distribution. If not, see www.gnu.org/copyleft/gpl.html.
 * Here is the short version:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * $Id: bidgui.cpp,v 1.1.2.12 2004/09/25 22:34:37 kevindication Exp $
 *
 * View and Control for Bidgroups
 */

#include "config.h"

#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef HAVE_STL
# include <cstdarg>
# include <cstdlib>
# include <cstring>
# include <map>
#else
# include <stdarg.h>
# include <stdlib.h>
# include <string.h>
#endif

#include "bidgui.h"
#include "portability.h"

using namespace std;

// Replaces or removes ascii characters > 127. On some system they are not properly shown.
void removeAscii127(char *info)
{
  unsigned char *p = (unsigned char*)info;
  while(*p) {
    switch(*p) {
    case (unsigned char)'':
      *p = 'a';
      break;

    case (unsigned char)'':
      *p = 'o';
      break;

    case (unsigned char)'':
      *p = 'u';
      break;

    case (unsigned char)'':
      *p = 'A';
      break;

    case (unsigned char)'':
      *p = 'O';
      break;

    case (unsigned char)'':
      *p = 'u';
      break;

    case (unsigned char)'':
      *p = 's';
      break;
    }

    if(*p > 127) *p = ' ';

    p++;
  }
}

BidGroup *getBidGroup()
{
  static BidGroup bid_group;
  return &bid_group;
}

static bool hasBid(struct auctioninfo *auc)
{
  if(auc->isSnipe)         return true;
  if(auc->snipeAmount > 0) return true;
  if(auc->myBidAmount > 0) return true;
  return false;
}

static void copySnipe(struct auctioninfo *dst, const struct auctioninfo *src)
{
  float new_price = 0;

  if(strcmp(src->currency, dst->currency) == 0) {
    new_price = src->myBidAmount;
    if(strcmp(src->currency, src->ShippingCur[0]) == 0 &&
       strcmp(src->currency, dst->ShippingCur[0]) == 0) {
      new_price += src->Shipping[0] - dst->Shipping[0];
    }
  }

  if(new_price < 0) new_price = 0;

  setBid(dst, new_price, src->myBidQuantity, true, false);
}

// set/change bidgroup for an item
void switchBidGroup(struct auctioninfo *auction[], int auc_num, int current_auc, int new_auc,
                    const char *subdir, int time_diff, const char *auth_id)
{
  if(current_auc < 0 || current_auc >= auc_num) return;
  if(new_auc < 0 || new_auc >= auc_num) return;
  BidGroup *group = getBidGroup();
  struct auctioninfo *cauc = auction[current_auc];
  struct auctioninfo *auc = auction[new_auc];
  BidGroup::IdType cid = cauc->ItemNumber;
  BidGroup::IdType id = auc->ItemNumber;
  int cgid = group->groupId(cid);
//  int gid = group->groupId(id);

  if(cgid >= 0) group->delItem(id);
  group->addItem(cid, id);
  group->sweepList();
  if(hasBid(cauc) && !hasBid(auc)) copySnipe(auc, cauc);
  if(!hasBid(cauc) && hasBid(auc)) copySnipe(cauc, auc);

  setBidGroupAuction(auction, auc_num, auth_id);
  UpdateList();
  WriteAucFile();
}

char *configFileName(char *file, const char *subdir, const char *fname)
{
  sprintf(file, "%s/%s/%s", getenv("HOME"), subdir, fname);
  return file;
}

char *bidGroupFile(char *file, const char *subdir)
{
  return configFileName(file, subdir, "bidgroup.cfg");
}

void writeBidGroup(const char *subdir)
{
  char file[200];
  BidGroup *group = getBidGroup();
  group->write(bidGroupFile(file, subdir));
}

void readBidGroup(const char *subdir)
{
  char file[200];
  BidGroup *group = getBidGroup();
  group->clear();
  group->read(bidGroupFile(file, subdir));
}

void bidGroupComment(char **str, struct auctioninfo *auction[], int auc)
{
  BidGroup *group = getBidGroup();
  const int gid = group->groupId(auction[auc]->ItemNumber);
  if(group->numGroup() > 0) {
    if(gid >= 0) *str = g_strdup_printf(" %*s%d%*s: %s", gid, "", gid + 1, group->numGroup() - gid - 1, "", *str);
    else         *str = g_strdup_printf(" %*s : %s", group->numGroup() - 1, "", *str);
  }
}

SetBidGroupAuction::~SetBidGroupAuction()
{
  setBidGroupAuction(auction, auc_num, auth_id);
}

// mark all items in a successful bidgroup
#if HAVE_BIDGROUP
static void findSucceededAuc(map<BidGroup::IdType, bool> &succeeded, const BidGroup::ItemList *list)
{
  if(!list) return;

  bool any_succeeded = false;
  for(BidGroup::ItemList::const_iterator it = list->begin(); it != list->end(); ++it) {
    if(succeeded[*it]) {
      any_succeeded = true;
      break;
    }
  }

  if(any_succeeded) {
    for(BidGroup::ItemList::const_iterator it = list->begin(); it != list->end(); ++it) {
      succeeded[*it] = any_succeeded;
    }
  }
}

// I won this auction.
static bool isMine(struct auctioninfo *auction, const char *auth_id)
{
  return strcmp(auction->HighBidder, auth_id) == 0 && auction->UpdTime >= auction->EndsValue;
}
#endif

void setBidGroupAuction(struct auctioninfo *auction[], int auc_num, const char *auth_id)
{
#if HAVE_BIDGROUP
  BidGroup *group = getBidGroup();
  map<BidGroup::IdType, bool> succeeded;

  for(int i = 0; i < auc_num; i++) {
    struct auctioninfo *a = auction[i];
    succeeded[a->ItemNumber] = isMine(a, auth_id);
  }

  for(int i = 0; i < group->numGroup(); i++) {
    findSucceededAuc(succeeded, group->getGroup(i));
  }

  for(int i = 0; i < auc_num; i++) {
    struct auctioninfo *a = auction[i];
    if(a->isSnipe && !isMine(a, auth_id) && succeeded[a->ItemNumber]) {
      printf("clear snipe "LLU_FMT"\n", a->ItemNumber);
      ClearSnipe(i);
    }
  }
#endif
}

void delBidGroupItem(struct auctioninfo *auction[], int auc)
{
  BidGroup *group = getBidGroup();
  group->delItem(auction[auc]->ItemNumber);
  group->sweepList();
}

// save something in a file defined by format
void savePage(const char *page, const char *format, ...)
{
  va_list va;
  va_start(va, format);
  char fname[4096];
  vsprintf(fname, format, va);
  va_end(va);
  FILE *fp = fopen(fname, "w");
  if(fp) {
    fprintf(fp, "%s", page);
    fclose(fp);
  }
}

// set up a bid or snipe
void setBid(struct auctioninfo *auction, float bid, int quantity, bool snipe, bool fire_bid)
{
  if(!auction) return;

  auction->isSnipe = snipe;
  auction->isPreBid = snipe;
  auction->myBidAmount = bid;
  auction->myBidQuantity = quantity;
  memset(auction->snipeKey, 0, sizeof(auction->snipeKey));

  if(snipe) {
    auction->snipeAmount = bid;
    auction->snipeQty = quantity;
  } else {
    auction->snipeAmount = 0;
    auction->snipeQty = 0;
    auction->getkey(bid, quantity);
    if(fire_bid && auction->snipeKey[0]) auction->bid(FALSE);
  }

  UpdateList();
}

// set up a bid or snipe reading amount + quantity from a file
static struct auctioninfo *setBid(struct auctioninfo *auction, FILE *fp, bool snipe, bool fire_bid)
{
  if(!auction) return auction;
  float bid;
  int qty;
  if(fscanf(fp, "%f%d", &bid, &qty) != 2) return auction;
  setBid(auction, bid, qty, snipe, fire_bid);
  fprintf(stderr, "%s "LLU_FMT" bid: %f qty: %d\n", snipe ? "snipeing" : "bidding ", auction->ItemNumber, bid, qty);
  return auction;
}

static void lockFile(FILE *fp, bool rw, bool lock)
{
  struct flock tmp;
  tmp.l_type = lock ? rw ? F_WRLCK : F_RDLCK : F_UNLCK;
  tmp.l_whence = SEEK_SET;
  tmp.l_start = 0;
  tmp.l_len = 0;
  fcntl(fileno(fp), F_SETLKW, &tmp);
}

/*
 * Adds items from the file "~/.bidwatcher/item.add" to bidwatcher.
 * Places bids/snipes if required.
 *
 * Syntax: a continuous stream of the following command
 * "ebay-id"                            adds an item
 * "add ebay-id"                        adds an item
 * "bid ebay-id amount quantity"        bids for an item
 * "snipe ebay-id amount quantity"      snipes for an item
 *
 * Example:
 *   568880089
 *   add 568880010
 *   bid 568888888 10.51 1
 *   snipe 568888777 12.11 1
 *
 * I use this to remotely control my bidwatcher via email-filters.
 */
void addItemsFromFile(const char *subdir, volatile bool  &up_in_prog)
{
  class AutoFp {
    FILE *fp;

  public:
    AutoFp(FILE *fp, bool rw): fp(fp) {
      if(!fp) return;
      lockFile(fp, rw, true);
    }

    ~AutoFp() {
      if(!fp) return;
      fflush(fp);
      lockFile(fp, false, false);
      fclose(fp);
    }

    operator FILE*() const { return fp; }
    FILE* operator->() const { return fp; }
  };

  class AutoFlag {
    volatile bool &up_in_prog;

  public:
    AutoFlag(volatile bool  &up_in_prog): up_in_prog(up_in_prog) { up_in_prog = true; }
    ~AutoFlag() { up_in_prog = false;}
  };

  if(up_in_prog) return;
  AutoFlag a_flag(up_in_prog);

  if(interfereWithSnipe()) return;

  char file[256];
  configFileName(file, subdir, "item.add");
  AutoFp fp(fopen(file, "r+"), true);
  if(!fp) return;
  char buffer1[20];
  char buffer2[20];
  char *rd = buffer1;
  char *act = buffer2;
  const char *def_action = "add";
  strcpy(act, def_action);

  while(fscanf(fp, "%19s", rd) == 1) {
    if(interfereWithSnipe()) return;
    unsigned long long id;

    if(sscanf(rd, LLU_FMT, &id) != 1) {
      char *tmp = rd;
      rd = act;
      act = tmp;
      continue;
    }

    up_in_prog = false;
    if(strcmp(act, "add") == 0) {
      fprintf(stderr, "adding "LLU_FMT"\n", id);
      addNewItem(id);
    } else if(strcmp(act, "bid") == 0) {
      setBid(addNewItem(id), fp, false, true);
    } else if(strcmp(act, "snipe") == 0) {
      setBid(addNewItem(id), fp, true, false);
    } else {
      fprintf(stderr, "ignoring %s "LLU_FMT"\n", act, id);
    }
    up_in_prog = true;

    strcpy(act, def_action);
  }
  ftruncate(fileno(fp), 0);
}