66import java .io .ByteArrayOutputStream ;
77import java .io .DataInputStream ;
88import java .io .DataOutputStream ;
9+ import java .io .File ;
910import java .io .FileInputStream ;
1011import java .io .FileNotFoundException ;
1112import java .io .FileOutputStream ;
3031 * @author chrisws
3132 */
3233public class QrEncoder {
34+ private static final int DEF_SIZE = 500 ;
35+
3336 /**
3437 * @param args
3538 * @throws IOException
3639 * @throws WriterException
3740 */
3841 public static void main (String [] args ) throws IOException , WriterException {
39- new QrEncoder ().process (args [0 ]);
42+ File input = new File (args [0 ]);
43+ if (!input .exists () || !input .canRead () || !input .isFile ()) {
44+ System .err .println ("Invalid input: " + input .toString ());
45+ } else {
46+ int size = DEF_SIZE ;
47+ if (args .length > 1 ) {
48+ size = Integer .valueOf (args [1 ]);
49+ }
50+ new QrEncoder ().process (input , size );
51+ }
4052 }
4153
4254 protected String cleanupCode (String fileText ) {
55+ // convert DOS line endings to UNIX
56+ String result = fileText .replaceAll ("\\ r\\ n" , "\n " );
57+
4358 // remove quotes
44- String result = fileText .replaceAll ("(?mi)^\\ s*?rem.*[\\ r\\ n]" , "" );
45- result = result .replaceAll ("(?mi)^\\ s*?'.*[\\ r\\ n]" , "" );
46- result = result .replaceAll ("(?mi)^\\ s*?#.*[\\ r\\ n]" , "" );
59+ result = fileText .replaceAll ("(?mi)^\\ s*?rem.*[\\ r\\ n]" , "" );
60+ result = result .replaceAll ("(?m)^\\ s*?'.*[\\ r\\ n]" , "" );
61+ result = result .replaceAll ("(?m)^\\ s*?#.*[\\ r\\ n]" , "" );
62+
63+ // remove leading white space
64+ result = result .replaceAll ("(?m)^\\ s*" , "" );
4765
48- // remove extra white space
66+ // remove double spaces
4967 result = result .replaceAll ("[ \t ]{2,}" , " " );
5068
69+ // remove empty blank lines
70+ result = result .replaceAll ("[\\ r\\ n]{2,}" , "\n " );
71+
5172 return result .trim ();
5273 }
5374
@@ -78,9 +99,9 @@ protected byte[] createQRCode(String qrText, int size) throws WriterException, I
7899 return output .toByteArray ();
79100 }
80101
81- protected String getFileText (String fileName ) throws IOException {
102+ protected String getFileText (File input ) throws IOException {
82103 ByteArrayOutputStream out = new ByteArrayOutputStream ();
83- try (DataInputStream is = new DataInputStream (new FileInputStream (fileName ));) {
104+ try (DataInputStream is = new DataInputStream (new FileInputStream (input ));) {
84105 byte [] buf = new byte [1024 ];
85106 int len = is .read (buf );
86107 while (len != -1 ) {
@@ -91,11 +112,11 @@ protected String getFileText(String fileName) throws IOException {
91112 return out .toString ("utf-8" );
92113 }
93114
94- protected void process (String fileName ) throws IOException ,
115+ protected void process (File input , int size ) throws IOException ,
95116 FileNotFoundException , WriterException {
96117
97118 // load the file text
98- String fileText = getFileText (fileName );
119+ String fileText = getFileText (input );
99120
100121 // remove comments and empty white space
101122 fileText = cleanupCode (fileText );
@@ -104,9 +125,9 @@ protected void process(String fileName) throws IOException,
104125 String zipText = zipBase64 (fileText );
105126
106127 // encode the zipped data into a qrcode
107- byte [] out = createQRCode ("smallbasic://x/" + zipText , 512 );
128+ byte [] out = createQRCode ("smallbasic://x/" + zipText , size );
108129
109- writeOutput (out , fileName + ".png" );
130+ writeOutput (out , input + ".png" );
110131 }
111132
112133 protected void writeOutput (byte [] bytes , String fileName ) throws FileNotFoundException , IOException {
0 commit comments