Commit patch to not break on spaces.
[bowtie.git] / bowtie_build_main.cpp
1 /*
2  * bowtie_build_main.cpp
3  *
4  *  Created on: Sep 16, 2009
5  *      Author: Ben Langmead
6  */
7
8 #include <iostream>
9 #include <fstream>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <vector>
13 #include "tokenize.h"
14
15 using namespace std;
16
17 extern "C" {
18         int bowtie_build(int argc, const char **argv);
19 }
20
21 /**
22  * bowtie-build main function.  It is placed in a separate source file
23  * to make it slightly easier to compile as a library.
24  *
25  * If the user specifies -A <file> as the first two arguments, main
26  * will interpret that file as having one set of command-line arguments
27  * per line, and will dispatch each batch of arguments one at a time to
28  * bowtie-build.
29  */
30 int main(int argc, const char **argv) {
31         if(argc > 2 && strcmp(argv[1], "-A") == 0) {
32                 const char *file = argv[2];
33                 ifstream in;
34                 in.open(file);
35                 char buf[4096];
36                 int lastret = -1;
37                 while(in.getline(buf, 4095)) {
38                         vector<string> args;
39                         args.push_back(string(argv[0]));
40                         tokenize(buf, " \t", args);
41                         const char **myargs = (const char**)malloc(sizeof(char*)*args.size());
42                         for(size_t i = 0; i < args.size(); i++) {
43                                 myargs[i] = args[i].c_str();
44                         }
45                         if(args.size() == 1) continue;
46                         lastret = bowtie_build(args.size(), myargs);
47                         free(myargs);
48                 }
49                 if(lastret == -1) {
50                         cerr << "Warning: No arg strings parsed from " << file << endl;
51                         return 0;
52                 }
53                 return lastret;
54         } else {
55                 return bowtie_build(argc, argv);
56         }
57 }