Use less efficient arg processing for vala 0.11 compatibility
[moonshot-ui.git] / src / moonshot-webp-parser.vala
index 16ce39e..aa89660 100644 (file)
@@ -1,80 +1,86 @@
+using Moonshot;
+
 namespace WebProvisioning
-{
-  class WebProvisionParser
-  {
-    public WebProvisionParser (string path)
-    {
-      
-    }
-  }
-  
-  public void start_element_func (MarkupParseContext context,
-                                  string             element_name,
-                                  string[]           attribute_names,
-                                  string[]           attribute_values) throws MarkupError
-  {
-    debug ("Start: %s", element_name);
-  }
+{ 
 
-  public void end_element_func (MarkupParseContext context,
-                                string             element_name) throws MarkupError
-  {
-    debug ("End: %s", element_name);
-  }
-  
-  public void text_element_func (MarkupParseContext context,
-                                 string text,
-                                 size_t text_len) throws MarkupError
-  {
-    debug ("Text element: %s", text); 
-    foreach (string elm in context.get_element_stack ())
-    {
-      stdout.printf("%s\n", elm);
-    }
-  }
-  
 
   public static int main (string[] args)
   {
-    string text = "";
-    if (args.length < 2)
-    {
-      error ("Usage %s [-a] WEB_PROVISIONING_FILE", args[0]);
+    int arg_index = -1;
+    int force_flat_file_store = 0;
+    bool bad_switch = false;
+    for (arg_index = 1; arg_index < args.length; arg_index++) {
+      string arg = args[arg_index];
+      unichar c = arg.get_char();
+      if (c=='-') {
+          arg = arg.next_char();
+          c = arg.get_char();
+          switch (c) {
+            case 'f':
+              force_flat_file_store = 1;
+              break;
+            default:
+              bad_switch = true;
+              break;
+          }
+      } else {
+        break; // arg is not a switch; presume it's the file
+      }
     }
-    
-    if (!FileUtils.test (args[1], FileTest.EXISTS | FileTest.IS_REGULAR))
+    if (bad_switch || (arg_index != args.length - 1))
     {
-      error ("%s does not exist", args[1]);
+      error ("Usage %s [-f] WEB_PROVISIONING_FILE\n -f: add identities to flat file store", args[0]);
     }
+    string webp_file = args[arg_index];
     
-    var file = File.new_for_path (args[1]);
-    
-    try
-    {
-      var dis = new DataInputStream (file.read ());
-      string line;
-      while ((line = dis.read_line (null)) != null)
-        text += line;
-    }
-    catch (Error e)
+    if (!FileUtils.test (webp_file, FileTest.EXISTS | FileTest.IS_REGULAR))
     {
-      error ("Could not retreive file size");
+      error ("%s does not exist", webp_file);
     }
     
-    MarkupParser parser = {start_element_func, end_element_func, text_element_func, null, null};
+    var webp = new Parser (webp_file);
+    webp.parse();
     
-    var ctx = new MarkupParseContext(parser, 0, null, null);
-    
-    try
-    {
-      ctx.parse (text, text.length);
-    }
-    catch (Error e)
+    foreach (IdCard card in cards)
     {
-      error ("Could not parse %s, invalid content", args[1]);
+      Moonshot.Error error;
+      string[] rules_patterns = {};
+      string[] rules_always_confirm = {};
+        
+      if (card.rules.length > 0)
+      {
+        int i = 0;
+        rules_patterns = new string[card.rules.length];
+        rules_always_confirm = new string[card.rules.length];
+        foreach (Rule r in card.rules)
+        {
+          rules_patterns[i] = r.pattern;
+          rules_always_confirm[i] = r.always_confirm;
+          i++;
+        }
+      }
+
+      Moonshot.install_id_card (card.display_name,
+                                card.username,
+                                card.password,
+                                card.issuer,
+                                rules_patterns,
+                                rules_always_confirm,
+                                card.services,
+                                card.trust_anchor.ca_cert,
+                                card.trust_anchor.subject,
+                                card.trust_anchor.subject_alt,
+                                card.trust_anchor.server_cert,
+                                force_flat_file_store,
+                                out error);
+
+      if (error != null)
+      {
+        stderr.printf ("Error: %s", error.message);
+        continue;
+      }
     }
     
-    
     return 0;
   }
 }