[2696] | 1 | |
---|
| 2 | // ############################################################################ |
---|
| 3 | // |
---|
| 4 | // create_mz_kpp_module |
---|
| 5 | // |
---|
| 6 | // create vectorcode from .90 sources created by KPP to be used in MESSy |
---|
| 7 | // |
---|
| 8 | // COPYRIGHT Klaus Ketelsen and MPI-CH April 2007 |
---|
| 9 | // |
---|
| 10 | // ############################################################################ |
---|
| 11 | |
---|
| 12 | #include "program_line.h" |
---|
| 13 | |
---|
| 14 | void program_line::set_line(string s) { |
---|
| 15 | |
---|
| 16 | line = s; |
---|
| 17 | |
---|
| 18 | tokens.fill_token(line); |
---|
| 19 | |
---|
| 20 | return; |
---|
| 21 | } |
---|
| 22 | void program_line::substitute(string old_s, string new_s) { |
---|
| 23 | |
---|
| 24 | int pos = line.find (old_s, 0); // look for string |
---|
| 25 | |
---|
| 26 | if (pos != string::npos) { // found |
---|
| 27 | line.replace(pos,old_s.size(),new_s); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | return; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void program_line::global_substitute(string old_s, string new_s) { |
---|
| 34 | int pos; |
---|
| 35 | |
---|
| 36 | int start = line.size()-1; |
---|
| 37 | |
---|
| 38 | while (1) { |
---|
| 39 | pos = line.rfind (old_s, start); // look for string |
---|
| 40 | |
---|
| 41 | if (pos == string::npos) { |
---|
| 42 | break; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | line.replace(pos,old_s.size(),new_s); |
---|
| 46 | |
---|
| 47 | start = pos-1; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | return; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | int program_line::get_token_number_from_string (string s) { |
---|
| 54 | |
---|
| 55 | for (int i=0; i<tokens.size(); i++) { |
---|
| 56 | if(tokens.get_token_by_index(i) == s) { |
---|
| 57 | return i; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | return -1; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | int program_line::get_token_number_from_string_upper (string s) { |
---|
| 65 | |
---|
| 66 | string s1 = my_to_upper(s); |
---|
| 67 | for (int i=0; i<tokens.size(); i++) { |
---|
| 68 | if(my_to_upper(tokens.get_token_by_index(i)) == s1) { |
---|
| 69 | return i; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | return -1; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void program_line::update_token (int i, string s) { |
---|
| 77 | |
---|
| 78 | string s_old=tokens.get_token_by_index(i); |
---|
| 79 | tokens.update (i,s); |
---|
| 80 | int pos=tokens.get_position(i); |
---|
| 81 | line.replace(pos,s_old.size(),s); |
---|
| 82 | |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void program_line::change_variable_to_vector (string var) { |
---|
| 87 | string U_token; |
---|
| 88 | |
---|
| 89 | int ind = tokens.size()-2; |
---|
| 90 | for (int i=ind; i >= 0; i--) { |
---|
| 91 | U_token.clear(); |
---|
| 92 | U_token = my_to_upper(tokens.get_token_by_index(i)); |
---|
| 93 | if(U_token == my_to_upper(var) && tokens.get_token_by_index(i+1) == "(" ) { |
---|
| 94 | string new_s="(1:VL,"; |
---|
| 95 | tokens.update (i+1,new_s); |
---|
| 96 | int pos=tokens.get_position(i+1); |
---|
| 97 | line.replace(pos,1,new_s); |
---|
| 98 | tokens.reset (line); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void program_line::change_variable_to_vector_g (Vvar &var) { |
---|
| 106 | string U_token; |
---|
| 107 | string new_s; |
---|
| 108 | |
---|
| 109 | tokens.reset (line); |
---|
| 110 | |
---|
| 111 | int ind = tokens.size()-2; |
---|
| 112 | for (int i=ind; i >= 0; i--) { |
---|
| 113 | U_token.clear(); |
---|
| 114 | U_token = tokens.get_token_by_index(i); |
---|
| 115 | if(var.nr_dim() == 0) { |
---|
| 116 | if(my_to_upper(U_token) == my_to_upper(var.name) ) { |
---|
| 117 | new_s=U_token + "(k)"; |
---|
| 118 | tokens.update (i,new_s); |
---|
| 119 | int pos=tokens.get_position(i); |
---|
| 120 | line.replace(pos,U_token.size(),new_s); |
---|
| 121 | tokens.reset (line); |
---|
| 122 | } |
---|
| 123 | } else { |
---|
| 124 | if(my_to_upper(U_token) == my_to_upper(var.name) && tokens.get_token_by_index(i+1) == "(" ) { |
---|
| 125 | new_s="(k,"; |
---|
| 126 | tokens.update (i+1,new_s); |
---|
| 127 | int pos=tokens.get_position(i+1); |
---|
| 128 | line.replace(pos,1,new_s); |
---|
| 129 | tokens.reset (line); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | |
---|