Advertisement
Mr-A

AScope.cpp

Oct 24th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. #import "AScope.h"
  2.  
  3. int CountString( std::string line, std::string occured )
  4. {
  5.     int occurances_count = 0;
  6.     int search_index = 0;
  7.     while (line.find(occured, search_index) != std::string::npos)
  8.     {
  9.         occurances_count++;
  10.         search_index = line.find(occured, search_index) + 1;
  11.     }
  12.     return occurances_count;
  13. }
  14.  
  15. std::string GetString(std::string line, int init_position, int final_position)
  16. {
  17.     return line.substr(init_position, final_position-init_position);
  18. }
  19.  
  20. std::string FilterString(std::string line, std::string unwanted)
  21. {
  22.     while (line.find(unwanted) != std::string::npos)
  23.     {
  24.         line.erase (line.find(unwanted), unwanted.length());
  25.     }
  26.     return line;
  27. }
  28.  
  29.  
  30. template < typename numeraltype > AScope<numeraltype>::AScope()
  31. {
  32.     _parameter_seperator = ",";
  33.     _parameter_list_open = "(";
  34.     _parameter_list_close = ")";
  35. }
  36.  
  37. template < typename numeraltype > AScope<numeraltype>::~AScope()
  38. {
  39.  
  40. }
  41.  
  42. template < typename numeraltype > void AScope<numeraltype>::setFunctionParameterSeperator(const char * seperator)
  43. {
  44.     _parameter_seperator = seperator;
  45. }
  46.  
  47. template < typename numeraltype > void AScope<numeraltype>::setFunctionParameterBrackets(const char * openning, const char * closing)
  48. {
  49.     _parameter_list_open = openning;
  50.     _parameter_list_close = closing;
  51. }
  52.  
  53. template < typename numeraltype > void AScope<numeraltype>::addVariable(const char * name, numeraltype * value)
  54. {
  55.     _variable_names.push_back( name );
  56.     _variable_pointers.push_back( value );
  57. }
  58.  
  59. template < typename numeraltype > void AScope<numeraltype>::addVariables(std::vector<const char*> & names, std::vector<numeraltype*> * values)
  60. {
  61.     for (auto name : names)
  62.     {
  63.         _variable_names.push_back(name);
  64.     }
  65.     for (auto value : *values)
  66.     {
  67.         _variable_pointers.push_back(value);
  68.     }
  69. }
  70.  
  71. template < typename numeraltype > void AScope<numeraltype>::addFunction0(const char* name, numeraltype (*func)())
  72. {
  73.     _function0_names.push_back(name);
  74.     _function0_pointers.push_back(func);
  75. }
  76.  
  77. template < typename numeraltype > void AScope<numeraltype>::addFunction1(const char* name, numeraltype (*func)(numeraltype))
  78. {
  79.     _function1_names.push_back(name);
  80.     _function1_pointers.push_back(func);
  81. }
  82.  
  83. template < typename numeraltype > void AScope<numeraltype>::addFunction2(const char* name, numeraltype (*func)(numeraltype, numeraltype))
  84. {
  85.     _function2_names.push_back(name);
  86.     _function2_pointers.push_back(func);
  87. }
  88.  
  89. template < typename numeraltype > void AScope<numeraltype>::addFunction3(const char* name, numeraltype (*func)(numeraltype, numeraltype, numeraltype))
  90. {
  91.     _function3_names.push_back(name);
  92.     _function3_pointers.push_back(func);
  93. }
  94.  
  95. template < typename numeraltype > void AScope<numeraltype>::addFunction4(const char* name, numeraltype (*func)(numeraltype, numeraltype, numeraltype, numeraltype))
  96. {
  97.     _function4_names.push_back(name);
  98.     _function4_pointers.push_back(func);
  99. }
  100.  
  101. template < typename numeraltype > void AScope<numeraltype>::addFunction5(const char* name, numeraltype (*func)(numeraltype, numeraltype, numeraltype, numeraltype, numeraltype))
  102. {
  103.     _function5_names.push_back(name);
  104.     _function5_pointers.push_back(func);
  105. }
  106.  
  107. template < typename numeraltype > numeraltype AScope<numeraltype>::getVariableValue(int var_index)
  108. {
  109.     return *(_variable_pointers[var_index]);
  110. }
  111.  
  112. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction0Value(int func_index)
  113. {
  114.     return (*_function0_pointers[func_index])();
  115. }
  116.  
  117. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction1Value(int func_index, numeraltype a)
  118. {
  119.     return (*_function1_pointers[func_index])(a);
  120. }
  121.  
  122. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction2Value(int func_index, numeraltype a, numeraltype b)
  123. {
  124.     return (*_function2_pointers[func_index])(a, b);
  125. }
  126.  
  127. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction3Value(int func_index, numeraltype a, numeraltype b, numeraltype c)
  128. {
  129.     return (*_function3_pointers[func_index])(a, b, c);
  130. }
  131.  
  132. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction4Value(int func_index, numeraltype a, numeraltype b, numeraltype c, numeraltype d)
  133. {
  134.     return (*_function4_pointers[func_index])(a, b, c, d);
  135. }
  136.  
  137. template < typename numeraltype > numeraltype AScope<numeraltype>::getFunction5Value(int func_index, numeraltype a, numeraltype b, numeraltype c, numeraltype d, numeraltype e)
  138. {
  139.     return (*_function5_pointers[func_index])(a, b, c, d, e);
  140. }
  141.  
  142. template < typename numeraltype > void AScope<numeraltype>::setVariableValue(int var_index, numeraltype new_value)
  143. {
  144.     *(_variable_pointers[var_index]) = new_value;
  145. }
  146.  
  147. template <typename numeraltype> std::string AScope<numeraltype>::getFuncParameter(std::string line, int number)
  148. {
  149.     int finds_count = 0, seperator_position, initial_search_position;
  150.  
  151.     while (line.rfind(_parameter_list_open)!=std::string::npos)
  152.     {
  153.         if (line.find(_parameter_list_close, line.rfind(_parameter_list_open)) != std::string::npos)
  154.         {
  155.             seperator_position = line.find(_parameter_seperator, line.rfind(_parameter_list_open));
  156.             while (seperator_position < line.find(_parameter_list_close, line.rfind(_parameter_list_open)))
  157.             {
  158.                 line = line.replace( seperator_position, 1, "1234557");
  159.                 seperator_position = line.find(_parameter_seperator, line.rfind(_parameter_list_open));
  160.             }
  161.             line = line.replace(line.rfind(_parameter_list_open), 1, "1234567");
  162.         }
  163.     }
  164.  
  165.     seperator_position = line.find(_parameter_seperator);
  166.     initial_search_position = seperator_position > 0 ? 0 : _parameter_seperator.length();
  167.     if (line.find(_parameter_seperator) == std::string::npos)
  168.     {
  169.         initial_search_position = 0;
  170.     }
  171.     while (finds_count <= CountString(line, _parameter_seperator))
  172.     {
  173.         if (finds_count == number)
  174.         {
  175.  
  176.             if (line.find(_parameter_seperator, initial_search_position) != std::string::npos)
  177.             {
  178.                 line = GetString(line, initial_search_position, line.find(_parameter_seperator, initial_search_position));
  179.                 while (line.find("1234557") != std::string::npos)
  180.                 {
  181.                     line = line.replace(line.find("1234557"), 7, _parameter_seperator);
  182.                 }
  183.                 while (line.find("1234567") != std::string::npos)
  184.                 {
  185.                     line = line.replace(line.find("1234567"), 7, _parameter_list_open);
  186.                 }
  187.                 return line;
  188.             }
  189.             else
  190.             {
  191.                 line = GetString(line, initial_search_position, line.length());
  192.                 while (line.find("1234557") != std::string::npos)
  193.                 {
  194.                     line = line.replace(line.find("1234557"), 7, _parameter_seperator);
  195.                 }
  196.                 while (line.find("1234567") != std::string::npos)
  197.                 {
  198.                     line = line.replace(line.find("1234567"), 7, _parameter_list_open);
  199.                 }
  200.                 return line;
  201.             }
  202.         }
  203.         finds_count++;
  204.         seperator_position = line.find(_parameter_seperator, initial_search_position);
  205.         initial_search_position = seperator_position + _parameter_seperator.length();
  206.     }
  207.  
  208.     return "0";
  209. }
  210.  
  211. template class AScope<float>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement