View Single Post
Old 01-20-2014, 06:20 AM   #1
Shadow Stranger
Avatar
 

Join Date: December 9, 2007
Posts: 594
Smiley Writing Source Code with a Spreadsheet

Under certain circumstances, writing part of a computer program can be automated to a degree. The piece of source code must follow a pattern, have a fair amount of repetition inside itself. Here we see elements that are common from one for clause to another and we also see elements that are not:

printf("\n,");
for (i = 912; i < 2152; i++) {
if (i % 16 == 0)
printf("%d,",i);
printf("%d,",buffer[i]);
if (i % 16 == 15)
printf("\n,");
if (i == 2151)
printf("\n,");
}

printf("\n,");
for (i = 2152; i < 4000; i++) {
if (i % 16 == 8)
printf("%d,",i);
printf("%d,",buffer[i]);
if (i % 16 == 7)
printf("\n,");
if (i == 3999)
printf("\n,");
}

(A whole lot of lines have been omitted for brevity's sake.)

printf("\n,");
for (i = 32466; i < 32983; i++) {
if (i % 16 == 2)
printf("%d,",i);
printf("%d,",buffer[i]);
if (i % 16 == 1)
printf("\n,");
if (i == 32982)
printf("\n,");
}

printf("\n,");
for (i = 32983; i < 33100; i++) {
if (i % 16 == 7)
printf("%d,",i);
printf("%d,",buffer[i]);
if (i % 16 == 6)
printf("\n,");
if (i == 33099)
printf("\n,");
}

The for clause is a common element, but the numbers contained in each clause are not.

First, we create a specimen clause of code. If a number is surrounded on both sides by tab stops, when it is pasted into a spreadsheet it will be in a cell by itself. Below, "\t" represents an otherwise invisible tab stop. All paragraph marks are replaced by at symbols, "@". This means the clause now occupies only one line of code.
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");@}@

The specimen line of code is now copied into a spreadsheet.

Now the the Fill Down function is used:
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@

(A whole lot of lines have been omitted for brevity's sake.)

printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@


Now all the variable parts are in columns by themselves, just copy in the relevant numbers and comments:
printf("\n,");@for (i = \t912\t; i < \t2152\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t2151\t)@printf("\n,");}@
printf("\n,");@for (i = \t2152\t; i < \t4000\t; i++) {@if (i % 16 == \t8\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t7\t)@printf("\n,");@if (i == \t3999\t)@printf("\n,");}@
printf("\n,");@for (i = \t4000\t; i < \t5976\t; i++) {@if (i % 16 == \t0\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t15\t)@printf("\n,");@if (i == \t5975\t)@printf("\n,");}@

(A whole lot of lines have been omitted for brevity's sake.)

printf("\n,");@for (i = \t31654\t; i < \t32466\t; i++) {@if (i % 16 == \t6\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t5\t)@printf("\n,");@if (i == \t32465\t)@printf("\n,");}@
printf("\n,");@for (i = \t32466\t; i < \t32983\t; i++) {@if (i % 16 == \t2\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t1\t)@printf("\n,");@if (i == \t32982\t)@printf("\n,");}@
printf("\n,");@for (i = \t32983\t; i < \t33100\t; i++) {@if (i % 16 == \t7\t)@printf("%d,";i);@printf("%d,";buffer[i]);@if (i % 16 == \t6\t)@printf("\n,");@if (i == \t33099\t)@printf("\n,");}@

The code is then copied to a file, which is saved. File is then opened in a hex editor.
All tab stops are deleted, in Windows they are all equal to 09h. Use Replace All to replace all of them with nothing, this has the effect of deleting all of them.
Now at symbols, @, need to be replaced by a paragraph mark and a tab. In Windows hex terms 40h needs to be replaced by 0D0A09h.

This method is valid for any brand of source code.
Shadow Stranger is offline   Reply With Quote