/[jscoverage]/trunk/instrument.c
ViewVC logotype

Diff of /trunk/instrument.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by siliconforks, Wed Aug 1 13:51:53 2007 UTC revision 116 by siliconforks, Sat May 31 21:42:36 2008 UTC
# Line 1  Line 1 
1  /*  /*
2      instrument.c - file and directory instrumentation routines      instrument.c - file and directory instrumentation routines
3      Copyright (C) 2007 siliconforks.com      Copyright (C) 2007, 2008 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by      it under the terms of the GNU General Public License as published by
# Line 17  Line 17 
17      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */  */
19    
20    #include <config.h>
21    
22  #include "instrument.h"  #include "instrument.h"
23    
24  #include <assert.h>  #include <assert.h>
# Line 54  Line 56 
56    }    }
57  }  }
58    
 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, "<html><head><title>%s</title>\n", relative_path);  
   fprintf(output, "<link rel=\"stylesheet\" type='text/css' href='%sjscoverage.css'>\n", relative_path_to_ancestor);  
   fprintf(output, "<link rel=\"stylesheet\" type='text/css' href='%ssh_nedit.css'>\n", relative_path_to_ancestor);  
   fprintf(output, "<script src=\"%sjscoverage.js\"></script>\n", relative_path_to_ancestor);  
   fprintf(output, "<script src=\"%ssh_main.min.js\"></script>\n", relative_path_to_ancestor);  
   fprintf(output, "<script src=\"%ssh_javascript.min.js\"></script>\n", relative_path_to_ancestor);  
   fprintf(output, "<script>\n");  
   fprintf(output, "var gCurrentFile = \"%s\";\n", relative_path);  
   fprintf(output, "</script>\n");  
   fprintf(output, "</head><body onload=\"source_load();\">\n");  
   fprintf(output, "<h1>%s</h1>\n", relative_path);  
   fprintf(output, "<pre id=\"sourceDiv\" class='sh_%s'>", 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, "&lt;");  
     }  
     else if (c == '>') {  
       fprintf(output, "&gt;");  
     }  
     else if (c == '&') {  
       fprintf(output, "&amp;");  
     }  
     else {  
       if (c == '\n') {  
         line++;  
         atLineStart = 1;  
       }  
       fputc(c, output);  
     }  
   }  
   fprintf(output, "</pre></body></html>\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);  
 }  
   
59  static void check_same_file(const char * file1, const char * file2) {  static void check_same_file(const char * file1, const char * file2) {
60    if (is_same_file(file1, file2)) {    if (is_same_file(file1, file2)) {
61      fatal("source and destination are the same");      fatal("source and destination are the same");
# Line 162  Line 88 
88        copy_file(source_file, destination_file);        copy_file(source_file, destination_file);
89        break;        break;
90      case FILE_TYPE_JS:      case FILE_TYPE_JS:
       highlight_file(source_file, destination_file, id);  
91        {        {
92          FILE * input = xfopen(source_file, "r");          FILE * input = xfopen(source_file, "r");
93          FILE * output = xfopen(destination_file, "w");          FILE * output = xfopen(destination_file, "w");
94          jscoverage_instrument_js(id, input, output);  
95            Stream * input_stream = Stream_new(0);
96            Stream * output_stream = Stream_new(0);
97    
98            Stream_write_file_contents(input_stream, input);
99    
100            jscoverage_instrument_js(id, input_stream, output_stream);
101    
102            if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) {
103              fatal("cannot write to file: %s", destination_file);
104            }
105    
106            Stream_delete(input_stream);
107            Stream_delete(output_stream);
108    
109          fclose(input);          fclose(input);
110          fclose(output);          fclose(output);
111        }        }
# Line 225  Line 164 
164      fatal("cannot stat directory: %s", destination);      fatal("cannot stat directory: %s", destination);
165    }    }
166    
167      /* copy the resources */
168      jscoverage_copy_resources(destination);
169    
170    /* finally: copy the directory */    /* finally: copy the directory */
171    struct DirListEntry * list = make_recursive_dir_list(source);    struct DirListEntry * list = make_recursive_dir_list(source);
172    for (struct DirListEntry * p = list; p != NULL; p = p->next) {    for (struct DirListEntry * p = list; p != NULL; p = p->next) {
# Line 264  Line 206 
206    }    }
207    free_dir_list(list);    free_dir_list(list);
208  }  }
   
 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("sh_main.min.js", destination_directory);  
   copy_resource("sh_javascript.min.js", destination_directory);  
   copy_resource("sh_nedit.css", destination_directory);  
 }  

Legend:
Removed from v.2  
changed lines
  Added in v.116

  ViewVC Help
Powered by ViewVC 1.1.24