1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 203 204 205 206 207 209 210 211 212 213 214 215 216 217 218 219 221 222 223 224 |
? PHP/** * PropertyList class * Implements writing Apple Property List (. plist) XML and text files to an array. * * @author Jesus A. Alvarez <zydeco@namedfork.net>/function Plist_encode_text ($obj) {$plist = new propertylist ($obj); return $plist->text (); function Plist_encode_xml ($obj) {$plist = new propertylist ($obj); return $plist->xml ();} class PropertyList {Priv Ate $obj, $xml, $text; Public function __construct ($obj) {$this->obj = $obj.} private static function Is_assoc ($array) {return (Is_array ( $array) && 0!== count (Array_diff_key ($array, Array_keys (Array_keys ($array)))); Public function XML () {if (isset $this->xml)) return $this->xml; $x = new XMLWriter (); $x->openmemory (); $x-& Gt;setindent (TRUE); $x->startdocument (' 1.0 ', ' UTF-8 '); $x->writedtd (' plist ', '-//apple//dtd plist 1.0//en ', ' http://www.apple.com/DTDs/PropertyList-1.0.dtd '); $x->startelement (' plist '); $x->writeattribute (' Version ', ' 1.0 '); $this->xMlwritevalue ($x, $this->obj); $x->endelement (); plist $x->enddocument (); $this->xml = $x->outputmemory (); return $this->xml; Public Function text () {if (Isset ($this->text)) return $this->text; $text = '; $this->textwritevalue ($text, $ This->obj); $this->text = $text; return $this->text; Private Function Xmlwritedict (XMLWriter $x, & $dict) {$x->startelement (' dict '); foreach ($dict as $k => & $v {$x->writeelement (' key ', $k); $this->xmlwritevalue ($x, $v);} $x->endelement (); Dict} Private Function Xmlwritearray (XMLWriter $x, & $arr) {$x->startelement (' array '); foreach ($arr as & $v) $this->xmlwritevalue ($x, $v); $x->endelement (); Array} Private Function Xmlwritevalue (XMLWriter $x, & $v) {if (Is_int ($v) | | is_long ($V)) $x->writeelement (' int Eger ', $v); ElseIf (Is_float ($v) | | | is_real ($v) | | is_double ($V)) $x->writeelement (' real ', $v); ElseIf (is_string ($v)) $x->writeelement (' string ', $v);ElseIf (Is_bool ($v)) $x->writeelement ($v? ') True ': ' false '); ElseIf (PROPERTYLIST::IS_ASSOC ($v)) $this->xmlwritedict ($x, $v); ElseIf (Is_array ($v)) $this->xmlwritearray ($x, $v); ElseIf (Is_a ($v, ' Plistdata ')) $x->writeelement (' data ', $v->base64encodeddata ()); ElseIf (Is_a ($v, ' plistdate ')) $x->writeelement (' Date ', $v->encodeddate ()); else {trigger_error ("Unsupported data type in plist ($v)", e_user_warning); $x->writeelement (' string ', $v);} Private Function Textwritevalue (& $text, & $v, $indentLevel = 0) {if (Is_int ($v) | | is_long ($V)) $text. = sprintf (" %d ", $v); ElseIf (Is_float ($v) | | | | is_real ($v) | | is_double ($V)) $text. = sprintf ("%g", $v); ElseIf (is_string ($v)) $this->textwritestring ($text, $v); ElseIf (Is_bool ($v)) $text. = $v? ' YES ': ' NO '; ElseIf (PROPERTYLIST::IS_ASSOC ($v)) $this->textwritedict ($text, $v, $indentLevel); ElseIf (Is_array ($v)) $this->textwritearray ($text, $v, $indentLevel); ElseIf (Is_a ($v, ' Plistdata ')) $text. = ' < '. $v->hexencodeddata (). ' > '; ElseIf (Is_a ($v, ' plistdate ')) $text. = ' "'. $v->iso8601date (). '"'; else {trigger_error ("Unsupported data type in plist ($v)", e_user_warning); $this->textwritestring ($text, $v);} Private Function textwritestring (& $text, & $str) {$oldlocale = setlocale (Lc_ctype, "0"); if (Ctype_alnum ($STR)) $ text. = $str; else $text. = ' "'. $this->textencodestring ($STR). '"'; SetLocale (Lc_ctype, $oldlocale); Private Function textencodestring (& $str) {$newstr = '; $i = 0; $len = strlen ($STR); while ($i < $len) {$ch = or D (substr ($str, $i, 1)); if ($ch = = 0x22 | | $ch = = 0x5c) {//escape double quote, backslash $newstr. = '. Chr ($ch); $i + +} else if ($ch >= 0 x07 && $ch <= 0x0D) {//control characters with escape sequences $newstr. = '. substr (' Abtnvfr ', $ch-7, 1) ; $i + +; else if ($ch <) {//other non-printable characters escaped as Unicode $newstr. = sprintf (' u%04x ', $ch); $i + +;} E LSE if ($ch < 128) {//ASCII printable $newstr. = Chr ($ch); $i + +; else if ($ch = = 193) {//invalid encoding of ASCII characters $i + +;} else if (($ch & 0xc0) = = 0x80) { Part of a lost multibyte sequence, skip $i + +; else if (($ch & 0xe0) = = 0xc0) {//u+0080-u+07ff (2 bytes) $u = (($ch & 0x1F) << 6) | (Ord (substr ($str, $i +1, 1)) & 0x3F); $newstr. = sprintf (' u%04x ', $u); $i + 2; else if (($ch & 0xF0) = = 0xe0) {//U+0800-u+ffff (3 bytes) $u = (($ch & 0x0f) << 12) | ((Ord (substr ($str, $i +1, 1)) & 0x3F) << 6) | (Ord (substr ($str, $i +2, 1)) & 0x3F); $newstr. = sprintf (' u%04x ', $u); $i + 3; else if (($ch & 0xf8) = = 0xF0) {//U+10000-u+3ffff (4 bytes) $u = (($ch & 0x07) << 18) | ((Ord (substr ($str, $i +1, 1)) & 0x3F) << 12) | ((Ord (substr ($str, $i +2, 1)) & 0x3F) << 6) | (Ord (substr ($str, $i +3, 1)) & 0x3F); $newstr. = sprintf (' u%04x ', $u); $i + 4; else {//5 and 6 byte sequences are not valid UTF-8 $i + +; } return $NEWSTR; Private Function Textwritedict (& $text, & $dict, $indentLevel) {if (count ($dict) = 0) {$text. = ' {} '; $text. = "{n"; $indent = '; $indentLevel + + while (strlen ($indent) < $indentLevel) $indent. = "T"; foreach ($dict as $k => & $v) {$text. = $indent; $this->textwritevalue ($text, $k); $text. = ' = '; $this->textwritevalue ($text, $ V, $indentLevel); $text. = "; n"; $text. = substr ($indent, 0,-1). '}'; Private Function Textwritearray (& $text, & $arr, $indentLevel) {if (count ($arr) = 0) {$text. = ' () '; return;} $text. = "(n"; $indent = '; $indentLevel + + while (strlen ($indent) < $indentLevel) $indent. = "T"; foreach ($arr as &am P $v) {$text. = $indent; $this->textwritevalue ($text, $v, $indentLevel); $text. = ", n";} $text. = substr ($indent, 0, -1). ')'; } class Plistdata {private $data; public function __construct ($str) {$this->data = $str;} public Function Base64en Codeddata () {return Base64_encOde ($this->data); The Public Function Hexencodeddata () {$len = strlen ($this->data); $hexstr = "for ($i = 0; $i < $len; $i + 4) $hex str. = Bin2Hex (substr ($this->data, $i, 4)). ' '; Return substr ($hexstr, 0,-1); Class Plistdate {private $dateval; public function __construct ($init = NULL) {if (Is_int ($init)) $this->dateval = $init; ElseIf (is_string ($init)) $this->dateval = Strtotime ($init); ElseIf ($init = = NULL) $this->dateval = time (); The Public Function iso8601date () {return gmdate (' Y-m-dth:i:sz ', $this->dateval);} ?> |