Make output path to render_pictures optional
Review URL: https://codereview.appspot.com/6779049

git-svn-id: http://skia.googlecode.com/svn/trunk@6136 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp
index 6d6e8db..ae9e94d 100644
--- a/tools/render_pictures_main.cpp
+++ b/tools/render_pictures_main.cpp
@@ -23,7 +23,8 @@
     SkDebugf("SkPicture rendering tool\n");
     SkDebugf("\n"
 "Usage: \n"
-"     %s <input>... <outputDir> \n"
+"     %s <input>... \n"
+"     [-w <outputDir>]"
 "     [--mode pow2tile minWidth height[%] | simple\n"
 "         | tile width[%] height[%]]\n"
 "     [--pipe]\n"
@@ -84,7 +85,7 @@
     path->remove(path->size() - 4, 4);
 }
 
-static bool render_picture(const SkString& inputPath, const SkString& outputDir,
+static bool render_picture(const SkString& inputPath, const SkString* outputDir,
                            sk_tools::PictureRenderer& renderer) {
     SkString inputFilename;
     sk_tools::get_basename(&inputFilename, inputPath);
@@ -109,12 +110,17 @@
     renderer.init(&picture);
     renderer.setup();
 
-    SkString outputPath;
-    make_output_filepath(&outputPath, outputDir, inputFilename);
-
-    success = renderer.render(&outputPath);
-    if (!success) {
-        SkDebugf("Could not write to file %s\n", outputPath.c_str());
+    SkString* outputPath = NULL;
+    if (NULL != outputDir) {
+        outputPath = SkNEW(SkString);
+        make_output_filepath(outputPath, *outputDir, inputFilename);
+    }
+    success = renderer.render(outputPath);
+    if (outputPath) {
+        if (!success) {
+            SkDebugf("Could not write to file %s\n", outputPath->c_str());
+        }
+        SkDELETE(outputPath);
     }
 
     renderer.resetState();
@@ -123,11 +129,12 @@
     return success;
 }
 
-static int process_input(const SkString& input, const SkString& outputDir,
+static int process_input(const SkString& input, const SkString* outputDir,
                           sk_tools::PictureRenderer& renderer) {
     SkOSFile::Iter iter(input.c_str(), "skp");
     SkString inputFilename;
     int failures = 0;
+    SkDebugf("process_input, %s\n", input.c_str());
     if (iter.next(&inputFilename)) {
         do {
             SkString inputPath;
@@ -150,7 +157,7 @@
 }
 
 static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
-                              sk_tools::PictureRenderer*& renderer){
+                              sk_tools::PictureRenderer*& renderer, SkString*& outputDir){
     const char* argv0 = argv[0];
     char* const* stop = argv + argc;
 
@@ -247,6 +254,14 @@
             SkDELETE(renderer);
             usage(argv0);
             exit(-1);
+        } else if (0 == strcmp(*argv, "-w")) {
+            ++argv;
+            if (argv >= stop) {
+                SkDebugf("Missing output directory for -w\n");
+                usage(argv0);
+                exit(-1);
+            }
+            outputDir = SkNEW_ARGS(SkString, (*argv));
         } else {
             inputs->push_back(SkString(*argv));
         }
@@ -325,8 +340,11 @@
         renderer = SkNEW(sk_tools::PipePictureRenderer);
     }
 
-    if (inputs->count() < 2) {
+    if (inputs->empty()) {
         SkDELETE(renderer);
+        if (NULL != outputDir) {
+            SkDELETE(outputDir);
+        }
         usage(argv0);
         exit(-1);
     }
@@ -343,13 +361,12 @@
     SkAutoGraphics ag;
     SkTArray<SkString> inputs;
     sk_tools::PictureRenderer* renderer = NULL;
-
-    parse_commandline(argc, argv, &inputs, renderer);
-    SkString outputDir = inputs[inputs.count() - 1];
+    SkString* outputDir = NULL;
+    parse_commandline(argc, argv, &inputs, renderer, outputDir);
     SkASSERT(renderer);
 
     int failures = 0;
-    for (int i = 0; i < inputs.count() - 1; i ++) {
+    for (int i = 0; i < inputs.count(); i ++) {
         failures += process_input(inputs[i], outputDir, *renderer);
     }
     if (failures != 0) {
@@ -365,7 +382,9 @@
     }
 #endif
 #endif
-
+    if (NULL != outputDir) {
+        SkDELETE(outputDir);
+    }
     SkDELETE(renderer);
     return 0;
 }