Allow TABS in config
[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/Kernel.h"
9 #include "libs/utils.h"
10 #include "system_LPC17xx.h"
11 #include "LPC17xx.h"
12 #include "utils.h"
13 using namespace std;
14 #include <string>
15 using std::string;
16 #include <cstring>
17 #include <stdio.h>
18
19 volatile bool _isr_context = false;
20
21 uint16_t get_checksum(const string& to_check){
22 return get_checksum(to_check.c_str());
23 }
24
25 uint16_t get_checksum(const char* to_check){
26 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
27 uint16_t sum1 = 0;
28 uint16_t sum2 = 0;
29 const char* p= to_check;
30 char c;
31 while((c= *p++) != 0) {
32 sum1 = (sum1 + c) % 255;
33 sum2 = (sum2 + sum1) % 255;
34 }
35 return (sum2 << 8) | sum1;
36 }
37
38 void get_checksums(uint16_t check_sums[], const string key){
39 const string k = key+" ";
40 check_sums[0] = 0x0000;
41 check_sums[1] = 0x0000;
42 check_sums[2] = 0x0000;
43 size_t begin_key = 0;
44 unsigned int counter = 0;
45 while( begin_key < key.size()-1 ){
46 const size_t end_key = k.find_first_of(" .", begin_key);
47 const string key_node = k.substr(begin_key, end_key - begin_key);
48 check_sums[counter] = get_checksum(key_node);
49 begin_key = end_key + 1;
50 counter++;
51 }
52 }
53
54 bool is_alpha(int c)
55 {
56 if ((c >= 'a') && (c <= 'z')) return true;
57 if ((c >= 'A') && (c <= 'Z')) return true;
58 if ((c == '_')) return true;
59 return false;
60 }
61
62 bool is_digit(int c)
63 {
64 if ((c >= '0') && (c <= '9')) return true;
65 return false;
66 }
67
68 bool is_numeric(int c)
69 {
70 if (is_digit(c)) return true;
71 if ((c == '.') || (c == '-')) return true;
72 if ((c == 'e')) return true;
73 return false;
74 }
75
76 bool is_alphanum(int c)
77 {
78 return is_alpha(c) || is_numeric(c);
79 }
80
81 bool is_whitespace(int c)
82 {
83 if ((c == ' ') || (c == '\t')) return true;
84 return false;
85 }
86
87 // Convert to lowercase
88 string lc(string str){
89 for (unsigned int i=0; i<strlen(str.c_str()); i++)
90 if (str[i] >= 0x41 && str[i] <= 0x5A)
91 str[i] = str[i] + 0x20;
92 return str;
93 }
94
95 // Remove non-number characters
96 string remove_non_number( string str ){
97 string number_mask = "0123456789-.abcdefpxABCDEFPX";
98 size_t found=str.find_first_not_of(number_mask);
99 while (found!=string::npos){
100 //str[found]='*';
101 str.replace(found,1,"");
102 found=str.find_first_not_of(number_mask);
103 }
104 return str;
105 }
106
107 // Get the first parameter, and remove it from the original string
108 string shift_parameter( string &parameters ){
109 size_t beginning = parameters.find_first_of(" ");
110 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; }
111 string temp = parameters.substr( 0, beginning );
112 parameters = parameters.substr(beginning+1, parameters.size());
113 return temp;
114 }
115
116 // Separate command from arguments
117 string get_arguments( string possible_command ){
118 size_t beginning = possible_command.find_first_of(" ");
119 if( beginning == string::npos ){ return ""; }
120 return possible_command.substr( beginning + 1, possible_command.size() - beginning + 1);
121 }
122
123 // Returns true if the file exists
124 bool file_exists( const string file_name ){
125 bool exists = false;
126 FILE *lp = fopen(file_name.c_str(), "r");
127 if(lp){ exists = true; }
128 fclose(lp);
129 return exists;
130 }
131
132 // Prepares and executes a watchdog reset for dfu or reboot
133 void system_reset( bool dfu ){
134 if(dfu) {
135 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
136 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
137 LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second
138 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
139 LPC_WDT->WDFEED = 0xAA; // Kick the dog!
140 LPC_WDT->WDFEED = 0x55;
141 }else{
142 NVIC_SystemReset();
143 }
144 }
145
146 // Convert a path indication ( absolute or relative ) into a path ( absolute )
147 string absolute_from_relative( string path )
148 {
149 string cwd = THEKERNEL->current_path;
150
151 if ( path.empty() ) {
152 return THEKERNEL->current_path;
153 }
154
155 if ( path[0] == '/' ) {
156 return path;
157 }
158
159 string match = "../" ;
160 while ( path.substr(0,3) == match ) {
161 path = path.substr(3);
162 unsigned found = cwd.find_last_of("/");
163 cwd = cwd.substr(0,found);
164 }
165
166 match = ".." ;
167 if ( path.substr(0,2) == match ) {
168 path = path.substr(2);
169 unsigned found = cwd.find_last_of("/");
170 cwd = cwd.substr(0,found);
171 }
172
173 if ( cwd[cwd.length() - 1] == '/' ) {
174 return cwd + path;
175 }
176
177 return cwd + '/' + path;
178 }