YARP
Yet Another Robot Platform
SplitString.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <cstdlib>
13 #include <cstring>
14 
15 
17 
18 SplitString::SplitString() :
19  argc(0)
20 {
21  for (auto& i : argv) {
22  i = nullptr;
23  }
24 }
25 
26 SplitString::SplitString(const char* command, const char splitter)
27 {
28  apply(command, splitter);
29 }
30 
32 {
33  return argc;
34 }
35 
36 void SplitString::set(int index, const char* txt)
37 {
38  if (index >= 0 && index < size()) {
39  strncpy(buf[index], (char*)txt, MAX_ARG_LEN - 1);
40  buf[index][MAX_ARG_LEN - 1] = '\0';
41  }
42 }
43 
44 const char* SplitString::get(int idx)
45 {
46  return buf[idx];
47 }
48 
49 const char** SplitString::get()
50 {
51  return (const char**)argv;
52 }
53 
54 void SplitString::apply(const char* command, char splitter)
55 {
56  size_t at = 0;
57  size_t sub_at = 0;
58  unsigned int i;
59  for (i = 0; i < strlen(command) + 1; i++) {
60  if (at < MAX_ARG_CT) {
61  // yarpserver can deal with quoting.
62  char ch = command[i];
63  if (ch == '\"') {
64  ch = ' ';
65  }
66  if (ch >= 32 || ch == '\0' || ch == '\n') {
67  if (ch == splitter || ch == '\n') {
68  ch = '\0';
69  }
70  if (sub_at < MAX_ARG_LEN) {
71  buf[at][sub_at] = ch;
72  sub_at++;
73  }
74  }
75  if (ch == '\0') {
76  if (sub_at > 1) {
77  at++;
78  }
79  sub_at = 0;
80  }
81  }
82  }
83  for (i = 0; i < MAX_ARG_CT; i++) {
84  argv[i] = buf[i];
85  buf[i][MAX_ARG_LEN - 1] = '\0';
86  }
87 
88  argc = at;
89 }
yarp::os::impl::SplitString::get
const char ** get()
Definition: SplitString.cpp:49
yarp::os::impl::SplitString
Split a string into pieces.
Definition: SplitString.h:27
yarp::os::impl::SplitString::size
int size()
Definition: SplitString.cpp:31
yarp::os::impl::SplitString::SplitString
SplitString()
Definition: SplitString.cpp:18
yarp::os::impl::SplitString::set
void set(int index, const char *txt)
Definition: SplitString.cpp:36
yarp::os::impl::SplitString::apply
void apply(const char *command, char splitter=' ')
Definition: SplitString.cpp:54
SplitString.h