~tokyocabinet/tokyocabinet/c99-posix-trunk

« back to all changes in this revision

Viewing changes to example/tctchat.c

  • Committer: Baptiste Lepilleur
  • Date: 2009-07-30 19:32:21 UTC
  • Revision ID: blep@users.sourceforge.net-20090730193221-ap333ajj7u5ccqz9
Tags: tokyocabinet-1.4.25
releaseĀ 1.4.25

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    query[clen] = '\0';
34
34
  }
35
35
  if(query) tcwwwformdecode(query, params);
 
36
  const char *type = tcmapget4(params, "type", "");
36
37
  const char *author = tcmapget4(params, "author", "");
37
38
  const char *text = tcmapget4(params, "text", "");
38
39
  int page = tcatoi(tcmapget4(params, "page", "1"));
39
40
  const char *dbpath = tctmplconf(tmpl, "dbpath");
40
41
  if(!dbpath) dbpath = "casket.tct";
41
42
  TCLIST *msgs = tcmpoollistnew(mpool);
42
 
  TCTDB *tdb = tctdbnew();
43
 
  tcmpoolpush(mpool, tdb, (void (*)(void *))tctdbdel);
 
43
  TCTDB *tdb = tcmpoolpush(mpool, tctdbnew(), (void (*)(void *))tctdbdel);
44
44
  rp = getenv("REQUEST_METHOD");
45
45
  if(rp && !strcmp(rp, "POST") && *author != '\0' && *text != '\0'){
46
46
    if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT))
59
59
      tclistprintf(msgs, "The database could not be opened (%s).", tctdberrmsg(tctdbecode(tdb)));
60
60
  }
61
61
  TCLIST *logs = tcmpoollistnew(mpool);
62
 
  TDBQRY *qry = tctdbqrynew(tdb);
63
 
  tcmpoolpush(mpool, qry, (void (*)(void *))tctdbqrydel);
 
62
  TDBQRY *qry = tcmpoolpush(mpool, tctdbqrynew(tdb), (void (*)(void *))tctdbqrydel);
64
63
  tctdbqrysetorder(qry, "", TDBQONUMDESC);
65
64
  tctdbqrysetlimit(qry, 16, page > 0 ? (page - 1) * 16 : 0);
66
 
  TCLIST *res = tctdbqrysearch(qry);
67
 
  tcmpoolpushlist(mpool, res);
 
65
  TCLIST *res = tcmpoolpushlist(mpool, tctdbqrysearch(qry));
68
66
  int rnum = tclistnum(res);
69
67
  for(int i = rnum - 1; i >= 0; i--){
70
68
    int pksiz;
71
69
    const char *pkbuf = tclistval(res, i, &pksiz);
72
 
    TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
 
70
    TCMAP *cols = tcmpoolpushmap(mpool, tctdbget(tdb, pkbuf, pksiz));
73
71
    if(cols){
74
 
      tcmpoolpushmap(mpool, cols);
75
72
      tcmapprintf(cols, "pk", "%s", pkbuf);
76
73
      char date[64];
77
74
      tcdatestrwww(tcatoi(pkbuf) / 1000, INT_MAX, date);
87
84
  tcmapprintf(vars, "author", "%s", author);
88
85
  if(page > 1) tcmapprintf(vars, "prev", "%d", page - 1);
89
86
  if(tctdbrnum(tdb) > page * 16) tcmapprintf(vars, "next", "%d", page + 1);
90
 
  char *str = tctmpldump(tmpl, vars);
91
 
  printf("Content-Type: text/html; charset=UTF-8\r\n");
 
87
  char *str = tcmpoolpushptr(mpool, tctmpldump(tmpl, vars));
 
88
  printf("Content-Type: %s\r\n", !strcmp(type, "xml") ? "application/xml" :
 
89
         !strcmp(type, "xhtml") ? "application/xhtml+xml" : "text/html; charset=UTF-8");
 
90
  printf("Cache-Control: no-cache\r\n");
92
91
  printf("\r\n");
93
92
  fwrite(str, 1, strlen(str), stdout);
94
 
  tcfree(str);
95
93
}