|
| 1 | +<?php |
| 2 | +namespace PHPtoCExt\Converter; |
| 3 | + |
| 4 | +/** |
| 5 | + * Automatically convert c_function_auto api to corresponding c_function_call apis |
| 6 | + */ |
| 7 | +class CFunctionAutoConverter extends \PHPtoCExt\Converter |
| 8 | +{ |
| 9 | + public function convert() |
| 10 | + { |
| 11 | + //first, find out all call_c_auto calls and find out the corresponding class context this c function call belongs to |
| 12 | + $classMap = $this->getClassMap(); |
| 13 | + |
| 14 | + foreach($classMap as $className => $classInfo) { |
| 15 | + $originalClassCode = implode("\n",array_slice($this->codeLines, $classInfo->startLine - 1, $classInfo->endLine - $classInfo->startLine + 1)); |
| 16 | + foreach($classInfo->methodInfos as $methodPureName => $methodInfo) { |
| 17 | + for ($index = $methodInfo->startLine; $index <= $methodInfo->endLine; $index++) { |
| 18 | + if (preg_match("/call_c_auto\(.*\)/", $this->codeLines[$index], $matches)) { |
| 19 | + if(count($matches) == 1) { |
| 20 | + $codeLine = $this->codeLines[$index]; //store the original code line here |
| 21 | + $filteredCodeLine = str_replace(array("call_c_auto","(",")",";"),"",trim($codeLine)); |
| 22 | + $lineComps = explode("=",$filteredCodeLine); |
| 23 | + $lineCompsCount = count($lineComps); |
| 24 | + $className[0] = ""; |
| 25 | + $className = trim($className); |
| 26 | + $classNameComps = explode("\\", $className); |
| 27 | + //remove namespace |
| 28 | + array_shift($classNameComps); |
| 29 | + $className = implode("/",$classNameComps); |
| 30 | + if ($lineCompsCount == 1) { //this means we do not have return result variable |
| 31 | + $inputParamsStr = trim($lineComps[0]); |
| 32 | + if (strlen($inputParamsStr) == 0) { |
| 33 | + $this->searchAndReplace($filteredCodeLine,"call_c_function(\"$className.c\",\"$methodPureName\");"); |
| 34 | + } else { |
| 35 | + $this->searchAndReplace($codeLine,"call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);"); |
| 36 | + } |
| 37 | + } else if ($lineCompsCount == 2){ //this means we have input return result variable |
| 38 | + $returnVarStr = $lineComps[0]; |
| 39 | + $inputParamsStr = $lineComps[1]; |
| 40 | + if (strlen($inputParamsStr) == 0) { |
| 41 | + $this->searchAndReplace($filteredCodeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\");"); |
| 42 | + } else { |
| 43 | + $this->searchAndReplace($codeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);"); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments