| 1 | --- src/xml_edit.c 2006-10-09 10:50:18.000000000 +0200 |
|---|
| 2 | +++ src/xml_edit.c 2006-10-09 11:04:17.000000000 +0200 |
|---|
| 3 | @@ -59,7 +59,8 @@ |
|---|
| 4 | typedef struct _edOptions { /* Global 'edit' options */ |
|---|
| 5 | int noblanks; /* Remove insignificant spaces from XML tree */ |
|---|
| 6 | int preserveFormat; /* Preserve original XML formatting */ |
|---|
| 7 | - int omit_decl; /* Ommit XML declaration line <?xml version="1.0"?> */ |
|---|
| 8 | + int omit_decl; /* Omit XML declaration line <?xml version="1.0"?> */ |
|---|
| 9 | + int inplace; /* Edit file inplace (no output on stdout) */ |
|---|
| 10 | } edOptions; |
|---|
| 11 | |
|---|
| 12 | typedef edOptions *edOptionsPtr; |
|---|
| 13 | @@ -119,13 +120,14 @@ |
|---|
| 14 | " -P (or --pf) - preserve original formatting\n" |
|---|
| 15 | " -S (or --ps) - preserve non-significant spaces\n" |
|---|
| 16 | " -O (or --omit-decl) - omit XML declaration (<?xml ...?>)\n" |
|---|
| 17 | +" -L (or --inplace) - edit file inplace\n" |
|---|
| 18 | " -N <name>=<value> - predefine namespaces (name without \'xmlns:\')\n" |
|---|
| 19 | " ex: xsql=urn:oracle-xsql\n" |
|---|
| 20 | " Multiple -N options are allowed.\n" |
|---|
| 21 | -" -N options must be last global options.\n" |
|---|
| 22 | -" --help or -h - display help\n\n"; |
|---|
| 23 | +" -N options must be last global options.\n"; |
|---|
| 24 | |
|---|
| 25 | static const char edit_usage_str_3[] = |
|---|
| 26 | +" --help or -h - display help\n\n" |
|---|
| 27 | "where <action>\n" |
|---|
| 28 | " -d or --delete <xpath>\n" |
|---|
| 29 | " -i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value>\n" |
|---|
| 30 | @@ -171,6 +173,7 @@ |
|---|
| 31 | ops->noblanks = 1; |
|---|
| 32 | ops->omit_decl = 0; |
|---|
| 33 | ops->preserveFormat = 0; |
|---|
| 34 | + ops->inplace = 0; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | @@ -196,6 +199,10 @@ |
|---|
| 39 | { |
|---|
| 40 | ops->omit_decl = 1; |
|---|
| 41 | } |
|---|
| 42 | + else if (!strcmp(argv[i], "-L") || !strcmp(argv[i], "--inplace")) |
|---|
| 43 | + { |
|---|
| 44 | + ops->inplace = 1; |
|---|
| 45 | + } |
|---|
| 46 | else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h") || |
|---|
| 47 | !strcmp(argv[i], "-?") || !strcmp(argv[i], "-Z")) |
|---|
| 48 | { |
|---|
| 49 | @@ -842,6 +849,54 @@ |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | + * Output document |
|---|
| 54 | + */ |
|---|
| 55 | +void |
|---|
| 56 | +edOutput(const char* filename, edOptions g_ops) |
|---|
| 57 | +{ |
|---|
| 58 | + xmlDocPtr doc = xmlParseFile(filename); |
|---|
| 59 | + |
|---|
| 60 | + if (!doc) |
|---|
| 61 | + { |
|---|
| 62 | + edCleanupNSArr(ns_arr); |
|---|
| 63 | + xmlCleanupParser(); |
|---|
| 64 | + xmlCleanupGlobals(); |
|---|
| 65 | + exit(2); |
|---|
| 66 | + } |
|---|
| 67 | + |
|---|
| 68 | + edProcess(doc, ops, ops_count); |
|---|
| 69 | + |
|---|
| 70 | + /* Print out result */ |
|---|
| 71 | + if (!g_ops.omit_decl) |
|---|
| 72 | + { |
|---|
| 73 | + xmlSaveFormatFile(g_ops.inplace ? filename : "-", doc, 1); |
|---|
| 74 | + } |
|---|
| 75 | + else |
|---|
| 76 | + { |
|---|
| 77 | + int format = 1; |
|---|
| 78 | + int ret = 0; |
|---|
| 79 | + char *encoding = NULL; |
|---|
| 80 | + xmlOutputBufferPtr buf = NULL; |
|---|
| 81 | + xmlCharEncodingHandlerPtr handler = NULL; |
|---|
| 82 | + buf = xmlOutputBufferCreateFilename(g_ops.inplace ? filename : "-", handler, 0); |
|---|
| 83 | + |
|---|
| 84 | + if (doc->children != NULL) |
|---|
| 85 | + { |
|---|
| 86 | + xmlNodePtr child = doc->children; |
|---|
| 87 | + while (child != NULL) |
|---|
| 88 | + { |
|---|
| 89 | + xmlNodeDumpOutput(buf, doc, child, 0, format, encoding); |
|---|
| 90 | + xmlOutputBufferWriteString(buf, "\n"); |
|---|
| 91 | + child = child->next; |
|---|
| 92 | + } |
|---|
| 93 | + } |
|---|
| 94 | + ret = xmlOutputBufferClose(buf); |
|---|
| 95 | + } |
|---|
| 96 | + |
|---|
| 97 | + xmlFreeDoc(doc); |
|---|
| 98 | +} |
|---|
| 99 | + |
|---|
| 100 | +/** |
|---|
| 101 | * This is the main function for 'edit' option |
|---|
| 102 | */ |
|---|
| 103 | int |
|---|
| 104 | @@ -1059,90 +1114,12 @@ |
|---|
| 105 | |
|---|
| 106 | if (i >= argc) |
|---|
| 107 | { |
|---|
| 108 | - xmlDocPtr doc = xmlParseFile("-"); |
|---|
| 109 | - |
|---|
| 110 | - if (!doc) |
|---|
| 111 | - { |
|---|
| 112 | - edCleanupNSArr(ns_arr); |
|---|
| 113 | - xmlCleanupParser(); |
|---|
| 114 | - xmlCleanupGlobals(); |
|---|
| 115 | - exit(2); |
|---|
| 116 | - } |
|---|
| 117 | - |
|---|
| 118 | - edProcess(doc, ops, ops_count); |
|---|
| 119 | - |
|---|
| 120 | - /* Print out result */ |
|---|
| 121 | - if (!g_ops.omit_decl) |
|---|
| 122 | - { |
|---|
| 123 | - xmlSaveFormatFile("-", doc, 1); |
|---|
| 124 | - } |
|---|
| 125 | - else |
|---|
| 126 | - { |
|---|
| 127 | - int format = 1; |
|---|
| 128 | - int ret = 0; |
|---|
| 129 | - char *encoding = NULL; |
|---|
| 130 | - xmlOutputBufferPtr buf = NULL; |
|---|
| 131 | - xmlCharEncodingHandlerPtr handler = NULL; |
|---|
| 132 | - buf = xmlOutputBufferCreateFile(stdout, handler); |
|---|
| 133 | - |
|---|
| 134 | - if (doc->children != NULL) |
|---|
| 135 | - { |
|---|
| 136 | - xmlNodePtr child = doc->children; |
|---|
| 137 | - while (child != NULL) |
|---|
| 138 | - { |
|---|
| 139 | - xmlNodeDumpOutput(buf, doc, child, 0, format, encoding); |
|---|
| 140 | - xmlOutputBufferWriteString(buf, "\n"); |
|---|
| 141 | - child = child->next; |
|---|
| 142 | - } |
|---|
| 143 | - } |
|---|
| 144 | - ret = xmlOutputBufferClose(buf); |
|---|
| 145 | - } |
|---|
| 146 | - |
|---|
| 147 | - xmlFreeDoc(doc); |
|---|
| 148 | + edOutput("-", g_ops); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | for (n=i; n<argc; n++) |
|---|
| 152 | { |
|---|
| 153 | - xmlDocPtr doc = xmlParseFile(argv[n]); |
|---|
| 154 | - |
|---|
| 155 | - if (!doc) |
|---|
| 156 | - { |
|---|
| 157 | - edCleanupNSArr(ns_arr); |
|---|
| 158 | - xmlCleanupParser(); |
|---|
| 159 | - xmlCleanupGlobals(); |
|---|
| 160 | - exit(2); |
|---|
| 161 | - } |
|---|
| 162 | - |
|---|
| 163 | - edProcess(doc, ops, ops_count); |
|---|
| 164 | - |
|---|
| 165 | - /* Print out result */ |
|---|
| 166 | - if (!g_ops.omit_decl) |
|---|
| 167 | - { |
|---|
| 168 | - xmlSaveFormatFile("-", doc, 1); |
|---|
| 169 | - } |
|---|
| 170 | - else |
|---|
| 171 | - { |
|---|
| 172 | - int format = 1; |
|---|
| 173 | - int ret = 0; |
|---|
| 174 | - char *encoding = NULL; |
|---|
| 175 | - xmlOutputBufferPtr buf = NULL; |
|---|
| 176 | - xmlCharEncodingHandlerPtr handler = NULL; |
|---|
| 177 | - buf = xmlOutputBufferCreateFile(stdout, handler); |
|---|
| 178 | - |
|---|
| 179 | - if (doc->children != NULL) |
|---|
| 180 | - { |
|---|
| 181 | - xmlNodePtr child = doc->children; |
|---|
| 182 | - while (child != NULL) |
|---|
| 183 | - { |
|---|
| 184 | - xmlNodeDumpOutput(buf, doc, child, 0, format, encoding); |
|---|
| 185 | - xmlOutputBufferWriteString(buf, "\n"); |
|---|
| 186 | - child = child->next; |
|---|
| 187 | - } |
|---|
| 188 | - } |
|---|
| 189 | - ret = xmlOutputBufferClose(buf); |
|---|
| 190 | - } |
|---|
| 191 | - |
|---|
| 192 | - xmlFreeDoc(doc); |
|---|
| 193 | + edOutput(argv[n], g_ops); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | edCleanupNSArr(ns_arr); |
|---|
| 197 | |
|---|
| 198 | |
|---|