//---------- 010 Editor v3.x Script File ------------------------------ // File: Copy as Pascal Array // Author: Bernhard Foltz, Munich, Germany // Revision: 1 // Purpose: Copy selection or entire file to clipboard as Pascal code //--------------------------------------------------------------------- const int entire_always = 0; // 1: do for entire file always, 0: if no selection const char newline[] = "\r\n"; // newline. p.e. "\r\n" or "\n" int64 adr, siz; int actfile, newfile, bytpline, i, l; string name; char c; // At least, one file must be open if (FileCount() == 0) { MessageBox(idOk, "Copy as Pascal Array", "No file is open."); return -1; } // Do it for entire file if entire_always or no selection available if ((entire_always != 0) | (GetSelSize() <= 0)) { adr = 0; siz = FileSize(); } else { adr = GetSelStart(); siz = GetSelSize(); } // At least, one byte should be processed if (siz == 0) { MessageBox(idOk, "Copy as Pascal Array", "No bytes to process."); return -1; } // convert actual file name to constant name name = GetFileName(); for (i = Strlen(name) - 1; i >= 0; i--) { c = name[i]; if ((c == '\\') || (c == ':')) { name = StrDel(name, 0, i + 1); break; // remove path } else if (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))) name[i] = '_'; // replace non-alphanum } // create temporary file for to copy to clipboard actfile = GetFileNum(); newfile = FileNew(); FileSelect(actfile); // write pascal const definition FPrintf(newfile, "const %s: array[0 .. $%LX-1] of byte =%s (", name, siz, newline); // write array of byte bytpline = GetBytesPerLine(); l = 0; while (siz > 0) { FPrintf(newfile, "$%.2X", ReadUByte(adr)); adr++; siz--; l++; if (siz > 0) { FPrintf(newfile, ","); if (l >= bytpline) { FPrintf(newfile, "%s ", newline); l = 0; } } } // end of array FPrintf(newfile, ");"); if (l > 0) FPrintf(newfile, newline); // copy text to clipboard, delete temporary file FileSelect(newfile); SetSelection(0, FileSize()); CutToClipboard(); FileClose(); FileSelect(actfile);