1 /*
2 * \brief Utility for handling strings as AVL-node keys
3 * \author Norman Feske
4 * \date 2006-07-12
5 */
6
7 /*
8 * Copyright (C) 2006-2013 Genode Labs GmbH
9 *
10 * This file is part of the Genode OS framework, which is distributed
11 * under the terms of the GNU General Public License version 2.
12 */
13
14 #ifndef _INCLUDE__UTIL__AVL_STRING_H_
15 #define _INCLUDE__UTIL__AVL_STRING_H_
16
17 #include <util/avl_tree.h>
18 #include <util/string.h>
19
20 namespace Genode {
21
22 class Avl_string_base;
23 template <int> class Avl_string;
24 }
25
26
27 class Genode::Avl_string_base : public Avl_node<Avl_string_base>
28 {
29 private:
30
31 const char *_str;
32
33 protected:
34
35 /**
36 * Constructor
37 *
38 * \noapi
39 */
40 Avl_string_base(const char *str) : _str(str) { }
41
42 public:
43
44 const char *name() const { return _str; }
45
46
47 /************************
48 ** Avl node interface **
49 ************************/
50
51 bool higher(Avl_string_base *c) { return (strcmp(c->_str, _str) > 0); }
52
53 /**
54 * Find by name
55 */
56 Avl_string_base *find_by_name(const char *name)
57 {
58 if (strcmp(name, _str) == 0) return this;
59
60 Avl_string_base *c = Avl_node<Avl_string_base>::child(strcmp(name, _str) > 0);
61 return c ? c->find_by_name(name) : 0;
62 }
63 };
64
65
66 /*
67 * The template pumps up the Avl_string_base object and provides the buffer for
68 * the actual string.
69 */
70 template <int STR_LEN>
71 class Genode::Avl_string : public Avl_string_base
72 {
73 private:
74
75 char _str_buf[STR_LEN];
76
77 public:
78
79 Avl_string(const char *str) : Avl_string_base(_str_buf)
80 {
81 strncpy(_str_buf, str, sizeof(_str_buf));
82 _str_buf[STR_LEN - 1] = 0;
83 }
84 };
85
86 #endif /* _INCLUDE__UTIL__AVL_STRING_H_ */