@@ -96,18 +96,11 @@ public function convert()
96
96
if (!isset ($ cSourceCodeMap [$ classNameCSourceFileKey ])) {
97
97
//read the c source file content
98
98
$ cSourceCode = file_get_contents ($ this ->inputDir ."/ " .$ cSourceFile );
99
- //prepend file name on each defined c functions
100
- $ cSourceCode = preg_replace_callback ("|[a-zA-Z0-9_]+[\s]*\(.*\)([\s]*){| " ,function ($ matches ) use (&$ cSourceFile ) {
101
- if (count ($ matches ) > 0 && strlen ($ matches [0 ]) > 0 ) {
102
- //tricky, need to make sure it is not if, for and while
103
- $ functionName = trim (substr ($ matches [0 ], 0 , strpos ($ matches [0 ],"( " )));
104
- if ( $ functionName !== "for " && $ functionName !== "while " && $ functionName !== "if " ) {
105
- return explode (". " ,$ cSourceFile )[0 ]."_ " .$ matches [0 ];
106
- } else {
107
- return $ matches [0 ];
108
- }
109
- }
110
- },$ cSourceCode );
99
+
100
+ //now prefix the c source file's pure name to each function and corresponding function calls
101
+ //this way, we will be sure that we will not have the same function name defined in more than one c source file
102
+ $ cSourceCode = $ this ->prefixFunctionsWithCSourceFileName ($ cSourceFile , $ cSourceCode );
103
+
111
104
$ cSourceCodeMap [$ classNameCSourceFileKey ] = $ cSourceCode ;
112
105
$ withCSourceCode = "%{ \n" .$ cSourceCodeMap [$ classNameCSourceFileKey ]."\n}% \n" .$ originalCode ;
113
106
$ this ->postSearchAndReplace ($ originalCode , $ withCSourceCode );
@@ -125,4 +118,33 @@ public function convert()
125
118
$ this ->searchAndReplace ($ originalClassCode , $ currentClassCode );
126
119
}
127
120
}
121
+
122
+ private function prefixFunctionsWithCSourceFileName ($ cSourceFile , $ cSourceCode )
123
+ {
124
+ //prepend file name on each defined c functions
125
+ $ cFunctionCallsToSearch = array ();
126
+ $ cFunctionCallsToReplace = array ();
127
+ $ prefix = explode (". " ,$ cSourceFile )[0 ];
128
+ $ cSourceCode = preg_replace_callback ("|[a-zA-Z0-9_]+[\s]*\(.*\)([\s]*){| " ,function ($ matches ) use (&$ prefix ,&$ cFunctionCallsToSearch , &$ cFunctionCallsToReplace ) {
129
+ if (count ($ matches ) > 0 && strlen ($ matches [0 ]) > 0 ) {
130
+ //tricky, need to make sure it is not if, for and while
131
+ $ functionName = trim (substr ($ matches [0 ], 0 , strpos ($ matches [0 ],"( " )));
132
+ if ( $ functionName !== "for " && $ functionName !== "while " && $ functionName !== "if " ) {
133
+ $ cFunctionCallsToSearch [] = $ functionName ."( " ;
134
+ $ cFunctionCallsToReplace [] = $ prefix ."_ " .$ functionName ."( " ;
135
+ return $ prefix ."_ " .$ matches [0 ];
136
+ } else {
137
+ return $ matches [0 ];
138
+ }
139
+ }
140
+ },$ cSourceCode );
141
+
142
+ //now also change all subsequent function calls in context
143
+ $ cSourceCode = str_replace ($ cFunctionCallsToSearch , $ cFunctionCallsToReplace , $ cSourceCode );
144
+
145
+ //now this will result in the pattern {prefix}_{prefix}_, we need to change that to {prefix}_
146
+ $ cSourceCode = str_replace (" {$ prefix }_ {$ prefix }_ " ," {$ prefix }_ " , $ cSourceCode );
147
+
148
+ return $ cSourceCode ;
149
+ }
128
150
}
0 commit comments