RPA Toolkit
added ini file parsing test.
authorMartin Stoilov <martin@rpasearch.com>
Tue, 21 Jun 2011 05:44:38 +0000 (22:44 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Tue, 21 Jun 2011 05:44:38 +0000 (22:44 -0700)
phpext/prpa/prpa.c
phpext/prpa/tests/009.phpt
phpext/prpa/tests/010.phpt
phpext/prpa/tests/013.phpt [new file with mode: 0644]

index 49f2edf..d752c1a 100644 (file)
@@ -625,13 +625,15 @@ PHP_FUNCTION(rpa_stat_parse)
     }
     ZEND_FETCH_RESOURCE(phpstat, php_rpa_stat*, &zstat, -1, PHP_RPA_STAT_RES_NAME, le_rpa_stat);
     ret = rpa_stat_parse(phpstat->stat, rid, encoding, input, input, input + input_len, records);
-    if (ret <= 0)
-       goto error;
+    if (ret <= 0) {
+       if (zrecords)
+               ZVAL_NULL(zrecords);
+       rpa_records_destroy(records);
+       RETURN_LONG(ret);
+    }
     if (zrecords) {
        rpa_records2array(input, records, zrecords);
     }
-
-error:
        rpa_records_destroy(records);
        RETURN_LONG(ret);
 }
@@ -686,6 +688,8 @@ dbexerror:
                rpa_dbex_geterrorstr(dbex, buffer, sizeof(buffer) - 1);
                ZVAL_STRING(zerror, buffer, 1);
        }
+//     if (zrecords)
+//             ZVAL_NULL(zrecords);
        rpa_records_destroy(records);
        rpa_stat_destroy(stat);
        rpa_dbex_destroy(dbex);
@@ -696,6 +700,8 @@ staterror:
                rpa_stat_geterrorstr(stat, buffer, sizeof(buffer) - 1);
                ZVAL_STRING(zerror, buffer, 1);
        }
+//     if (zrecords)
+//             ZVAL_NULL(zrecords);
        rpa_records_destroy(records);
        rpa_stat_destroy(stat);
        rpa_dbex_destroy(dbex);
index 2e12eda..6db0b65 100644 (file)
@@ -37,14 +37,16 @@ rpa_dbex_parse($hDbex, $pattern, $myname)
        $matched = "matched: " . substr($myname, 0, $ret) . "\n";
        echo($matched);
        
-       foreach ($records as $record) {
-               if ($record['type'] & RPA_RECORD_START)
-                       echo("START ");
-               else if ($record['type'] & RPA_RECORD_END)
-                       echo("END   ");
-               else
-                       echo("UNKNOWN");
-               echo ("(" . $record['offset'] . ", " . $record['size'] . ") " . $record['uid'] . ", " . $record['rule'] . ": " . $record['input'] . "\n");
+       if (is_array($records)) {
+               foreach ($records as $record) {
+                       if ($record['type'] & RPA_RECORD_START)
+                               echo("START ");
+                       else if ($record['type'] & RPA_RECORD_END)
+                               echo("END   ");
+                       else
+                               echo("UNKNOWN");
+                       echo ("(" . $record['offset'] . ", " . $record['size'] . ") " . $record['uid'] . ", " . $record['rule'] . ": " . $record['input'] . "\n");
+               }
        }
        unset($stat);
        unset($hDbex);
index 626936e..bd3a2e0 100644 (file)
@@ -19,14 +19,16 @@ rpaparse($bnf, $input, $records, $error);
        $matched = "matched: " . substr($myname, 0, $ret) . "\n";
        echo($matched);
        
-       foreach ($records as $record) {
-               if ($record['type'] & RPA_RECORD_START)
-                       echo("START ");
-               else if ($record['type'] & RPA_RECORD_END)
-                       echo("END   ");
-               else
-                       echo("UNKNOWN");
-               echo ($record['uid'] . ", " . $record['rule'] . ": " . $record['input'] . "\n");
+       if (is_array($records)) {
+               foreach ($records as $record) {
+                       if ($record['type'] & RPA_RECORD_START)
+                               echo("START ");
+                       else if ($record['type'] & RPA_RECORD_END)
+                               echo("END   ");
+                       else
+                               echo("UNKNOWN");
+                       echo ($record['uid'] . ", " . $record['rule'] . ": " . $record['input'] . "\n");
+               }
        }
 ?>
 --EXPECT--
diff --git a/phpext/prpa/tests/013.phpt b/phpext/prpa/tests/013.phpt
new file mode 100644 (file)
index 0000000..92208df
--- /dev/null
@@ -0,0 +1,74 @@
+--TEST--
+Parse .ini file
+--SKIPIF--
+<?php if (!extension_loaded("prpa")) echo "skip\n"; ?>
+--FILE--
+<?php 
+       $records = 0;
+       $error = 0;
+       $bnf = "#!emitnone\n" .
+       "#!emitid SectionName 1\n" .
+       "#!emitid Name 2\n" .
+       "#!emitid Value 3\n" .
+       "S                                      ::= [#0x0009] | [#0x000B] | [#0x000C] | [#0x0020] | [#0x00A0] | [#0xFEFF]\n" .
+       "EOL                            ::= [#0x000D] [#0x000A] | [#0x000A] | [#0x000D] | [#0x2028] | [#0x2029]\n" .
+       "SectionName            ::= [a-zA-Z][a-zA-Z0-9]*\n" .
+       "SectionLine            ::= '[' <SectionName> ']' <S>* \n" .
+       "Comment                        ::= ';' (. - <EOL>)+ \n" .      
+       "Name                           ::= [a-zA-Z][a-zA-Z0-9]* \n" .
+       "Value                          ::= (. - <EOL>)+ \n" .
+       "NameValue                      ::= <Name> <S>* '=' <S>* <Value>? \n" .
+       "Section                        ::= <SectionLine> (<NameValue>|<EOL>|<S>|<Comment>)*\n" .
+       "Ini                            ::= <Section>+\n";
+       
+
+       $inifile = "[PersonName]\n" .
+       "FirstName = John\n" .
+       "LastName = Smith\n\n" .
+       "[PersonAddress]\n" .           
+       "Street = NE 231\n" .
+       "; this is a comment\n" .
+       "City = Seattle\n" .
+       "State = Washington\n" .
+       "ZIP = 98115\n";
+
+
+       $ret = rpaparse($bnf, RPA_ENCODING_UTF8, $inifile, $records, $error);
+       if ($ret < 0)
+               die($error . "\n");
+       echo($inifile . "\n");
+       if (is_array($records)) {
+               foreach ($records as $record) {
+                       if ($record['type'] & RPA_RECORD_START) {
+                               echo("START ");
+                               echo ($record['uid'] . ", " . $record['rule'] . ": " . $record['input'] . "\n");
+                       }
+               }
+       }
+?>
+--EXPECT--
+[PersonName]
+FirstName = John
+LastName = Smith
+
+[PersonAddress]
+Street = NE 231
+; this is a comment
+City = Seattle
+State = Washington
+ZIP = 98115
+
+START 1, SectionName: PersonName
+START 2, Name: FirstName
+START 3, Value: John
+START 2, Name: LastName
+START 3, Value: Smith
+START 1, SectionName: PersonAddress
+START 2, Name: Street
+START 3, Value: NE 231
+START 2, Name: City
+START 3, Value: Seattle
+START 2, Name: State
+START 3, Value: Washington
+START 2, Name: ZIP
+START 3, Value: 98115