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 : public Avl_node<Avl_string_base>
  23     {
  24        private:
  25  
  26           const char *_str;

  27  
  28        protected:
  29  
  30           Avl_string_base(const char *str) : _str(str) { }

  31  
  32        public:
  33  
  34           const char *name() const { return _str; }
  35  
  36  
  37           /************************
  38            ** Avl node interface **
  39            ************************/

  40  
  41           bool higher(Avl_string_base *c) { return (strcmp(c->_str, _str) > 0); }
  42  
  43           /**
  44            * Find by name
  45            */

  46           Avl_string_base *find_by_name(const char *name)
  47           {
  48              if (strcmp(name, _str) == 0) return this;
  49  
  50              Avl_string_base *= Avl_node<Avl_string_base>::child(strcmp(name, _str) > 0);
  51              return c ? c->find_by_name(name) : 0;

  52           }

  53     }
;

  54  
  55  
  56     /*
  57      * The template pumps up the Avl_string_base object and
  58      * provides the buffer for the actual string.
  59      */

  60     template <int STR_LEN>
  61     class Avl_string : public Avl_string_base
  62     {
  63        private:
  64  
  65           char _str_buf[STR_LEN];

  66  
  67        public:
  68  
  69           Avl_string(const char *str) : Avl_string_base(_str_buf)
  70           {
  71              strncpy(_str_buf, str, sizeof(_str_buf));
  72              _str_buf[STR_LEN - 1] = 0;

  73           }

  74     }
;

  75  }

  76  
  77  #endif /* _INCLUDE__UTIL__AVL_STRING_H_ */