--- trunk/instrument.c 2008/05/05 20:05:27 87 +++ trunk/instrument.c 2008/09/21 18:35:21 179 @@ -17,6 +17,8 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include + #include "instrument.h" #include @@ -27,6 +29,8 @@ #include #include +#include "encoding.h" +#include "global.h" #include "instrument-js.h" #include "resource-manager.h" #include "util.h" @@ -50,86 +54,10 @@ return FILE_TYPE_HTML; } else { - return FILE_TYPE_UNKNOWN; + return FILE_TYPE_OTHER; } } -static void highlight_file(const char * source_file, const char * destination_file, const char * relative_path) { - int depth = 0; - for (const char * p = relative_path; *p != '\0'; p++) { - if (*p == '/' || *p == '\\') { - depth++; - } - } - - enum FileType file_type = get_file_type(relative_path); - const char * suffix = ".jscoverage.html"; - char * highlighted_file = xmalloc(strlen(destination_file) + strlen(suffix) + 1); - strcpy(highlighted_file, destination_file); - strcat(highlighted_file, suffix); - - FILE * input = xfopen(source_file, "r"); - FILE * output = xfopen(highlighted_file, "w"); - - free(highlighted_file); - - char * relative_path_to_ancestor = xmalloc(depth * 3 + 1); - for (int i = 0; i < depth; i++) { - strcpy(relative_path_to_ancestor + i * 3, "../"); - } - relative_path_to_ancestor[depth * 3] = '\0'; - - fprintf(output, "%s\n", relative_path); - fprintf(output, "\n", relative_path_to_ancestor); - fprintf(output, "\n", relative_path_to_ancestor); - fprintf(output, "\n", relative_path_to_ancestor); - fprintf(output, "\n", relative_path_to_ancestor); - fprintf(output, "\n", relative_path_to_ancestor); - fprintf(output, "\n"); - fprintf(output, "\n"); - fprintf(output, "

%s

\n", relative_path); - fprintf(output, "
", file_type == FILE_TYPE_JS? "javascript": "html");
-  free(relative_path_to_ancestor);
-
-  int c;
-  int atLineStart = 1;
-  int line = 1;
-  while ((c = fgetc(input)) != EOF) {
-    if (atLineStart) {
-      atLineStart = 0;
-    }
-
-    if (c == '<') {
-      fprintf(output, "<");
-    }
-    else if (c == '>') {
-      fprintf(output, ">");
-    }
-    else if (c == '&') {
-      fprintf(output, "&");
-    }
-    else {
-      if (c == '\n') {
-        line++;
-        atLineStart = 1;
-      }
-      fputc(c, output);
-    }
-  }
-  fprintf(output, "
\n"); - - fclose(output); - fclose(input); - - suffix = ".jscoverage.js"; - char original_file[strlen(destination_file) + strlen(suffix) + 1]; - strcpy(original_file, destination_file); - strcat(original_file, suffix); - copy_file(source_file, original_file); -} - static void check_same_file(const char * file1, const char * file2) { if (is_same_file(file1, file2)) { fatal("source and destination are the same"); @@ -157,7 +85,7 @@ if (instrumenting) { enum FileType file_type = get_file_type(source_file); switch (file_type) { - case FILE_TYPE_UNKNOWN: + case FILE_TYPE_OTHER: case FILE_TYPE_HTML: copy_file(source_file, destination_file); break; @@ -165,15 +93,34 @@ { FILE * input = xfopen(source_file, "r"); FILE * output = xfopen(destination_file, "w"); - const char * suffix = ".jscoverage.js"; - char temporary_file_name[strlen(destination_file) + strlen(suffix) + 1]; - strcpy(temporary_file_name, destination_file); - strcat(temporary_file_name, suffix); - jscoverage_instrument_js(id, input, output, temporary_file_name); + + Stream * input_stream = Stream_new(0); + Stream * output_stream = Stream_new(0); + + Stream_write_file_contents(input_stream, input); + + size_t num_characters = input_stream->length; + uint16_t * characters = NULL; + int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters); + if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { + fatal("encoding %s not supported", jscoverage_encoding); + } + else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { + fatal("error decoding %s in file %s", jscoverage_encoding, id); + } + jscoverage_instrument_js(id, characters, num_characters, output_stream); + free(characters); + + if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) { + fatal("cannot write to file: %s", destination_file); + } + + Stream_delete(input_stream); + Stream_delete(output_stream); + fclose(input); fclose(output); } - highlight_file(source_file, destination_file, id); break; } } @@ -271,13 +218,3 @@ } free_dir_list(list); } - -void jscoverage_copy_resources(const char * destination_directory) { - copy_resource("jscoverage.html", destination_directory); - copy_resource("jscoverage.css", destination_directory); - copy_resource("jscoverage.js", destination_directory); - copy_resource("jscoverage-throbber.gif", destination_directory); - copy_resource("jscoverage-sh_main.js", destination_directory); - copy_resource("jscoverage-sh_javascript.js", destination_directory); - copy_resource("jscoverage-sh_nedit.css", destination_directory); -}