Donwey Posted February 22, 2015 Share Posted February 22, 2015 Hey all, Ive been lately dealing with a problem how to properly detect c function declarations in .h file (which is C99 standard) and save its info .... Suppose we have a .h file consisting of 3 lines: struct SymbolTable;void initContext(Context *pt); (<----- function 1)void deleteContext(Context *pt); i need a regular expresions to detect those function declarations and possibly save its info accordingly to this: $function1type == void$function1name == initContext$function1params == Context *pt Is it possible? Ive been struggling a lot with this but cant get it to work... In a .h file i have already removed blocs and macros and comments... Link to comment https://forums.phpfreaks.com/topic/294801-php-regex-to-match-function-declaration-in-h-file/ Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 Try // header file $linesInCode = file('yourheaderfile.h'); // function data will be stored in this array $functions = array(); // loop over the lines in the file foreach($linesInCode as $line) { // if a function signature has been found if(preg_match('~(\w+)\s+(\w+)\(([^\)]+)\);~i', $line, $match)) { // grab the function type, name and parameters into an array $function = array(); $function['type'] = $match[1]; $function['name'] = $match[2]; $function['params'] = explode(',', $match[3]); // split the function parameters by the comma // append function info to $functions array $functions[] = $function; } } // display structure of $functions array printf('<pre>%s></pre>', print_r($functions, 1)); Output with your test code Array ( [0] => Array ( [type] => void [name] => initContext [params] => Array ( [0] => Context *pt ) ) [1] => Array ( [type] => void [name] => deleteContext [params] => Array ( [0] => Context *pt ) ) ) Link to comment https://forums.phpfreaks.com/topic/294801-php-regex-to-match-function-declaration-in-h-file/#findComment-1506421 Share on other sites More sharing options...
Donwey Posted February 22, 2015 Author Share Posted February 22, 2015 Wow Thanks for answering so quickly , tryed it, seems to work ... but it should work with all function declarations you can type in C99 standard for example : char *stringGet(tString *str); schould return [type]--> char * [name] => stringGet[params] => Array([0] =>tString *str) But its not detecting it so it ignores it :/ Is it possible to detect it? Link to comment https://forums.phpfreaks.com/topic/294801-php-regex-to-match-function-declaration-in-h-file/#findComment-1506481 Share on other sites More sharing options...
Donwey Posted February 22, 2015 Author Share Posted February 22, 2015 EDIT : somehow made it work with : (\w+\s+\W)(\w+)\(([^\)]+)\); But iam not sure about what other declarations i can get and if the regex will detect them. Is it possible to make regex even b etter to make sure it will detect all possible C99 function declarations? Link to comment https://forums.phpfreaks.com/topic/294801-php-regex-to-match-function-declaration-in-h-file/#findComment-1506482 Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2015 Share Posted February 23, 2015 But iam not sure about what other declarations i can get and if the regex will detect them. Is it possible to make regex even better to make sure it will detect all possible C99 function declarations? I am not familiar with C so not sure what the C99 standards are. I only wrote the regex so it matched the example function declarations you had posted. Link to comment https://forums.phpfreaks.com/topic/294801-php-regex-to-match-function-declaration-in-h-file/#findComment-1506509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.