Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Interference_Simple StrCommandHandler_Demo
StrCommandHandler.cpp
00001 #include "StrCommandHandler.h" 00002 00003 char const * const StrCommandHandler::ARROW_UP = "\x1b\x5b\x41"; 00004 char const * const StrCommandHandler::ARROW_DOWN = "\x1b\x5b\x42"; 00005 char const * const StrCommandHandler::ARROW_RIGHT= "\x1b\x5b\x43"; 00006 char const * const StrCommandHandler::ARROW_LEFT = "\x1b\x5b\x44"; 00007 00008 StrCommandHandler::StrCommandHandler ( 00009 int const arg_num_ofcommands 00010 ): 00011 m_num_ofcommands(arg_num_ofcommands) 00012 { 00013 m_command_name = new char[m_num_ofcommands][16]; 00014 m_function = new (void * (* [m_num_ofcommands])(const char * const)); 00015 } 00016 00017 void StrCommandHandler::map( 00018 const char * const arg_command, 00019 void * (*arg_pfunc)(const char * const) 00020 ) 00021 { 00022 static int itr_push = 0; 00023 00024 00025 // Reallocation 00026 if (itr_push == m_num_ofcommands) { 00027 m_num_ofcommands += 5; 00028 char (*tmp_cm)[16] = new char[m_num_ofcommands][16]; 00029 void * (**tmp_fu)(const char * const)= new (void * (* [m_num_ofcommands])(const char * const)); 00030 00031 memcpy(tmp_cm, m_command_name, sizeof(char) * (m_num_ofcommands - 5) * 16); 00032 memcpy(tmp_fu, m_function, sizeof(void * (*)(const char * const)) * (m_num_ofcommands - 5)); 00033 00034 delete[] m_command_name; 00035 delete[] m_function; 00036 00037 m_command_name = tmp_cm; 00038 m_function = tmp_fu; 00039 } 00040 00041 // Register the command name 00042 for (int i = 0; i < 16; i++) { 00043 m_command_name[itr_push][i] = arg_command[i]; 00044 if (arg_command[i] == '\0') break; 00045 } 00046 m_command_name[itr_push][15] = '\0'; 00047 00048 // Register the pointer to function 00049 m_function[itr_push] = arg_pfunc; 00050 00051 itr_push++; 00052 } 00053 00054 00055 void * StrCommandHandler::exe( 00056 const char* const arg_command 00057 ) 00058 { 00059 int key = 0; 00060 00061 // Exception: NULL pointer 00062 if ( arg_command == NULL) return &(m_rescode = 0xFFFFFFFF); 00063 00064 // Exception: Null character 00065 if ( arg_command[0] == '\0') return &(m_rescode = 0xFFFFFFFE); 00066 00067 // Exception: Over length of Command's name 00068 for ( int i = 1; i < 16; i++ ) { 00069 if (arg_command[i] == '\0') break; 00070 if (i == 15) return &(m_rescode = 0xFFFFFFFD); 00071 } 00072 00073 // Search the command list 00074 while ( 00075 key < m_num_ofcommands 00076 && strcmp(m_command_name[key], arg_command) != 0 00077 ) { 00078 key++; 00079 } 00080 00081 // Exception: Not registered Command 00082 if ( key == m_num_ofcommands ) return &(m_rescode = 0xFFFFFFFC); 00083 00084 return (*m_function[key])(arg_command);; 00085 } 00086 00087 void StrCommandHandler::list() 00088 { 00089 puts("\n\n---------------"); 00090 puts("---------------\n"); 00091 puts("list of command\n"); 00092 for(int i = 0; i < m_num_ofcommands; i++) { 00093 if(m_function[i] != NULL) { 00094 printf("%02d: %16s", i, m_command_name[i]); 00095 //printf(" / result: %d", (*m_function[i])()); 00096 puts("\n"); 00097 } 00098 } 00099 puts("---------------\n"); 00100 }
Generated on Tue Jul 12 2022 23:49:56 by
1.7.2