1 // SemiTwist D Tools 2 // ini2urlencode: INI To URL Encode 3 // Written in the D programming language. 4 5 /++ 6 Author: 7 $(WEB www.semitwist.com, Nick Sabalausky) 8 9 This is very basic: 10 - Every line that doesn't have an equals sign is ignored. 11 - If a value if surrounded by double-quotes, the double-quotes are trimmed. 12 - Double-quotes can be used inside a double-quoted value without being escaped. 13 (And no escape sequences are recognized anyway.) 14 - If a value consists of nothing but one double-quote, the double-quote is 15 not trimmed. 16 - Whitespace that's not inside a name or a value is trimmed. 17 - There is no way to embed a newline in the name or value. 18 19 This has been tested to work with DMD 2.058 through 2.062 20 +/ 21 22 module semitwist.apps.miniapps.ini2urlencode.main; 23 24 import semitwist.cmd.all; 25 26 enum appVerStr = "0.03.1"; 27 Ver appVer; 28 static this() 29 { 30 appVer = appVerStr.toVer(); 31 } 32 33 void main(string[] args) 34 { 35 if(args.length != 2) 36 { 37 cmd.echo("INI To URL Encode (ini2urlencode) v"~appVerStr); 38 cmd.echo("Copyright (c) 2010 Nick Sabalausky"); 39 cmd.echo("See LICENSE.txt for license info"); 40 cmd.echo("Site: http://www.dsource.org/projects/semitwist"); 41 cmd.echo(); 42 cmd.echo("Sample Usages:"); 43 cmd.echo(" ini2urlencode input.ini"); 44 cmd.echo(" ini2urlencode input.ini > output.txt"); 45 cmd.echo(); 46 cmd.echo("See src/semitwist/apps/miniapps/ini2urlencode/main.d for extra information."); 47 return; 48 } 49 50 string iniData = cast(string)read(args[1]); 51 auto iniLines = iniData.split("\n"); 52 bool needAmp = false; 53 foreach(string line; iniLines) 54 { 55 line = line.strip(); 56 if(line == "") 57 continue; 58 59 auto indexEq = line.locate('='); 60 if(indexEq == line.length) 61 continue; 62 63 auto name = line[ 0 .. indexEq ].strip(); 64 auto val = line[ indexEq+1 .. $ ].strip(); 65 if(val.length > 1 && val[0] == '"' && val[$-1] == '"') 66 val = val[1..$-1]; 67 68 if(needAmp) 69 write("&"); 70 else 71 needAmp = true; 72 73 outputEncoded(name); 74 write("="); 75 outputEncoded(val); 76 } 77 } 78 79 void outputEncoded(string str) 80 { 81 foreach(char c; str) 82 { 83 if( (c >= 'a' && c <= 'z') || 84 (c >= 'A' && c <= 'Z') || 85 (c >= '0' && c <= '9') || 86 !"-_.~".find(c).empty ) 87 { 88 write(c); 89 } 90 else 91 { 92 write("%"); 93 writef("%.2X", c); 94 } 95 } 96 }