removed most mbed.h includes
[clinton/Smoothieware.git] / src / libs / utils.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #include "libs/utils.h"
9 using namespace std;
10 #include <string>
11 using std::string;
12 #include <cstring>
13
14
15 uint16_t get_checksum(string to_check){
16 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
17 uint16_t sum1 = 0;
18 uint16_t sum2 = 0;
19 for( int index = 0; index < to_check.length(); ++index ){
20 sum1 = (sum1 + to_check[index]) % 255;
21 sum2 = (sum2 + sum1) % 255;
22 }
23 return (sum2 << 8) | sum1;
24 }
25
26 // Convert to lowercase
27 string lc(string str){
28 for (int i=0;i<strlen(str.c_str());i++)
29 if (str[i] >= 0x41 && str[i] <= 0x5A)
30 str[i] = str[i] + 0x20;
31 return str;
32 }
33
34 // Remove non-number characters
35 string remove_non_number( string str ){
36 string number_mask = "0123456789-.";
37 size_t found=str.find_first_not_of(number_mask);
38 while (found!=string::npos){
39 //str[found]='*';
40 str.replace(found,1,"");
41 found=str.find_first_not_of(number_mask);
42 }
43 return str;
44 }
45
46 // Get the first parameter, and remove it from the original string
47 string shift_parameter( string &parameters ){
48 size_t beginning = parameters.find_first_of(" ");
49 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; }
50 string temp = parameters.substr( 0, beginning );
51 parameters = parameters.substr(beginning+1, parameters.size());
52 return temp;
53 }
54
55 // Separate command from arguments
56 string get_arguments( string possible_command ){
57 size_t beginning = possible_command.find_first_of(" ");
58 if( beginning == string::npos ){ return ""; }
59 return possible_command.substr( beginning+1, possible_command.size() - beginning);
60 }
61
62
63