Author Topic: PHP extract_constant_macros.lib.php  (Read 1713 times)

Offline chaos

  • BFF
  • ***
  • Posts: 291
  • Job, school, social life, sleep. Pick 2.5.
    • View Profile
    • Lost Souls
PHP extract_constant_macros.lib.php
« on: September 14, 2008, 07:29:04 PM »
This is a PHP utility function for extracting the simplest constant macros from a C or LPC .h file.  The intention is to make PHP code able to import lists of bit-flag values and whatnot from header files.

Code: [Select]
<?php

function extract_constant_macros($filename) {
    
$out = array();
    
$file fopen($filename'r');
    if(!
$file)
        return 
$out;
    
flock($fileLOCK_SH);
    while((
$line fgets($file)) !== false) {
        if(!
preg_match('/^#define (\w+)\s+(.*)/'$line$match))
            continue;
        
$name $match[1];
        
$expansion $match[2];
        if(
preg_match('/^\d+$/'$expansion))
            
$out[$name] = intval($expansion);
        elseif(
preg_match('/^0x[0-9a-fA-F]+$/'$expansion))
            
$out[$name] = hexdec($expansion);
        elseif(
preg_match('/^\d+\.\d*$/'$expansion))
            
$out[$name] = floatval($expansion);
        elseif(
preg_match('/^"(.*)"$/'$expansion$sub_match))
            
$out[$name] = $sub_match[1];
    }
    
fclose($file);
    return 
$out;
}

?>