summaryrefslogtreecommitdiff
path: root/led/complex1/menu.cc
blob: d6918ace809b0f9c2bb81008ebf67f4c929f042c (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
/*
 *  This is a general set of routines to provide a user interface
 *  for testing.
 *
 *  $Id$
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "menu.h"

int Test_menu_Get_int_bounded(
  const char        *str,
  int                min,
  int                max
)
{
  int                          selection = 0x80000000;
  char                         Message[ 80 ];
  int                          i=0;
  bool                         done = false;
  char                        *temp;

  assert( min <= max );
  do {
    if (i != 0)
      printf("  Invalid Entry\n");

    printf("\n %s (%d:%d) > ", str, min, max );
    fflush( stdout );
    fflush( stdin );
    fgets( Message, 80, stdin );
    if ((Message[0] != '\0') && (Message[0] != '\n')) {
      selection = strtol( Message, &temp, 10 );
      if ((*temp == '\0') || (*temp == '\n') || (*temp == '\r') )
        if (!( selection <  min  || selection > max ))
          done = true;
    }
    i++;
  }
  while  (!done );

  return selection;
}
int Test_menu_Get_int(
  const char        *str
)
{
  int                          selection = 0x80000000;
  char                         Message[ 80 ];
  int                          i=0;
  bool                         done = false;
  char                        *temp;

  do {
    if (i != 0)
      printf("  Invalid Entry\n");

    printf("\n %s > ", str );
    fflush( stdout );
    fflush( stdin );
    fgets( Message, 80, stdin );
    selection = strtol( Message, &temp, 10 );
    if ( *temp == '\0' )
      done = true;
    i++;
  }
  while  (!done );

  return selection;
}

float Test_menu_Get_float_bounded(
  const char        *str,
  float              min,
  float              max
)
{
  double                       selection = -99999999.0;
  char                         Message[ 80 ];
  int                          i=0;
  bool                         done = false;
  char                        *temp;

  assert( min <= max );

  do {
    if (i != 0)
      printf("  Invalid Entry\n");

    printf("\n %s (%f:%f) > ", str, min, max );
    fflush( stdout );
    fflush( stdin );
    fgets( Message, 80, stdin );
    selection = strtod( Message, &temp );
    if (( *temp == '\0' ) || ( *temp == '\n' ))
      if (!( selection <  min  || selection > max ))
        done = true;
    i++;
  }
  while  (!done );

  return (float)selection;
}

int Test_menu_Get_selection(
  const char        *title,
  int                count,
  char              *selection[]
)
{
  int      i;
  int      value;

  printf("\n     %s     \n     ", title);
  for(i=0; i<(int)strlen(title);i++)
    printf("=");
  printf("\n");

  if ( count<21 ) {
    for (i=0; i<count; i++)
      printf("\n(%d) %s", i, selection[i] );
  } else {
    for (i=0; i<count; ) {
      printf("\n(%2d) %-30s", i, selection[i] );
      if ( ++i >= count ) break;
      printf("    (%2d) %-30s", i, selection[i] );
      i++;
    }
  }

  value = Test_menu_Get_int_bounded(
    "    Enter your selection> ",
    0,
    count-1
  );

  return value;
}


char *Test_menu_Get_string(
   const char        *str
)
{
  static char               Message[ 500 ];
  int    last;

  printf("\n Enter %s : ", str);
  fgets( Message, 500, stdin );
  last = strlen( Message );

  if (Message[last-1] == '\n')
    Message[last-1] = '\0';

  return Message;
}

void Test_menu_run(
  Test_menu     *menu
)
{
  int                          selection;
  bool                         done = false;
  int                          i;

  while ( !done ) {

    printf("\n     %s     \n     ", menu->header);
    for(i=0; i<(int)strlen(menu->header);i++)
      printf("=");
     printf("\n");

    for (i=0; i<(int)menu->number_of_entries; i++)
      printf("\n     (%d) %s", i+1, menu->entry[i].name );

    printf("\n     (0) Exit\n");

    selection = Test_menu_Get_int_bounded(
      "    Enter your selection> ",
      0,
      menu->number_of_entries
    );

    if ( selection == 0 ) {
       // printf( "Exiting %s\n", menu->header );
       done = true;
    } else {
      (*menu->entry[ selection - 1 ].routine)();
    }
  }
}