Parcourir la Source

files read by chunks to improve performance

bmallred 12 ans auparavant
Parent
Commettre
49d7954e24
3 fichiers modifiés avec 231 ajouts et 44 suppressions
  1. 161 2
      hexmeh.sublime-workspace
  2. 3 3
      partials/dump.html
  3. 67 39
      scripts/controllers.js

+ 161 - 2
hexmeh.sublime-workspace

@ -3,6 +3,18 @@
3 3
	{
4 4
		"selected_items":
5 5
		[
6
			[
7
				"file",
8
				"file"
9
			],
10
			[
11
				"read",
12
				"readBlob"
13
			],
14
			[
15
				"otp",
16
				"outputLine"
17
			],
6 18
			[
7 19
				"binary",
8 20
				"binaryLength"
@ -15,6 +27,41 @@
15 27
	},
16 28
	"buffers":
17 29
	[
30
		{
31
			"file": "partials/dump.html",
32
			"settings":
33
			{
34
				"buffer_size": 592,
35
				"line_ending": "Unix"
36
			}
37
		},
38
		{
39
			"file": "index.html",
40
			"settings":
41
			{
42
				"buffer_size": 1526,
43
				"line_ending": "Unix"
44
			}
45
		},
46
		{
47
			"contents": "'use strict';\n\nfunction MainController($scope, $routeParams) {\n  $scope.allowedLengths = [4, 8, 16, 32, 64, 128, 256];\n  $scope.binaryLength = 64;\n  $scope.binary = generateBinary($scope.binaryLength);\n  $scope.parts = [];\n  $scope.hex = \"\";\n  \n  $scope.displayResults = function () {\n    return $scope.hex.length > 0;\n  };\n  \n  $scope.generate = function () {\n    $scope.binary = generateBinary($scope.binaryLength);\n    $scope.parts = [];\n    $scope.hex = \"\";\n    \n    return false;\n  };\n  \n  $scope.solve = function () {\n    $scope.parts = [];\n    $scope.hex = \"\";\n    \n    var original = $scope.binary;\n    \n    while (original.length > 0) {\n      // Find the 16 bytes we need.\n      var part = original.substr(-4);\n      var character = binaryToHex(part);\n      \n      // Divvy this stuff up.\n      $scope.parts.push({ \"binary\": part, \"hex\": character });\n      $scope.hex = character + $scope.hex;\n      \n      // Remove the last four.\n      original = original.substr(0, original.length - 4);\n    }\n    \n    return false;\n  };\n}\n\nfunction DumpController($scope, $routeParams) {\n  $scope.columns = 16;\n  $scope.fileApi = false;\n  $scope.instructions = \"Drop a file here!\";\n  $scope.uppercaseHex = false;\n  $scope.file = null;\n  $scope.lastLine = 0;\n  \n  $scope.isProcessing = function () {\n\n  };\n\n  function outputLine(bytes) {\n    // Add a break line.\n    $(\"#lineNumbers, #lineHex, #lineCharacters\").append(\"<br>\");\n    \n    // Get the line number.\n    var lineNumber = padLeft($scope.lastLine.toString(16), 6);\n    if ($scope.uppercaseHex) {\n      lineNumber = lineNumber.toUpperCase();\n    }\n    $(\"#lineNumbers\").append(lineNumber + \"<span class='symbol'>:</span>\");\n    \n    for (var i = 0; i < bytes.length; i++) {\n      // Get the hex string.\n      var hexString = padLeft(bytes[i].toString(16), 2);\n      if ($scope.uppercaseHex) {\n          hexString = hexString.toUpperCase();\n      }\n      \n      // Output the remaining lines.\n      $(\"#lineHex\").append(hexString + \"&nbsp;\");\n      $(\"#lineCharacters\").append(String.fromCharCode(bytes[i]));\n    }\n\n    // Increment to next line.\n    $scope.lastLine++;\n  }\n\n  function readBlob(start, stop) {\n    if (start < $scope.file.size) {\n      var reader = new FileReader();\n      \n      // Closure to capture the file information.\n      reader.onload = function(evt) {\n        var bytes = new Uint8Array(evt.target.result);\n        outputLine(bytes);\n        readBlob(stop, stop + $scope.columns);\n      }\n\n      var blob = $scope.file.webkitSlice(start, stop);\n      reader.readAsArrayBuffer(blob);\n    }\n  }\n  \n  // Check for the various File API support.\n  if (window.File && window.FileReader && window.FileList && window.Blob) {\n    // Great success! All the File APIs are supported.\n    $(\"#drop-zone\").bind(\"dragover\", function (e) {\n      e.stopPropagation();\n      e.preventDefault();\n      e.originalEvent.dataTransfer.dropEffect = 'copy';\n    });\n    \n    $(\"#drop-zone\").bind(\"drop\", function(e) {\n      e.stopPropagation();\n      e.preventDefault();\n      \n      // Remove previous output.\n      $(\"#lineNumbers, #lineHex, #lineCharacters\").empty();\n      $scope.lastLine = 0;\n      $scope.file = null;\n\n      //$(\"#drop-zone\").append($(\"<div id='progress_bar'><div class='percent'>0%</div></div>\"));\n      //var progress = $(\".percent\");\n      \n      // Retrieve the files.\n      var files = e.originalEvent.dataTransfer.files;\n      if (files) {\n        $scope.file = files[0];\n        readBlob(0, $scope.columns);\n      }\n    });\n  } \n  else {\n    $scope.fileApi = false;\n    $scope.instructions = \"Sorry, your browser needs to go to an old folks home.\";\n    alert('The File APIs are not fully supported in this browser.');\n  }\n}",
48
			"file": "scripts/controllers.js",
49
			"file_size": 3704,
50
			"file_write_time": 1348693725000000,
51
			"settings":
52
			{
53
				"buffer_size": 3707,
54
				"line_ending": "Unix"
55
			}
56
		},
57
		{
58
			"file": "styles/hexmeh.css",
59
			"settings":
60
			{
61
				"buffer_size": 10926,
62
				"line_ending": "Unix"
63
			}
64
		}
18 65
	],
19 66
	"build_system": "",
20 67
	"command_palette":
@ -40,6 +87,8 @@
40 87
	},
41 88
	"file_history":
42 89
	[
90
		"/home/bryan/Projects/hexmeh/scripts/app.js",
91
		"/home/bryan/Projects/hexmeh/scripts/components.js",
43 92
		"/home/bryan/Projects/hexmeh/scripts/utilities.js",
44 93
		"/home/bryan/Projects/hexmeh/styles/hexmeh.css",
45 94
		"/home/bryan/Projects/hexmeh/index.html",
@ -47,7 +96,6 @@
47 96
		"/home/bryan/Projects/hexmeh/partials/intro.html",
48 97
		"/home/bryan/Projects/hexmeh/scripts/services.js",
49 98
		"/home/bryan/Projects/hexmeh/crossdomain.xml",
50
		"/home/bryan/Projects/hexmeh/scripts/app.js",
51 99
		"/home/bryan/Projects/hexmeh/.gitignore",
52 100
		"/home/bryan/Projects/hexmeh/README.md",
53 101
		"/home/bryan/Projects/whistle/scripts/app.js",
@ -89,8 +137,119 @@
89 137
	"groups":
90 138
	[
91 139
		{
140
			"selected": 2,
92 141
			"sheets":
93 142
			[
143
				{
144
					"buffer": 0,
145
					"file": "partials/dump.html",
146
					"settings":
147
					{
148
						"buffer_size": 592,
149
						"regions":
150
						{
151
						},
152
						"selection":
153
						[
154
							[
155
								488,
156
								488
157
							]
158
						],
159
						"settings":
160
						{
161
							"syntax": "Packages/HTML/HTML.tmLanguage",
162
							"tab_size": 4,
163
							"translate_tabs_to_spaces": true
164
						},
165
						"translation.x": 0.0,
166
						"translation.y": 0.0,
167
						"zoom_level": 1.0
168
					},
169
					"type": "text"
170
				},
171
				{
172
					"buffer": 1,
173
					"file": "index.html",
174
					"settings":
175
					{
176
						"buffer_size": 1526,
177
						"regions":
178
						{
179
						},
180
						"selection":
181
						[
182
							[
183
								0,
184
								0
185
							]
186
						],
187
						"settings":
188
						{
189
							"syntax": "Packages/HTML/HTML.tmLanguage",
190
							"tab_size": 4,
191
							"translate_tabs_to_spaces": true
192
						},
193
						"translation.x": 0.0,
194
						"translation.y": 0.0,
195
						"zoom_level": 1.0
196
					},
197
					"type": "text"
198
				},
199
				{
200
					"buffer": 2,
201
					"file": "scripts/controllers.js",
202
					"settings":
203
					{
204
						"buffer_size": 3707,
205
						"regions":
206
						{
207
						},
208
						"selection":
209
						[
210
							[
211
								1296,
212
								1296
213
							]
214
						],
215
						"settings":
216
						{
217
							"syntax": "Packages/JavaScript/JavaScript.tmLanguage",
218
							"tab_size": 2,
219
							"translate_tabs_to_spaces": true
220
						},
221
						"translation.x": 0.0,
222
						"translation.y": 833.0,
223
						"zoom_level": 1.0
224
					},
225
					"type": "text"
226
				},
227
				{
228
					"buffer": 3,
229
					"file": "styles/hexmeh.css",
230
					"settings":
231
					{
232
						"buffer_size": 10926,
233
						"regions":
234
						{
235
						},
236
						"selection":
237
						[
238
							[
239
								74,
240
								74
241
							]
242
						],
243
						"settings":
244
						{
245
							"syntax": "Packages/CSS/CSS.tmLanguage"
246
						},
247
						"translation.x": 0.0,
248
						"translation.y": 1734.0,
249
						"zoom_level": 1.0
250
					},
251
					"type": "text"
252
				}
94 253
			]
95 254
		}
96 255
	],
@ -145,7 +304,7 @@
145 304
		[
146 305
			[
147 306
				"",
148
				"/home/bryan/Projects/mcnaughty/mcnaughty.sublime-project"
307
				"/home/bryan/Projects/whistle/whistle.sublime-project"
149 308
			]
150 309
		],
151 310
		"width": 380.0

+ 3 - 3
partials/dump.html

@ -14,7 +14,7 @@
14 14
</div>
15 15
16 16
<div id="terminal" class="mono">
17
    <div id="lineNumbers"></div>
18
    <div id="lineHex"></div>
19
    <div id="lineCharacters"></div>
17
    <div id="lineNumbers" class="number"></div>
18
    <div id="lineHex" class="hex"></div>
19
    <div id="lineCharacters" class="char"></div>
20 20
</div>

+ 67 - 39
scripts/controllers.js

@ -47,6 +47,65 @@ function DumpController($scope, $routeParams) {
47 47
  $scope.fileApi = false;
48 48
  $scope.instructions = "Drop a file here!";
49 49
  $scope.uppercaseHex = false;
50
  $scope.file = null;
51
  $scope.lastLine = 0;
52
53
  function toggleSwitches() {
54
    $("input, select").prop("disabled", !$("input, select").prop("disabled"));
55
  }
56
57
  function outputLine(bytes) {
58
    // Add a break line.
59
    $("#lineNumbers, #lineHex, #lineCharacters").append("<br>");
60
    
61
    // Get the line number.
62
    var lineNumber = padLeft($scope.lastLine.toString(16), 6);
63
    if ($scope.uppercaseHex) {
64
      lineNumber = lineNumber.toUpperCase();
65
    }
66
    $("#lineNumbers").append(lineNumber + "<span class='symbol'>:</span>");
67
    
68
    for (var i = 0; i < bytes.length; i++) {
69
      //var progress = $(".percent");
70
      //var percent = ((i + (($scope.lastLine + 1) * $scope.columns)) / $scope.file.size) * 100;
71
      //$(progress).text(percent + "%");
72
73
      // Get the hex string.
74
      var hexString = padLeft(bytes[i].toString(16), 2);
75
      if ($scope.uppercaseHex) {
76
          hexString = hexString.toUpperCase();
77
      }
78
      
79
      // Output the remaining lines.
80
      $("#lineHex").append(hexString + "&nbsp;");
81
      $("#lineCharacters").append(String.fromCharCode(bytes[i]));
82
    }
83
84
    // Increment to next line.
85
    $scope.lastLine++;
86
  }
87
88
  function readBlob(start, stop) {
89
    if (start < $scope.file.size) {
90
      var reader = new FileReader();
91
      
92
      // Closure to capture the file information.
93
      reader.onload = function(evt) {
94
        var bytes = new Uint8Array(evt.target.result);
95
        outputLine(bytes);
96
        readBlob(stop, stop + $scope.columns);
97
      }
98
99
      var blob = $scope.file.webkitSlice(start, stop);
100
      reader.readAsArrayBuffer(blob);
101
    }
102
    else {
103
      $scope.lastLine = 0;
104
      $scope.file = null;
105
      toggleSwitches();
106
      $("#progress_bar").remove();
107
    }
108
  }
50 109
  
51 110
  // Check for the various File API support.
52 111
  if (window.File && window.FileReader && window.FileList && window.Blob) {
@ -63,50 +122,19 @@ function DumpController($scope, $routeParams) {
63 122
      
64 123
      // Remove previous output.
65 124
      $("#lineNumbers, #lineHex, #lineCharacters").empty();
125
      toggleSwitches();
126
127
      //$("#drop-zone").append($("<div id='progress_bar'><div class='percent'>0%</div></div>"));
66 128
      
67 129
      // Retrieve the files.
68 130
      var files = e.originalEvent.dataTransfer.files;
69
      
70
      // Iterate though each file.
71
      for (var i = 0, f; f = files[i]; i++) {
72
        var reader = new FileReader();
73
        
74
        // Closure to capture the file information.
75
        reader.onload = (function(theFile) {
76
          return function(e) {
77
            var bytes = new Uint8Array(e.target.result);
78
79
            for (var i = 0; i < bytes.length; i++) {
80
              if (i % $scope.columns === 0) {
81
                // Add a break line.
82
                $("#lineNumbers, #lineHex, #lineCharacters").append("<br>");
83
                
84
                // Get the line number.
85
                var lineNumber = padLeft(i.toString(16), 6);
86
                if ($scope.uppercaseHex) {
87
                  lineNumber = lineNumber.toUpperCase();
88
                }
89
                $("#lineNumbers").append("<span class='number'>" + lineNumber + "</span><span class='symbol'>:</span>");
90
              }
91
              
92
              // Get the hex string.
93
              var hexString = padLeft(bytes[i].toString(16), 2);
94
              if ($scope.uppercaseHex) {
95
                  hexString = hexString.toUpperCase();
96
              }
97
              
98
              // Output the remaining lines.
99
              $("#lineHex").append("<span class='hex'>" + hexString + "</span>&nbsp;");
100
              $("#lineCharacters").append("<span class='char'>" + String.fromCharCode(bytes[i]) + "</span>");
101
            }
102
          };
103
        })(f);
104
        
105
        // Read in the image file as a data URL.
106
        reader.readAsArrayBuffer(f);
131
      if (files) {
132
        $scope.file = files[0];
133
        readBlob(0, $scope.columns);
107 134
      }
108 135
    });
109
  } else {
136
  } 
137
  else {
110 138
    $scope.fileApi = false;
111 139
    $scope.instructions = "Sorry, your browser needs to go to an old folks home.";
112 140
    alert('The File APIs are not fully supported in this browser.');