Skip to content

Commit 75ac7af

Browse files
code refactor, prefix c source file name to all corresponding c functions
1 parent 6e1b3f9 commit 75ac7af

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/PHPtoCExt/Converter/CFuntionCallConverter.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,11 @@ public function convert()
9696
if (!isset($cSourceCodeMap[$classNameCSourceFileKey])) {
9797
//read the c source file content
9898
$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+
111104
$cSourceCodeMap[$classNameCSourceFileKey] = $cSourceCode;
112105
$withCSourceCode = "%{\n".$cSourceCodeMap[$classNameCSourceFileKey]."\n}%\n".$originalCode;
113106
$this->postSearchAndReplace($originalCode, $withCSourceCode);
@@ -125,4 +118,33 @@ public function convert()
125118
$this->searchAndReplace($originalClassCode, $currentClassCode);
126119
}
127120
}
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+
}
128150
}

0 commit comments

Comments
 (0)