summaryrefslogtreecommitdiffstats
path: root/linklist.h
blob: 1b06b869e76669f6a67bb6e320dd45a97b8ecef0 (plain)
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
/*  C++ Linked list class, threadsafe (boolean is atom)
 *
 *  (c) Copyright 2001 Denis Roio aka jaromil <jaromil@dyne.org>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This source code 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.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __linklist_h__
#define __linklist_h__


// uncomment to have mutex locked operations
// can be slow on OSX and adds dependency to pthreads
//#define THREADSAFE 1
#ifdef THREADSAFE
#include <pthread.h>
#endif

// maximum lenght of names
#define MAX_ENTRY_NAME 512

// maximum number of members returned by the completion
#define MAX_COMPLETION 256

class Entry;

class Linklist {
 public:
  Linklist();
  virtual ~Linklist();

  Entry *begin() { return(first); };
  Entry *end() { return(last); };
  int len() { return(length); };
  
  void append(Entry *addr);
  void add(Entry *addr) { append(addr); }; /* lazy backward compatibility */
  void prepend(Entry *addr);
  void insert(Entry *addr, int pos);
  void insert_after(Entry *addr, Entry *pos);  
  void rem(int pos);
  void sel(int pos);
  void clear();
  bool moveup(int pos);
  bool movedown(int pos);
  bool moveto(int num, int pos);
  Entry *pick(int pos);  
  Entry *search(char *name);
  int *completion(char *needle);

  Entry *selected();

  Entry *operator[](int pos) { return pick(pos); };

  /* don't touch these directly */
  Entry *first;
  Entry *last;
  int length;


#ifdef THREADSAFE
  pthread_mutex_t mutex;
  void lock() { pthread_mutex_lock(&mutex); };
  void unlock() { pthread_mutex_unlock(&mutex); };
#endif
  
  int compbuf[MAX_COMPLETION]; // maximum completion listsize allowed

};

class Entry {
  friend class Linklist;

 public:
  Entry();
  ~Entry();

  void set_name(char *nn);
  
  Entry *next;
  Entry *prev;

  Linklist *list;

  bool up();
  bool down();
  bool move(int pos);
  void rem();
  void sel(bool on);
  
  bool select;

  char name[MAX_ENTRY_NAME];

  void *data; ///< generic data pointer
};

#endif