input_ip.cc
Go to the documentation of this file.
1 /*
2  * Roboception GmbH
3  * Munich, Germany
4  * www.roboception.com
5  *
6  * Copyright (c) 2024 Roboception GmbH
7  * All rights reserved
8  *
9  * Author: Heiko Hirschmueller
10  */
11 
12 #include "input_ip.h"
13 
14 #include <sstream>
15 
16 InputIP::InputIP(int x, int y, int w, int h, const char *label) : Fl_Input(x, y, w, h, label)
17 {
18  cb=0;
19  user=0;
20 
21  tooltip("Format example: 10.123.4.5");
22 }
23 
24 uint32_t InputIP::getIP()
25 {
26  std::istringstream in(value());
27 
28  // read formated
29 
30  uint32_t v[4]={256, 256, 256, 256};
31  char s[3]={' ', ' ', ' '};
32  in >> v[0] >> s[0] >> v[1] >> s[1] >> v[2] >> s[2] >> v[3];
33 
34  // all separators must be '.'
35 
36  for (int i=0; i<3; i++)
37  {
38  if (s[i] != '.') return 0;
39  }
40 
41  // its invalid if there is unparsed characters left
42 
43  if (!in.eof())
44  {
45  return 0;
46  }
47 
48  // check validity of individual numbers
49 
50  for (int i=0; i<4; i++)
51  {
52  if (v[i] > 255) return 0;
53  }
54 
55  // return IP as integer
56 
57  return ((v[0]<<24) | (v[1]<<16) | (v[2]<<8) | v[3]);
58 }
59 
60 void InputIP::setIP(uint32_t v)
61 {
62  std::ostringstream out;
63 
64  out << ((v>>24)&0xff) << '.' << ((v>>16)&0xff) << '.' << ((v>>8)&0xff) << '.' << (v&0xff);
65 
66  value(out.str().c_str());
67 }
68 
69 int InputIP::handle(int event)
70 {
71  if (event == FL_KEYBOARD && !(Fl::event_key() == FL_Tab))
72  {
73  char ascii=Fl::event_text()[0];
74 
75  if (std::isprint(ascii) && !isdigit(ascii) && ascii != '.')
76  {
77  return 1; // ignore all printable characters except accepted ones
78  }
79  }
80 
81  std::string v=value();
82 
83  // handle event in superclass
84 
85  int ret=Fl_Input::handle(event);
86 
87  // call callback if value has changed
88 
89  if (cb && v != value())
90  {
91  cb(this, user);
92  }
93 
94  return ret;
95 }
InputIP::InputIP
InputIP(int x, int y, int w, int h, const char *label)
Definition: input_ip.cc:16
InputIP::handle
int handle(int event)
Definition: input_ip.cc:69
input_ip.h
InputIP::cb
Fl_Callback * cb
Definition: input_ip.h:37
InputIP::setIP
void setIP(uint32_t v)
Definition: input_ip.cc:60
InputIP::user
void * user
Definition: input_ip.h:38
InputIP::getIP
uint32_t getIP()
Definition: input_ip.cc:24


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56