3
votes

I need to read and write variables from a "libconfig" formatted file in PHP. But I can't find a library for that anywhere. I know about the C/C++ library of course, but we'd have to write an extension to use that.

Does such a library or extension exist?

2
Google search tends to return lots of results with "lib/config.php" that have nothing to do with it. Putting "libconfig" in quotes helps eliminate those, but still do not find what I'm looking for.GoatRider
Use the C/C++ library and then just call the library and get the results from PHP using exec or similar?Nick Pickering
Are you referring to hyperrealm.com/libconfig ? If so, you might do well to edit the post to make the term "libconfig" a link to it.TML

2 Answers

0
votes
    function parseLibconfig_section( &$Array, &$LineID ){
    $RetVal = array(); // Initializing return value as empty array
    while( count( $Array ) > $LineID ){ // While not riches last line
        if( stripos( $Array[$LineID] , ':' ) !== false ){ 
            // In case we have section Title - just remember it
            // The section will parsed later at section begin (next loop)
            $TArr = explode( ' ', trim( $Array[$LineID] ) );
            $CS = $TArr[0]; 
        } elseif( stripos( $Array[$LineID] , '{' ) !== false ){
            // We at section open Tag -> call recurrent function to parse 
            // from next line of input data
            $LineID++;
            $RetVal[$CS] = parseLibconfig_section( $Array, $LineID );
        } elseif( stripos( $Array[$LineID] , '}' ) !== false ){
            // End of section - return back from subsection
            break;
        } else {
            // nor section begin/ nor section end - parse line as field
            // by standard PHP function parse_ini_string (please see PHP ref)
            $TVrr = parse_ini_string( trim( $Array[$LineID] ) );
            if( count( $TVrr ) ){
                // fill return array by fields from parse_ini_string function
                foreach( $TVrr as $Key => $Val ) $RetVal[$Key] = $Val;
            };
        };
        // Next please!
        $LineID++;
    };
    return $RetVal;
};

function parseLibconfig( $FName ){
    $RetVal = array();       // Initializing return value as empty array
    $Data = file( $FName );  // Reading content of libconfig's
                             // config file into array of lines
    if( count($Data)> 0 ){   // If we have some data read then - working
        $Index = 0;          // Init an variable to pass by reference into
                             // function that will be called recursively
        $RetVal = parseLibconfig_section( $Data, $Index );
    };
    return $RetVal;
};
-1
votes

Using the C or C++ API detailed in the documentation, write a small program to convert the libconfig format file to either JSON or XML (or if you're feeling adventurous, PHP's serialisation format) and then use the PHP libraries to deal with that output. If the file hasn't changed, you could even cache the converted form.

You can call external programs from PHP and get the output using exec().

The best solution would of course be to write PHP bindings for the library, but depending on how important this library is to your application, that is likely not worth it.

Looking at the format, I would not recommend attempting to use regular expressions to parse the file.