24.08.2015, 23:16
Код:
stock toJSON(format_[], {Float,_}:...) { new string[1024]; new stringIndex = 0; new formatIndex = 0; new formatLength = strlen(format_); new parameterIndex = 1; new parameterCount = numargs(); new character = '1'; new argumentIndex = 0; new result; new Float:resultFloat; string[stringIndex] = '{'; stringIndex++; while(formatIndex < formatLength && parameterIndex < parameterCount && stringIndex < 1000) { // limiting the length to 1000 allows room for the format/number/float. String will check it itself. string[stringIndex] = '"'; stringIndex++; character = '1'; argumentIndex = 0; while(character != 0) { character = getarg(parameterIndex, argumentIndex); if(character != 0) { string[stringIndex] = character; stringIndex++; if(stringIndex + 24 >= 1024) { format(string, 1024, "{null}"); return string; } } argumentIndex++; } parameterIndex++; string[stringIndex] = '"'; stringIndex++; string[stringIndex] = ':'; stringIndex++; switch(format_[formatIndex]) { case '\0': { format(string, 1024, "{null}"); return string; } case 'b': { result = getarg(parameterIndex); if(result == 0) { format(string, 1024, "%sfalse", string); stringIndex += 5; } else { format(string, 1024, "%strue", string); stringIndex += 4; } } case 'i', 'd': { result = getarg(parameterIndex); format(string, 1024, "%s%i", string, result); stringIndex = strlen(string); } case 'f': { resultFloat = Float:getarg(parameterIndex); format(string, 1024, "%s%f", string, resultFloat); stringIndex = strlen(string); } case 's': { string[stringIndex] = '"'; stringIndex++; character = '1'; argumentIndex = 0; while(character != 0) { character = getarg(parameterIndex, argumentIndex); if(character != 0) { string[stringIndex] = character; stringIndex++; if(stringIndex + 3 >= 1024) { format(string, 1024, "{null}"); return string; } } argumentIndex++; } string[stringIndex] = '"'; stringIndex++; } default: { continue; } } formatIndex++; parameterIndex++; if(formatIndex < formatLength && parameterIndex < parameterCount && stringIndex < 1000) { string[stringIndex] = ','; stringIndex++; } } string[stringIndex] = '}'; stringIndex++; string[stringIndex] = '\0'; printf("%s:%i", string, strlen(string)); return string; }
Test code:
Код:
new string[1024]; format(string, 1024, "%s", toJSON("sibf", "test", "Hello world!", "birthyear", 1994, "HaleyIsCute!", true, "float", 69.69)); print(string);
Please help me