00001 #include "bhand_motors.h"
00002
00003 #include <ctype.h>
00004 #include <string.h>
00005
00006
00007 void toMotorChar(BHMotors bhMotors, char *chMotors)
00008 {
00009 if (bhMotors != 15)
00010 {
00011 if ((bhMotors & 7) < 7)
00012 {
00013 if (bhMotors & 1) *(chMotors++) = '1';
00014 if (bhMotors & 2) *(chMotors++) = '2';
00015 if (bhMotors & 4) *(chMotors++) = '3';
00016 }
00017 else
00018 *(chMotors++) = 'G';
00019 if (bhMotors & 8) *(chMotors++) = 'S';
00020 }
00021 *chMotors = 0;
00022 }
00023
00024 BHMotors toBHMotors(const char *motors)
00025 {
00026 BHMotors bhmotors = 0;
00027
00028 if (ContainsMotor(motors, '1')) bhmotors |= 1;
00029 if (ContainsMotor(motors, '2')) bhmotors |= 2;
00030 if (ContainsMotor(motors, '3')) bhmotors |= 4;
00031 if (ContainsMotor(motors, '4')) bhmotors |= 8;
00032
00033 return bhmotors;
00034 }
00035
00036 unsigned int countMotors(BHMotors bhMotors)
00037 {
00038 unsigned int count = 0;
00039 while (bhMotors)
00040 {
00041 if (bhMotors & 1)
00042 count++;
00043 bhMotors >>= 1;
00044 }
00045 return count;
00046 }
00047
00048 bool Contains(const char *str, const char ch)
00049 {
00050 char c = tolower(ch);
00051 for (unsigned int i = 0; i < strlen(str); i++)
00052 if (tolower(str[i]) == c)
00053 return true;
00054 return false;
00055 }
00056
00057 bool ContainsAllFingers(const char *motor)
00058 {
00059 return (strlen(motor) == 0) || Contains(motor, 'g') || (Contains(motor, '1') & Contains(motor, '2') & Contains(motor, '3'));
00060 }
00061
00062 bool ContainsAnyFingers(const char *motor)
00063 {
00064 return (strlen(motor) == 0) || Contains(motor, 'g') || Contains(motor, '1') || Contains(motor, '2') || Contains(motor, '3');
00065 }
00066
00067 bool ContainsSpread(const char *motor)
00068 {
00069 return (strlen(motor) == 0) || Contains(motor, 's') || Contains(motor, '4');
00070 }
00071
00072 bool ContainsMotor(const char *motorString, const char motorChar)
00073 {
00074 char ch = tolower(motorChar);
00075 if (ch == 's')
00076 ch = '4';
00077
00078
00079 if ((ch >= '1' && ch <= '4') || ch == 's')
00080 {
00081 if (motorString[0] == 0)
00082 return true;
00083
00084
00085 if (Contains(motorString, ch))
00086 return true;
00087
00088 if (ch == '4' && Contains(motorString, 's'))
00089 return true;
00090
00091
00092 if (Contains(motorString, 'g') && ch >= '1' && ch <= '3')
00093 return true;
00094 }
00095
00096 return false;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113