trunk/src/tools/srcclean.c
| r18371 | r18372 | |
| 67 | 67 | |
| 68 | 68 | int main(int argc, char *argv[]) |
| 69 | 69 | { |
| 70 | | int removed_tabs = 0, added_tabs = 0, removed_spaces = 0, fixed_mac_style = 0, fixed_nix_style = 0, added_newline = 0, removed_newlines = 0; |
| 71 | | int src = 0, dst = 0, in_c_comment = FALSE, in_cpp_comment = FALSE, in_c_string = FALSE; |
| 70 | int removed_tabs = 0; |
| 71 | int added_tabs = 0; |
| 72 | int removed_spaces = 0; |
| 73 | int fixed_mac_style = 0; |
| 74 | int fixed_nix_style = 0; |
| 75 | int added_newline = 0; |
| 76 | int removed_newlines = 0; |
| 77 | int src = 0; |
| 78 | int dst = 0; |
| 79 | bool in_c_comment = false; |
| 80 | bool in_cpp_comment = false; |
| 81 | int indent_c_comment = 0; |
| 82 | int in_c_string = FALSE; |
| 72 | 83 | int hichars = 0; |
| 73 | | int is_c_file, is_xml_file; |
| 84 | bool is_c_file; |
| 85 | bool is_xml_file; |
| 74 | 86 | const char *ext; |
| 75 | 87 | FILE *file; |
| 76 | 88 | int bytes; |
| r18371 | r18372 | |
| 134 | 146 | |
| 135 | 147 | /* track whether or not we are within a C-style comment */ |
| 136 | 148 | if (!in_c_comment && ch == '/' && original[src] == '*') |
| 137 | | in_c_comment = TRUE; |
| 149 | { |
| 150 | in_c_comment = true; |
| 151 | if (col > 0 && modified[dst-1] == 0x09) |
| 152 | { |
| 153 | indent_c_comment = col; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | indent_c_comment = 0; |
| 158 | } |
| 159 | } |
| 138 | 160 | else if (in_c_comment && ch == '*' && original[src] == '/') |
| 139 | | in_c_comment = FALSE; |
| 161 | { |
| 162 | in_c_comment = false; |
| 163 | indent_c_comment = 0; |
| 164 | } |
| 140 | 165 | |
| 141 | 166 | /* track whether or not we are within a C++-style comment */ |
| 142 | 167 | else if (!in_c_comment && ch == '/' && original[src] == '/') |
| 143 | | in_cpp_comment = TRUE; |
| 168 | in_cpp_comment = true; |
| 144 | 169 | else |
| 145 | 170 | consume = FALSE; |
| 146 | 171 | |
| r18371 | r18372 | |
| 183 | 208 | fixed_mac_style = 1; |
| 184 | 209 | |
| 185 | 210 | /* we are no longer in a C++-style comment */ |
| 186 | | in_cpp_comment = FALSE; |
| 211 | in_cpp_comment = false; |
| 187 | 212 | |
| 188 | 213 | if (in_c_string) |
| 189 | 214 | { |
| r18371 | r18372 | |
| 197 | 222 | { |
| 198 | 223 | int spaces = tab_size - (col % tab_size); |
| 199 | 224 | |
| 200 | | col += spaces; |
| 225 | /* convert tabs to spaces, if not used for indenting */ |
| 226 | if ((in_c_comment && col >= indent_c_comment) || (col != 0 && modified[dst-1] != 0x09)) |
| 227 | { |
| 228 | while (spaces > 0) |
| 229 | { |
| 230 | modified[dst++] = ' '; |
| 231 | col++; |
| 232 | spaces--; |
| 233 | } |
| 201 | 234 | |
| 202 | | /* if inside a comment or in the middle of a line, expand it */ |
| 203 | | if (in_c_comment || in_cpp_comment || (col - spaces > 0 && modified[dst-1] != 0x09)) |
| 204 | | { |
| 205 | | while (spaces--) modified[dst++] = ' '; |
| 206 | 235 | removed_tabs++; |
| 207 | 236 | } |
| 208 | 237 | else |
| 209 | 238 | { |
| 210 | 239 | modified[dst++] = ch; |
| 240 | col += spaces; |
| 211 | 241 | } |
| 212 | 242 | } |
| 213 | 243 | |
| 214 | | /* convert spaces to tabs at beginning of lines */ |
| 215 | | else if (ch == 0x20 && !in_c_comment && !in_cpp_comment && (col == 0 || modified[dst-1] == 0x09)) |
| 244 | /* if we hit a space... */ |
| 245 | else if (ch == 0x20) |
| 216 | 246 | { |
| 217 | 247 | int spaces = 1; |
| 218 | 248 | |
| r18371 | r18372 | |
| 230 | 260 | spaces -= realign; |
| 231 | 261 | } |
| 232 | 262 | |
| 233 | | while (spaces > 0) |
| 263 | /* convert spaces to tabs, if used for indenting */ |
| 264 | while (spaces > 0 && (!in_c_comment || col < indent_c_comment) && (col == 0 || modified[dst-1] == 0x09)) |
| 234 | 265 | { |
| 235 | 266 | modified[dst++] = 0x09; |
| 236 | 267 | spaces -= tab_size; |
| 237 | 268 | col += tab_size; |
| 238 | 269 | added_tabs++; |
| 239 | 270 | } |
| 271 | |
| 272 | while (spaces > 0) |
| 273 | { |
| 274 | modified[dst++] = ' '; |
| 275 | col++; |
| 276 | spaces--; |
| 277 | } |
| 240 | 278 | } |
| 241 | 279 | |
| 242 | 280 | /* otherwise, copy the source character */ |