summaryrefslogtreecommitdiffstats
path: root/akregator/src/mk4storage/metakit/src/field.cpp
blob: 3867d6b5cefccff24fbe821e23e3edf5a7cfa0de (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
111
112
113
114
115
116
117
118
119
// field.cpp --
// $Id$
// This is part of Metakit, the homepage is http://www.equi4.com/metakit/

/** @file
 * Implementation of the field structure tree
 */

#include "header.h"
#include "field.h"

#include <stdlib.h>   // strtol

#if !q4_INLINE
#include "field.inl"
#endif

/////////////////////////////////////////////////////////////////////////////
// Implemented in this file

  class c4_Field;

/////////////////////////////////////////////////////////////////////////////
// c4_Field

c4_Field::c4_Field (const char*& description_, c4_Field* parent_)
  : _type (0)
{
  _indirect = this;

  size_t n = strcspn(description_, ",[]");
  const char* p = strchr(description_, ':');

  if (p != 0 && p < description_ + n) {
    _name = c4_String (description_, p - description_);
    _type = p[1] & ~0x20; // force to upper case
  } else {
    _name = c4_String (description_, n);
    _type = 'S';
  }

  description_ += n;
    
  if (*description_ == '[') {
    ++description_;
    _type = 'V';

    if (*description_ == '^') {
      ++description_;
      _indirect = parent_;
      d4_assert(*description_ == ']');
    }

    if (*description_ == ']')
      ++description_;
    else
      do {
	  // 2004-01-20 ignore duplicate property names
	  // (since there is no good way to report errors at this point)
	c4_Field* sf = d4_new c4_Field (description_, this);
	for (int i = 0; i < NumSubFields(); ++i)
	  if (SubField(i).Name().CompareNoCase(sf->Name()) == 0) {
	    delete sf;
	    sf = 0;
	    break;
	  }
	if (sf != 0)
	  _subFields.Add(sf);
      } while (*description_++ == ',');
  }
}

c4_Field::~c4_Field ()
{
  if (_indirect == this) {
    //better? for (int i = NumSubFields(); --i >= 0 ;)
    for (int i = 0; i < NumSubFields(); ++i) {
      c4_Field* sf = & SubField(i);
      if (sf != this) // careful with recursive subfields
      delete sf;
    }
  }
}

c4_String c4_Field::Description(bool anonymous_) const
{
  c4_String s = anonymous_ ? "?" : (const char*) Name();
  
  if (Type() == 'V')
    s += "[" + DescribeSubFields(anonymous_) + "]";
  else {
    s += ":";
    s += (c4_String) Type();
  }
  
  return s;
}
  
c4_String c4_Field::DescribeSubFields(bool) const
{
  d4_assert(Type() == 'V');

  if (_indirect != this)
    return "^";

  c4_String s;
  char c = 0;
  
  for (int i = 0; i < NumSubFields(); ++i) {
    if (c != 0)
      s += (c4_String) c;
    s += SubField(i).Description();
    c = ',';
  } 
  
  return s;
}
  
/////////////////////////////////////////////////////////////////////////////