aboutsummaryrefslogtreecommitdiff
path: root/xsd/usage.hxx
blob: ca07b07c6cca0c1615eee86c1a4c88b3d5f085e4 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// file      : xsd/usage.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef USAGE_HXX
#define USAGE_HXX

#include <cult/types.hxx>

#include <backend-elements/indentation/buffer.hxx>
#include <backend-elements/indentation/clip.hxx>

namespace CLI
{
  using namespace Cult::Types;

  template <typename C>
  class OptionsUsage: public BackendElements::Indentation::Buffer<C>
  {
    typedef BackendElements::Indentation::Buffer<C> Buffer;

  public:
    typedef
    typename Buffer::Traits
    Traits;

    typedef
    typename Buffer::AsChar
    AsChar;

    typedef
    typename Buffer::AsInt
    AsInt;

    typedef
    typename Buffer::Write
    Write;

  public:
    OptionsUsage (Buffer& out)
        : out_ (out),
          option_length_ (0),
          construct_ (Construct::newline)
    {
    }

  public:
    virtual AsInt
    put (AsChar c)
    {
      AsInt result (Traits::to_int_type (c));

      try
      {
        switch (c)
        {
        case '\n':
          {
            switch (construct_)
            {
            case Construct::newline:
              {
                result = out_.put (c);
                break;
              }
            case Construct::option:
              {
                construct_ = Construct::newline;
                break;
              }
            case Construct::description:
              {
                result = out_.put (c);
                construct_ = Construct::newline;
                break;
              }
            default:
              {
                abort ();
              }
            }

            break;
          }
        case '-':
          {
            switch (construct_)
            {
            case Construct::newline:
              {
                construct_ = Construct::option;

                option_length_ = 0;

                output_indentation ();
                result = out_.put (c);

                ++option_length_;

                break;
              }
            case Construct::option:
              {
                ++option_length_;
                //fall through
              }
            case Construct::description:
              {
                result = out_.put (c);
                break;
              }
            default:
              {
                abort ();
              }
            }

            break;
          }
        default:
          {
            switch (construct_)
            {
            case Construct::newline:
              {
                construct_ = Construct::description;

                output_indentation ();

                result = out_.put (c);
                break;
              }
            case Construct::option:
              {
                ++option_length_;
                //fall through
              }
            default:
              {
                result = out_.put (c);
                break;
              }
            }

            break;
          }
        }
      }
      catch (Write const&)
      {
        result = Traits::eof ();
      }

      return result;
    }

    virtual Void
    unbuffer ()
    {
    }

  private:
    Void
    output_indentation ()
    {
      UnsignedLong spaces;

      switch (construct_)
      {
      case Construct::option:
        {
          spaces = 2;
          option_length_ += 2;
          break;
        }
      case Construct::description:
        {
          spaces = 29;

          if (option_length_)
          {
            if (option_length_ > spaces)
              spaces = 1;
            else
              spaces -= option_length_;

            option_length_ = 0;
          }

          break;
        }
      default:
        {
          abort ();
        }
      }

      while (spaces--)
        out_.put (' ');
    }

  private:
    Buffer& out_;
    UnsignedLong option_length_;

    struct Construct
    {
      enum Value
      {
        newline,
        option,
        description
      };
    };

    typename Construct::Value construct_;
  };

  //@@ rename Indentation to Indent in be?
  //
  namespace Indent = BackendElements::Indentation;
}

#endif // USAGE_HXX