Changeset 215
- Timestamp:
- 07/29/06 18:10:26 (7 years ago)
- Location:
- libyaml/trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libyaml/trunk/src/api.c
r213 r215 630 630 (octet & 0xF0) == 0xE0 ? 3 : 631 631 (octet & 0xF8) == 0xF0 ? 4 : 0; 632 value = (octet & 0x80) == 0x00 ? octet & 0x7F :632 value = (octet & 0x80) == 0x00 ? octet & 0x7F : 633 633 (octet & 0xE0) == 0xC0 ? octet & 0x1F : 634 634 (octet & 0xF0) == 0xE0 ? octet & 0x0F : -
libyaml/trunk/src/emitter.c
r214 r215 53 53 #define WRITE_BREAK(emitter,string) \ 54 54 (FLUSH(emitter) \ 55 && (COPY(emitter->buffer,string), \ 56 emitter->column = 0, \ 57 emitter->line ++, \ 58 1)) 55 && (CHECK(string,'\n') ? \ 56 (PUT_BREAK(emitter), \ 57 string.pointer ++, \ 58 1) : \ 59 (COPY(emitter->buffer,string), \ 60 emitter->column = 0, \ 61 emitter->line ++, \ 62 1))) 59 63 60 64 /* … … 231 235 static int 232 236 yaml_emitter_write_tag_content(yaml_emitter_t *emitter, 233 yaml_char_t *value, size_t length );237 yaml_char_t *value, size_t length, int need_whitespace); 234 238 235 239 static int … … 244 248 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, 245 249 yaml_char_t *value, size_t length, int allow_breaks); 250 251 static int 252 yaml_emitter_determine_chomping(yaml_emitter_t *emitter, 253 yaml_string_t string); 246 254 247 255 static int … … 602 610 return 0; 603 611 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix, 604 strlen((char *)tag_directive->prefix) ))612 strlen((char *)tag_directive->prefix), 1)) 605 613 return 0; 606 614 if (!yaml_emitter_write_indent(emitter)) … … 1230 1238 if (emitter->tag_data.suffix) { 1231 1239 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, 1232 emitter->tag_data.suffix_length ))1240 emitter->tag_data.suffix_length, 0)) 1233 1241 return 0; 1234 1242 } … … 1239 1247 return 0; 1240 1248 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, 1241 emitter->tag_data.suffix_length ))1249 emitter->tag_data.suffix_length, 0)) 1242 1250 return 0; 1243 1251 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0)) … … 1787 1795 yaml_string_t string = STRING(value, length); 1788 1796 1797 if (!emitter->whitespace) { 1798 if (!PUT(emitter, ' ')) return 0; 1799 } 1800 1789 1801 while (string.pointer != string.end) { 1790 1802 if (!WRITE(emitter, string)) return 0; … … 1799 1811 static int 1800 1812 yaml_emitter_write_tag_content(yaml_emitter_t *emitter, 1801 yaml_char_t *value, size_t length) 1802 { 1803 return 0; 1813 yaml_char_t *value, size_t length, 1814 int need_whitespace) 1815 { 1816 yaml_string_t string = STRING(value, length); 1817 1818 if (need_whitespace && !emitter->whitespace) { 1819 if (!PUT(emitter, ' ')) return 0; 1820 } 1821 1822 while (string.pointer != string.end) { 1823 if (IS_ALPHA(string) 1824 || CHECK(string, ';') || CHECK(string, '/') 1825 || CHECK(string, '?') || CHECK(string, ':') 1826 || CHECK(string, '@') || CHECK(string, '&') 1827 || CHECK(string, '=') || CHECK(string, '+') 1828 || CHECK(string, '$') || CHECK(string, ',') 1829 || CHECK(string, '_') || CHECK(string, '.') 1830 || CHECK(string, '~') || CHECK(string, '*') 1831 || CHECK(string, '\'') || CHECK(string, '(') 1832 || CHECK(string, ')') || CHECK(string, '[') 1833 || CHECK(string, ']')) { 1834 if (!WRITE(emitter, string)) return 0; 1835 } 1836 else { 1837 int width = WIDTH(string); 1838 unsigned int value; 1839 while (width --) { 1840 value = *(string.pointer++); 1841 if (!PUT(emitter, '%')) return 0; 1842 if (!PUT(emitter, (value >> 8) 1843 + ((value >> 8) < 10 ? '0' : 'A' - 10))) 1844 return 0; 1845 if (!PUT(emitter, (value & 0x0F) 1846 + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) 1847 return 0; 1848 } 1849 } 1850 } 1851 1852 emitter->whitespace = 0; 1853 emitter->indention = 0; 1854 1855 return 1; 1804 1856 } 1805 1857 … … 1808 1860 yaml_char_t *value, size_t length, int allow_breaks) 1809 1861 { 1810 return 0; 1862 yaml_string_t string = STRING(value, length); 1863 int spaces = 0; 1864 int breaks = 0; 1865 1866 if (!emitter->whitespace) { 1867 if (!PUT(emitter, ' ')) return 0; 1868 } 1869 1870 while (string.pointer != string.end) 1871 { 1872 if (IS_SPACE(string)) 1873 { 1874 if (allow_breaks && !spaces 1875 && emitter->column > emitter->best_width 1876 && !IS_SPACE_AT(string, 1)) { 1877 if (!yaml_emitter_write_indent(emitter)) return 0; 1878 MOVE(string); 1879 } 1880 else { 1881 if (!WRITE(emitter, string)) return 0; 1882 } 1883 spaces = 1; 1884 } 1885 else if (IS_BREAK(string)) 1886 { 1887 if (!breaks && CHECK(string, '\n')) { 1888 if (!PUT_BREAK(emitter)) return 0; 1889 } 1890 if (!WRITE_BREAK(emitter, string)) return 0; 1891 emitter->indention = 1; 1892 breaks = 1; 1893 } 1894 else 1895 { 1896 if (breaks) { 1897 if (!yaml_emitter_write_indent(emitter)) return 0; 1898 } 1899 if (!WRITE(emitter, string)) return 0; 1900 emitter->indention = 0; 1901 spaces = 0; 1902 breaks = 0; 1903 } 1904 } 1905 1906 emitter->whitespace = 0; 1907 emitter->indention = 0; 1908 1909 return 1; 1811 1910 } 1812 1911 … … 1815 1914 yaml_char_t *value, size_t length, int allow_breaks) 1816 1915 { 1817 return 0; 1916 yaml_string_t string = STRING(value, length); 1917 int spaces = 0; 1918 int breaks = 0; 1919 1920 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) 1921 return 0; 1922 1923 while (string.pointer != string.end) 1924 { 1925 if (IS_SPACE(string)) 1926 { 1927 if (allow_breaks && !spaces 1928 && emitter->column > emitter->best_width 1929 && string.pointer != string.start 1930 && string.pointer != string.end - 1 1931 && !IS_SPACE_AT(string, 1)) { 1932 if (!yaml_emitter_write_indent(emitter)) return 0; 1933 MOVE(string); 1934 } 1935 else { 1936 if (!WRITE(emitter, string)) return 0; 1937 } 1938 spaces = 1; 1939 } 1940 else if (IS_BREAK(string)) 1941 { 1942 if (!breaks && CHECK(string, '\n')) { 1943 if (!PUT_BREAK(emitter)) return 0; 1944 } 1945 if (!WRITE_BREAK(emitter, string)) return 0; 1946 emitter->indention = 1; 1947 breaks = 1; 1948 } 1949 else 1950 { 1951 if (breaks) { 1952 if (!yaml_emitter_write_indent(emitter)) return 0; 1953 } 1954 if (CHECK(string, '\'')) { 1955 if (!PUT(emitter, '\'')) return 0; 1956 } 1957 if (!WRITE(emitter, string)) return 0; 1958 emitter->indention = 0; 1959 spaces = 0; 1960 breaks = 0; 1961 } 1962 } 1963 1964 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) 1965 return 0; 1966 1967 emitter->whitespace = 0; 1968 emitter->indention = 0; 1969 1970 return 1; 1818 1971 } 1819 1972 … … 1822 1975 yaml_char_t *value, size_t length, int allow_breaks) 1823 1976 { 1824 return 0; 1977 yaml_string_t string = STRING(value, length); 1978 int spaces = 0; 1979 1980 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) 1981 return 0; 1982 1983 while (string.pointer != string.end) 1984 { 1985 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) 1986 || IS_BOM(string) || IS_BREAK(string) 1987 || CHECK(string, '"') || CHECK(string, '\\')) 1988 { 1989 unsigned char octet; 1990 unsigned int width; 1991 unsigned int value; 1992 int k; 1993 1994 octet = string.pointer[0]; 1995 width = (octet & 0x80) == 0x00 ? 1 : 1996 (octet & 0xE0) == 0xC0 ? 2 : 1997 (octet & 0xF0) == 0xE0 ? 3 : 1998 (octet & 0xF8) == 0xF0 ? 4 : 0; 1999 value = (octet & 0x80) == 0x00 ? octet & 0x7F : 2000 (octet & 0xE0) == 0xC0 ? octet & 0x1F : 2001 (octet & 0xF0) == 0xE0 ? octet & 0x0F : 2002 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 2003 for (k = 1; k < width; k ++) { 2004 octet = string.pointer[k]; 2005 value = (value << 6) + (octet & 0x3F); 2006 } 2007 string.pointer += width; 2008 2009 if (!PUT(emitter, '\\')) return 0; 2010 2011 switch (value) 2012 { 2013 case 0x00: 2014 if (!PUT(emitter, '0')) return 0; 2015 break; 2016 2017 case 0x07: 2018 if (!PUT(emitter, 'a')) return 0; 2019 break; 2020 2021 case 0x08: 2022 if (!PUT(emitter, 'b')) return 0; 2023 break; 2024 2025 case 0x09: 2026 if (!PUT(emitter, 't')) return 0; 2027 break; 2028 2029 case 0x0A: 2030 if (!PUT(emitter, 'n')) return 0; 2031 break; 2032 2033 case 0x0B: 2034 if (!PUT(emitter, 'v')) return 0; 2035 break; 2036 2037 case 0x0C: 2038 if (!PUT(emitter, 'f')) return 0; 2039 break; 2040 2041 case 0x0D: 2042 if (!PUT(emitter, 'r')) return 0; 2043 break; 2044 2045 case 0x1B: 2046 if (!PUT(emitter, 'e')) return 0; 2047 break; 2048 2049 case 0x22: 2050 if (!PUT(emitter, '\"')) return 0; 2051 break; 2052 2053 case 0x5C: 2054 if (!PUT(emitter, '\\')) return 0; 2055 break; 2056 2057 case 0x85: 2058 if (!PUT(emitter, 'N')) return 0; 2059 break; 2060 2061 case 0xA0: 2062 if (!PUT(emitter, '_')) return 0; 2063 break; 2064 2065 case 0x2028: 2066 if (!PUT(emitter, 'L')) return 0; 2067 break; 2068 2069 case 0x2029: 2070 if (!PUT(emitter, 'P')) return 0; 2071 break; 2072 2073 default: 2074 if (value <= 0xFF) { 2075 if (!PUT(emitter, 'x')) return 0; 2076 width = 2; 2077 } 2078 else if (value <= 0xFFFF) { 2079 if (!PUT(emitter, 'u')) return 0; 2080 width = 4; 2081 } 2082 else { 2083 if (!PUT(emitter, 'U')) return 0; 2084 width = 8; 2085 } 2086 for (k = width*4; k >= 0; k -= 4) { 2087 if (!PUT(emitter, (value >> k) & 0x0F)) return 0; 2088 } 2089 } 2090 spaces = 0; 2091 } 2092 else if (IS_SPACE(string)) 2093 { 2094 if (allow_breaks && !spaces 2095 && emitter->column > emitter->best_width 2096 && string.pointer != string.start 2097 && string.pointer != string.end - 1) { 2098 if (!yaml_emitter_write_indent(emitter)) return 0; 2099 if (IS_SPACE_AT(string, 1)) { 2100 if (!PUT(emitter, '\\')) return 0; 2101 } 2102 MOVE(string); 2103 } 2104 else { 2105 if (!WRITE(emitter, string)) return 0; 2106 } 2107 spaces = 1; 2108 } 2109 else 2110 { 2111 if (!WRITE(emitter, string)) return 0; 2112 spaces = 0; 2113 } 2114 } 2115 2116 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) 2117 return 0; 2118 2119 emitter->whitespace = 0; 2120 emitter->indention = 0; 2121 2122 return 1; 2123 } 2124 2125 static int 2126 yaml_emitter_determine_chomping(yaml_emitter_t *emitter, 2127 yaml_string_t string) 2128 { 2129 string.pointer = string.end; 2130 if (string.start == string.pointer) 2131 return -1; 2132 while ((string.pointer[-1] & 0xC0) == 0x80) { 2133 string.pointer --; 2134 } 2135 if (!IS_BREAK(string)) 2136 return -1; 2137 if (string.start == string.pointer) 2138 return 0; 2139 while ((string.pointer[-1] & 0xC0) == 0x80) { 2140 string.pointer --; 2141 } 2142 if (!IS_BREAK(string)) 2143 return 0; 2144 return +1; 2145 1825 2146 } 1826 2147 … … 1829 2150 yaml_char_t *value, size_t length) 1830 2151 { 1831 return 0; 2152 yaml_string_t string = STRING(value, length); 2153 int chomp = yaml_emitter_determine_chomping(emitter, string); 2154 int breaks = 0; 2155 2156 if (!yaml_emitter_write_indicator(emitter, 2157 chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0)) 2158 return 0; 2159 if (!yaml_emitter_write_indent(emitter)) 2160 return 0; 2161 2162 while (string.pointer != string.end) 2163 { 2164 if (IS_BREAK(string)) 2165 { 2166 if (!WRITE_BREAK(emitter, string)) return 0; 2167 emitter->indention = 1; 2168 breaks = 1; 2169 } 2170 else 2171 { 2172 if (breaks) { 2173 if (!yaml_emitter_write_indent(emitter)) return 0; 2174 } 2175 if (!WRITE(emitter, string)) return 0; 2176 emitter->indention = 0; 2177 breaks = 0; 2178 } 2179 } 2180 2181 if (!yaml_emitter_write_indent(emitter)) return 0; 2182 2183 return 1; 1832 2184 } 1833 2185 … … 1836 2188 yaml_char_t *value, size_t length) 1837 2189 { 1838 return 0; 1839 } 1840 2190 yaml_string_t string = STRING(value, length); 2191 int chomp = yaml_emitter_determine_chomping(emitter, string); 2192 int breaks = 0; 2193 int leading_spaces = 1; 2194 2195 if (!yaml_emitter_write_indicator(emitter, 2196 chomp == -1 ? ">-" : chomp == +1 ? ">+" : ">", 1, 0, 0)) 2197 return 0; 2198 if (!yaml_emitter_write_indent(emitter)) 2199 return 0; 2200 2201 while (string.pointer != string.end) 2202 { 2203 if (IS_BREAK(string)) 2204 { 2205 if (!breaks && !leading_spaces && CHECK(string, '\n')) { 2206 int k = 0; 2207 while (IS_BREAK_AT(string, k)) { 2208 k += WIDTH_AT(string, k); 2209 } 2210 if (!IS_BLANK_AT(string, k)) { 2211 if (!PUT_BREAK(emitter)) return 0; 2212 } 2213 } 2214 if (!WRITE_BREAK(emitter, string)) return 0; 2215 emitter->indention = 1; 2216 breaks = 1; 2217 } 2218 else 2219 { 2220 if (breaks) { 2221 if (!yaml_emitter_write_indent(emitter)) return 0; 2222 leading_spaces = IS_BLANK(string); 2223 } 2224 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) 2225 && emitter->column > emitter->best_width) { 2226 if (!yaml_emitter_write_indent(emitter)) return 0; 2227 MOVE(string); 2228 } 2229 else { 2230 if (!WRITE(emitter, string)) return 0; 2231 } 2232 emitter->indention = 0; 2233 breaks = 0; 2234 } 2235 } 2236 2237 if (!yaml_emitter_write_indent(emitter)) return 0; 2238 2239 return 1; 2240 } 2241
Note: See TracChangeset
for help on using the changeset viewer.
