Blame src/keyvalue.h

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#ifndef KEYVALUE_H
Packit 8f70b4
#define KEYVALUE_H
Packit 8f70b4
Packit 8f70b4
#include "xstring.h"
Packit 8f70b4
Packit 8f70b4
class StringMangler
Packit 8f70b4
{
Packit 8f70b4
   typedef const char *(*mangle_t)(const char *);
Packit 8f70b4
   mangle_t mangle;
Packit 8f70b4
public:
Packit 8f70b4
   const char *operator()(const char *s) { return mangle?mangle(s):s; }
Packit 8f70b4
   StringMangler(mangle_t m=0) { mangle=m; }
Packit 8f70b4
};
Packit 8f70b4
Packit 8f70b4
class KeyValueDB
Packit 8f70b4
{
Packit 8f70b4
public:
Packit 8f70b4
   class Pair
Packit 8f70b4
   {
Packit 8f70b4
   public:
Packit 8f70b4
      xstring_c key;
Packit 8f70b4
      xstring_c value;
Packit 8f70b4
      Pair *next;
Packit 8f70b4
      Pair(const char *k,const char *v)
Packit 8f70b4
	 : key(k), value(v), next(0) {}
Packit 8f70b4
      virtual ~Pair() {}
Packit 8f70b4
      int KeyCompare(const char *s) const
Packit 8f70b4
	 {
Packit 8f70b4
	    return strcmp(s,key);
Packit 8f70b4
	 }
Packit 8f70b4
      void SetValue(const char *v) { value.set(v); }
Packit 8f70b4
   };
Packit 8f70b4
Packit 8f70b4
protected:
Packit 8f70b4
   void Purge(Pair **p)
Packit 8f70b4
      {
Packit 8f70b4
	 Pair *to_free=*p;
Packit 8f70b4
	 if(current==to_free)
Packit 8f70b4
	    current=to_free->next;
Packit 8f70b4
	 *p=to_free->next;
Packit 8f70b4
	 delete to_free;
Packit 8f70b4
      }
Packit 8f70b4
   Pair **LookupPair(const char *key) const;
Packit 8f70b4
   void AddPair(Pair *p)
Packit 8f70b4
      {
Packit 8f70b4
	 p->next=chain;
Packit 8f70b4
	 chain=p;
Packit 8f70b4
      }
Packit 8f70b4
   virtual Pair *NewPair(const char *id,const char *value)
Packit 8f70b4
      {
Packit 8f70b4
	 return new Pair(id,value);
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
   int Lock(int fd,int type);
Packit 8f70b4
Packit 8f70b4
   Pair *chain;
Packit 8f70b4
Packit 8f70b4
   Pair *current;
Packit 8f70b4
Packit 8f70b4
public:
Packit 8f70b4
   void Add(const char *id,const char *value);
Packit 8f70b4
   void Remove(const char *id);
Packit 8f70b4
   const char *Lookup(const char *id) const;
Packit 8f70b4
   void Empty()
Packit 8f70b4
      {
Packit 8f70b4
	 while(chain)
Packit 8f70b4
	    Purge(&chain);
Packit 8f70b4
      }
Packit 8f70b4
   int Write(int fd);
Packit 8f70b4
   int Read(int fd);
Packit 8f70b4
   void Sort();
Packit 8f70b4
   char *Format(StringMangler m=0); // returns formatted contents (malloc'ed)
Packit 8f70b4
Packit 8f70b4
   void Rewind()
Packit 8f70b4
      {
Packit 8f70b4
	 current=chain;
Packit 8f70b4
      }
Packit 8f70b4
   const char *CurrentKey() const
Packit 8f70b4
      {
Packit 8f70b4
	 if(!current)
Packit 8f70b4
	    return 0;
Packit 8f70b4
	 return current->key;
Packit 8f70b4
      }
Packit 8f70b4
   const char *CurrentValue() const
Packit 8f70b4
      {
Packit 8f70b4
	 if(!current)
Packit 8f70b4
	    return 0;
Packit 8f70b4
	 return current->value;
Packit 8f70b4
      }
Packit 8f70b4
   bool Next()
Packit 8f70b4
      {
Packit 8f70b4
	 if(current==0)
Packit 8f70b4
	    return false;
Packit 8f70b4
	 current=current->next;
Packit 8f70b4
	 return current!=0;
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
   KeyValueDB()
Packit 8f70b4
      {
Packit 8f70b4
	 chain=0;
Packit 8f70b4
	 current=0;
Packit 8f70b4
      }
Packit 8f70b4
   virtual ~KeyValueDB()
Packit 8f70b4
      {
Packit 8f70b4
	 Empty();
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
   static int KeyCompare(const Pair *a,const Pair *b)
Packit 8f70b4
      {
Packit 8f70b4
	 return strcmp(a->key,b->key);
Packit 8f70b4
      }
Packit 8f70b4
   static int VKeyCompare(const void *a,const void *b);
Packit 8f70b4
};
Packit 8f70b4
#endif //KEYVALUE_H