Author Topic: [Snippet] Append string to file  (Read 4142 times)

Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
[Snippet] Append string to file
« on: June 24, 2015, 12:20:04 am »
usage: v_register("im a string!", "file.txt");
or: v_register("im a string!");

Code: [Select]
function v_register(text, fl = "file.xt") {
local f = file(fl, "a+");
if (f) {
local i = 1, i1 = 2, op = 0, done = false;
while(!done) {
if (op == 0) {
f.writen(int_letters(text.slice(0, i)), 'b');
op = 1;
}
else if (op == 1) {
i = 0;
op = 2;
}
else if (op == 2) {
if (i >= text.len()) {
done = true;
break;
}
f.writen(int_letters(text.slice(i, i1)), 'b');
string += text.slice(i, i1);
i1++;
}
i++;
}

f.writen(int_letters("\n"), 'b');
f.flush();
f.close();
}
}

function int_letters(letter) {
switch(letter) {
case "\n": return '\n';
case "[": return '[';
case "]": return ']';
case "-": return '-';
case "+": return '+';
case "^": return '^';
case "*": return '*';
case "}": return '}';
case "{": return '{';
case "(": return '(';
case ")": return ')';
case "#": return '#';
case "/": return '/';
case ":": return ':';
case ".": return '.';
case ",": return ',';
case "<": return '<';
case ">": return '>';
case "|": return '|';
case " ": return ' ';

case "0": return '0';
case "1": return '1';
case "2": return '2';
case "3": return '3';
case "4": return '4';
case "5": return '5';
case "6": return '6';
case "7": return '7';
case "8": return '8';
case "9": return '9';

case "A": return 'A';
case "B": return 'B';
case "C": return 'C';
case "D": return 'D';
case "E": return 'E';
case "F": return 'F';
case "G": return 'G';
case "H": return 'H';
case "I": return 'I';
case "J": return 'J';
case "K": return 'K';
case "L": return 'L';
case "M": return 'M';
case "N": return 'N';
case "O": return 'O';
case "P": return 'P';
case "Q": return 'Q';
case "R": return 'R';
case "S": return 'S';
case "T": return 'T';
case "U": return 'U';
case "V": return 'V';
case "W": return 'W';
case "X": return 'X';
case "Y": return 'Y';
case "Z": return 'Z';

case "a": return 'a';
case "b": return 'b';
case "c": return 'c';
case "d": return 'd';
case "e": return 'e';
case "f": return 'f';
case "g": return 'g';
case "h": return 'h';
case "i": return 'i';
case "j": return 'j';
case "k": return 'k';
case "l": return 'l';
case "m": return 'm';
case "n": return 'n';
case "o": return 'o';
case "p": return 'p';
case "q": return 'q';
case "r": return 'r';
case "s": return 's';
case "t": return 't';
case "u": return 'u';
case "v": return 'v';
case "w": return 'w';
case "x": return 'x';
case "y": return 'y';
case "z": return 'z';
default: return 0;
}
}
« Last Edit: June 11, 2016, 09:09:53 am by Ankris »

Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
Re: [Snippet] Append string to file
« Reply #1 on: September 01, 2015, 11:07:23 pm »
v2:

Code: [Select]
int_letters <- { };
function onScriptLoad() { MakeCharsTable(); }

function MakeCharsTable() {
int_letters = { };

local i = 0;
while(i < 300) {
int_letters[i.tochar()] <- i;
i++;
}
}

function v_register(text, dir = "file.xt") {
local f = file(dir, "a+");
if (f) {
local i = 1, i1 = 2, op = 0, done = false, string = "";
while(!done) {
if (op == 0) {
f.writen(int_letters[text.slice(0, i)], 'b');
op = 1;
}
else if (op == 1) {
i = 0;
op = 2;
}
else if (op == 2) {
if (i >= text.len()) {
done = true;
break;
}
f.writen(int_letters[text.slice(i, i1)], 'b');
string += text.slice(i, i1);
i1++;
}
i++;
}

f.writen(int_letters["\n"], 'b');
f.flush();
f.close();
}
}

Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
Re: [Snippet] Append string to file
« Reply #2 on: June 11, 2016, 09:11:10 am »
v3: made time ago

Code: [Select]
function v_register(text, f = "file.xt") {
local f = file(f, "a+");
if (!f) return false;

foreach(idx, val in text) {
f.writen(val, 'b');
}

f.writen('\n', 'b');
f.flush();
f.close();

return true;
}

 

© Liberty Unleashed Team.