Changeset 939


Ignore:
Timestamp:
2000-02-17T15:40:21+13:00 (24 years ago)
Author:
sjboddie
Message:

tidied up a bit - fixed a bug (gdbm file wasn't always being closed properly)

Location:
trunk/gsdl/src/recpt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/recpt/historydb.cpp

    r928 r939  
     1/**********************************************************************
     2 *
     3 * historydb.cpp --
     4 * Copyright (C) 1999  The New Zealand Digital Library Project
     5 *
     6 * A component of the Greenstone digital library software
     7 * from the New Zealand Digital Library Project at the
     8 * University of Waikato, New Zealand.
     9 *
     10 * This program is free software; you can redistribute it and/or modify
     11 * it under the terms of the GNU General Public License as published by
     12 * the Free Software Foundation; either version 2 of the License, or
     13 * (at your option) any later version.
     14 *
     15 * This program is distributed in the hope that it will be useful,
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 * GNU General Public License for more details.
     19 *
     20 * You should have received a copy of the GNU General Public License
     21 * along with this program; if not, write to the Free Software
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     23 *
     24 * $Id$
     25 *
     26 *********************************************************************/
     27
     28/*
     29  $Log$
     30  Revision 1.2  2000/02/17 02:40:21  sjboddie
     31  tidied up a bit - fixed a bug (gdbm file wasn't always being closed properly)
     32
     33
     34  */
    135
    236#include "historydb.h"
     
    1044// returns true on success (in which case historyinfo will contain
    1145// the information for this history)
    12 bool get_history_info (const text_t &userid, text_tarray &historyinfo)
    13 {
     46bool get_history_info (const text_t &userid, text_tarray &historyinfo, ostream &logout) {
     47
    1448  text_t historyfile = filename_cat(GSDL_GSDLHOME, "etc", "history.db");
    1549 
     
    2155    // get history list
    2256    text_t historyresult;
    23 
     57   
    2458    historydb.getkeydata(userid, historyresult);
    2559   
    2660    if (historyresult != "") { //  there are entries, process them
    27    
     61     
    2862      splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
    2963      result = true;
     
    3165    historydb.closedatabase();
    3266   
    33     } // opendatabase
    34     else {
    35       cerr << "couldn't open db\n";
    36      
    37     }
     67  } else {
     68    outconvertclass text_t2ascii;
     69    logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
     70  }
    3871  return result;
    39 } // get history info
     72}
    4073
    4174
    4275// returns true on success
    43 bool set_history_info (const text_t &userid, const text_t &history)
    44 {
     76bool set_history_info (const text_t &userid, const text_t &history) {
    4577
    4678  text_t historyfile = filename_cat(GSDL_GSDLHOME, "etc", "history.db");
     
    94126
    95127// deletes all a users history
    96 bool delete_all_history_info (const text_t &userid)
    97 {
     128bool delete_all_history_info (const text_t &userid) {
     129
    98130  text_t historyfile = filename_cat(GSDL_GSDLHOME, "etc", "history.db");
    99131 
     
    104136
    105137  historydb.deletekey(userid);
     138  historydb.closedatabase();
    106139  return true;
    107140 
     
    109142
    110143//deletes only the selected records
    111 bool delete_history_info(const text_t &userid, const text_t &deletemode, const text_t &selection)
    112 {
     144bool delete_history_info(const text_t &userid, const text_t &deletemode, const text_t &selection) {
     145
    113146  text_t historyfile = filename_cat(GSDL_GSDLHOME, "etc", "history.db");
    114147  bool result;
     
    211244}
    212245
    213 bool get_selection_params (text_t &data, int &start, int &stop)
    214 {
    215    text_tarray results;
     246bool get_selection_params (text_t &data, int &start, int &stop) {
     247
     248  text_tarray results;
    216249
    217250  splitchar(data.begin(), data.end(), '-', results);
     
    221254     
    222255      return true;
    223   }
    224   else {
     256
     257  } else {
    225258    // error message
    226259    return false;
    227260  }
    228  
    229261}
    230262
    231263// expand query takes a HistRecord, and expands out any hashes in
    232264// the query string for queries that aren't to be saved
    233 bool expand_query(HistRecord &record,  HistRecordmap &records)
    234 {
     265bool expand_query(HistRecord &record,  HistRecordmap &records) {
     266
    235267  text_tarray args;
    236    splitchar(record.entry.begin(), record.entry.end(), '&', args);
     268  splitchar(record.entry.begin(), record.entry.end(), '&', args);
    237269
    238270  text_tarray::iterator begin=args.begin();
     
    246278      args.erase(begin);
    247279      break;
    248     }
    249     else {
    250       begin++;
    251     }
     280    } else begin++;
    252281  }
    253282  if (query !="") {
    254 
     283   
    255284    //remove the q=
    256285    text_t::iterator front = query.begin();
     
    271300 
    272301    return true;
    273   }else return false;
     302  } else return false;
    274303}// expand query
    275304
     
    293322    // get history list
    294323    if (historydb.getkeydata(userid, historyresult)) {
    295     exists=true;
     324      exists=true;
    296325     
    297     historydb.closedatabase();
     326      historydb.closedatabase();
    298327    }
    299328  }
     
    313342
    314343  // if have got to here, have a previous history in historyresult
    315    text_tarray historyinfo;
    316    splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
    317    text_tarray::iterator histbegin = historyinfo.begin();
    318    text_tarray::iterator histend = historyinfo.end();
    319    
    320    text_tmap historymap;
    321    // put all entries into a map, so can pull out the appropriate one easily
    322    while (histbegin != histend) {
    323      text_t num;
    324      get_query_num(*histbegin, num);
    325      historymap[num]=*histbegin;
    326      histbegin++;
    327    }
    328    while(it!=end) { // while there still is a hash present
    329    text_t querynum;
    330    text_t newquery;
    331    
    332    while (begin!=end) {
    333      if (*begin==text_t('#')) {
    334        begin++;
    335        while(begin !=end && *begin>='0'&&*begin<='9') {
    336      querynum.push_back(*begin);
    337      begin++;
    338        }
     344  text_tarray historyinfo;
     345  splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
     346  text_tarray::iterator histbegin = historyinfo.begin();
     347  text_tarray::iterator histend = historyinfo.end();
     348 
     349  text_tmap historymap;
     350  // put all entries into a map, so can pull out the appropriate one easily
     351  while (histbegin != histend) {
     352    text_t num;
     353    get_query_num(*histbegin, num);
     354    historymap[num]=*histbegin;
     355    histbegin++;
     356  }
     357  while(it!=end) { // while there still is a hash present
     358    text_t querynum;
     359    text_t newquery;
     360    
     361    while (begin!=end) {
     362      if (*begin==text_t('#')) {
     363    begin++;
     364    while(begin !=end && *begin>='0'&&*begin<='9') {
     365      querynum.push_back(*begin);
     366      begin++;
     367    }
    339368        text_t oldquery = historymap[querynum];
    340        if (oldquery !="") {   // valid entry
    341      parse_saved_args(oldquery, text_t("q"), newquery);
    342      decode_cgi_arg(newquery);
    343      queryresult += newquery;
    344        }
    345        else {
    346      result=false;
    347        }
    348        querynum.clear();
     369    if (oldquery !="") {   // valid entry
     370      parse_saved_args(oldquery, "q", newquery);
     371      decode_cgi_arg(newquery);
     372      queryresult += newquery;
     373    }
     374    else {
     375      result=false;
     376    }
     377    querynum.clear();
    349378        // else do nothing (replace #x with nothing)
    350     } // if
    351     else {
    352       queryresult.push_back(*begin);
    353       begin++;
    354     }
    355    } // while begin!=end
    356 
    357    // have got to end of query string,
    358    // now go back and check for internal #
    359    query = queryresult;
    360    begin = query.begin();
    361    end = query.end();
    362    it = findchar(begin, end, '#');
    363    queryresult.clear();
    364 
    365    } // while it !=end
    366 
    367 
    368     return result;
    369    
     379      } // if
     380      else {
     381    queryresult.push_back(*begin);
     382    begin++;
     383      }
     384    } // while begin!=end
     385   
     386    // have got to end of query string,
     387    // now go back and check for internal #
     388    query = queryresult;
     389    begin = query.begin();
     390    end = query.end();
     391    it = findchar(begin, end, '#');
     392    queryresult.clear();
     393   
     394  } // while it !=end
     395 
     396 
     397  return result;
     398 
    370399} // combine query
    371400
     
    383412  bool changed=true;
    384413  while(it!=end && changed) { // while there still is a hash present
    385                              // and query has changed since last time round
     414                              // and query has changed since last time round
    386415                              // we are leaving some #X in
    387    text_t querynum;
    388    text_t newquery;
    389    changed=false;
    390    while (begin!=end) { // go through the query looking for hash
    391      if (*begin==text_t('#')) {
    392        begin++;
    393        while(begin !=end && *begin>='0'&&*begin<='9') {
    394      querynum.push_back(*begin);
    395      begin++;
    396        }
    397        if(records.count(querynum)>0) { // valid entry
    398      if (!records[querynum].save){ // referenced record to be deleted
    399        // get the q arg out of referenced query
    400        parse_saved_args(records[querynum].entry, text_t("q"), newquery);
    401        decode_cgi_arg(newquery);
    402        queryresult += newquery;
    403        changed=true;
    404        }
    405      else { // leave the #x in
     416    text_t querynum;
     417    text_t newquery;
     418    changed=false;
     419    while (begin!=end) { // go through the query looking for hash
     420      if (*begin==text_t('#')) {
     421    begin++;
     422    while(begin !=end && *begin>='0'&&*begin<='9') {
     423      querynum.push_back(*begin);
     424      begin++;
     425    }
     426    if(records.count(querynum)>0) { // valid entry
     427      if (!records[querynum].save){ // referenced record to be deleted
     428        // get the q arg out of referenced query
     429        parse_saved_args(records[querynum].entry, "q", newquery);
     430        decode_cgi_arg(newquery);
     431        queryresult += newquery;
     432        changed=true;
     433      }
     434      else { // leave the #x in
    406435       queryresult.push_back('#');
    407436       queryresult +=  querynum;
    408      }
    409        }
    410        querynum.clear();
    411        newquery.clear();
    412        // else do nothing (replace #x with nothing)
    413     } // if its a hash
    414     else {
    415       queryresult.push_back(*begin);
    416       begin++;
    417     }
    418    } // while begin!=end
    419 
    420    // have got to end of query string,
    421    // now go back and check for internal #
    422    query = queryresult;
    423    begin = query.begin();
    424    end = query.end();
    425    it = findchar(begin, end, '#');
    426    queryresult.clear();
    427 
    428    } // while it !=end
    429 
    430 
    431     return true;
    432    
     437      }
     438    }
     439    querynum.clear();
     440    newquery.clear();
     441    // else do nothing (replace #x with nothing)
     442      } // if its a hash
     443      else {
     444    queryresult.push_back(*begin);
     445    begin++;
     446      }
     447    } // while begin!=end
     448   
     449    // have got to end of query string,
     450    // now go back and check for internal #
     451    query = queryresult;
     452    begin = query.begin();
     453    end = query.end();
     454    it = findchar(begin, end, '#');
     455    queryresult.clear();
     456   
     457  } // while it !=end
     458 
     459  return true;
    433460} // combine query
    434461
    435462// retrieves the value of one of the arguments
    436 void parse_saved_args(text_t &args, text_t &key, text_t &value)
     463void parse_saved_args(text_t &args, text_t key, text_t &value)
    437464{
    438465  text_t::iterator here = args.begin();
     
    492519
    493520  while (*begin >='0'&& *begin <='9') { // get the digits
    494         querynum.push_back(*begin);
    495         begin++;
     521    querynum.push_back(*begin);
     522    begin++;
    496523  }
    497524  if (*begin != ':') {
     
    499526  }   
    500527}
     528
    501529void split_saved_query(text_t &query, text_t &querynum, text_t &numdocs, text_t &cgiargs)
    502530{
    503531  text_t::iterator begin = query.begin();
    504532  text_t::iterator end = query.end();
    505 
     533 
    506534  while (*begin >='0'&& *begin <='9') { // get the digits
    507535    querynum.push_back(*begin);
     
    513541  else begin++;
    514542  while (*begin >='0'&& *begin <='9') { // get the digits
    515         numdocs.push_back(*begin);
    516         begin++;
     543    numdocs.push_back(*begin);
     544    begin++;
    517545  }                                                                                                                         
    518546  if (*begin == '+') {  // get the + if there
     
    529557  logout << "in format user info";
    530558  text_tset metadata;
    531 
     559 
    532560  infodbclass argsinfo;
    533561  parse_saved_args(cgiargs, argsinfo);
    534 
     562 
    535563  text_t collect = argsinfo["c"];
    536564  if (collect=="") {
     
    558586      mode = "some words";
    559587    }
    560 
     588   
    561589  }
    562590  else { // advanced mode
     
    568596    }
    569597  }
    570 
     598 
    571599  text_t options;
    572600  if (argsinfo["k"]=="0") {
     
    582610    options += ", ignore word endings";
    583611  }
    584    
     612 
    585613  userinfo.clear();
    586614  userinfo = "collection ("+argsinfo["c"] + ") ";
  • trunk/gsdl/src/recpt/historydb.h

    r928 r939  
     1/**********************************************************************
     2 *
     3 * historydb.h --
     4 * Copyright (C) 1999  The New Zealand Digital Library Project
     5 *
     6 * A component of the Greenstone digital library software
     7 * from the New Zealand Digital Library Project at the
     8 * University of Waikato, New Zealand.
     9 *
     10 * This program is free software; you can redistribute it and/or modify
     11 * it under the terms of the GNU General Public License as published by
     12 * the Free Software Foundation; either version 2 of the License, or
     13 * (at your option) any later version.
     14 *
     15 * This program is distributed in the hope that it will be useful,
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 * GNU General Public License for more details.
     19 *
     20 * You should have received a copy of the GNU General Public License
     21 * along with this program; if not, write to the Free Software
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     23 *
     24 * $Id$
     25 *
     26 *********************************************************************/
    127
    228
    3 #ifndef HISTORY_H
    4 #define HISTORY_H
     29#ifndef HISTORYDB_H
     30#define HISTORYDB_H
    531
    632#include "infodbclass.h"
    733#include "text_t.h"
    834#include "recptproto.h"
    9 //#include <vector.h>
    1035#include <map.h>
    1136
     
    2651// returns true on success (in which case historyinfo will contain
    2752// the information for this history)
    28 bool get_history_info ( const text_t &userid, text_tarray &historyinfo);
     53bool get_history_info ( const text_t &userid, text_tarray &historyinfo, ostream &logout);
    2954
    3055// returns true on success
     
    5277// retrieves the value of an arg (key) from a list of cgi args
    5378// in the form a=q&b=1&...
    54 void parse_saved_args(text_t &args, text_t &key, text_t &value);
     79void parse_saved_args(text_t &args, text_t key, text_t &value);
    5580
    5681// extracts out the components of an entry in the database
Note: See TracChangeset for help on using the changeset viewer.