| 1 | #include <osg/ArgumentParser> |
|---|
| 2 | #include <osgDB/FileNameUtils> |
|---|
| 3 | |
|---|
| 4 | #include <iostream> |
|---|
| 5 | #include <fstream> |
|---|
| 6 | #include <set> |
|---|
| 7 | #include <vector> |
|---|
| 8 | #include <stdlib.h> |
|---|
| 9 | |
|---|
| 10 | int main(int argc, char** argv) |
|---|
| 11 | { |
|---|
| 12 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 13 | |
|---|
| 14 | #if DEBUG |
|---|
| 15 | std::ofstream fout("output.txt"); |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | if (arguments.argc()<=1) |
|---|
| 19 | { |
|---|
| 20 | std::cout<<"Nothing to run"<<std::endl; |
|---|
| 21 | return 1; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | typedef std::vector<std::string> ArgumentList; |
|---|
| 25 | ArgumentList argumentList; |
|---|
| 26 | for(int i=1; i<arguments.argc(); ++i) |
|---|
| 27 | { |
|---|
| 28 | std::string argument = arguments[i]; |
|---|
| 29 | std::cout<<"argument = ["<<argument<<"]"<<std::endl; |
|---|
| 30 | std::string::size_type pos = 0; |
|---|
| 31 | std::string::size_type prev_start = 0; |
|---|
| 32 | for(pos = 0; pos<argument.size();) |
|---|
| 33 | { |
|---|
| 34 | if (argument[pos]=='"') |
|---|
| 35 | { |
|---|
| 36 | if (pos-prev_start>0) |
|---|
| 37 | { |
|---|
| 38 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | prev_start = pos; |
|---|
| 42 | ++pos; |
|---|
| 43 | |
|---|
| 44 | while (pos<argument.size() && argument[pos]!='"') { ++pos; } |
|---|
| 45 | |
|---|
| 46 | if (pos<argument.size()) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | ++pos; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | if (pos-prev_start>0) |
|---|
| 53 | { |
|---|
| 54 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | prev_start = pos; |
|---|
| 58 | |
|---|
| 59 | } |
|---|
| 60 | if (argument[pos]=='\'') |
|---|
| 61 | { |
|---|
| 62 | if (pos-prev_start>0) |
|---|
| 63 | { |
|---|
| 64 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | prev_start = pos; |
|---|
| 68 | ++pos; |
|---|
| 69 | |
|---|
| 70 | while (pos<argument.size() && argument[pos]!='\'') { ++pos; } |
|---|
| 71 | |
|---|
| 72 | if (pos<argument.size()) |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | ++pos; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if (pos-prev_start>0) |
|---|
| 79 | { |
|---|
| 80 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | prev_start = pos; |
|---|
| 84 | |
|---|
| 85 | } |
|---|
| 86 | else if (argument[pos]==' ') |
|---|
| 87 | { |
|---|
| 88 | if (pos-prev_start>0) |
|---|
| 89 | { |
|---|
| 90 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | while (pos<argument.size() && argument[pos]==' ') { ++pos; } |
|---|
| 94 | prev_start = pos; |
|---|
| 95 | } |
|---|
| 96 | else |
|---|
| 97 | { |
|---|
| 98 | ++pos; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (pos-prev_start>0) |
|---|
| 103 | { |
|---|
| 104 | argumentList.push_back(argument.substr(prev_start, pos-prev_start)); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | for(ArgumentList::iterator itr = argumentList.begin(); |
|---|
| 110 | itr != argumentList.end(); |
|---|
| 111 | ++itr) |
|---|
| 112 | { |
|---|
| 113 | std::cout<<"result ["<<*itr<<"]"<<std::endl; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if (argumentList.empty()) |
|---|
| 117 | { |
|---|
| 118 | std::cout<<"Nothing to run"<<std::endl; |
|---|
| 119 | return 1; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | for(int i=0; i<argumentList.size(); ++i) |
|---|
| 123 | { |
|---|
| 124 | std::string& argument = argumentList[i]; |
|---|
| 125 | std::string::size_type start_pos = argument.find("${"); |
|---|
| 126 | if (start_pos != std::string::npos) |
|---|
| 127 | { |
|---|
| 128 | std::string::size_type end_pos = argument.find("}",start_pos); |
|---|
| 129 | if (start_pos != std::string::npos) |
|---|
| 130 | { |
|---|
| 131 | std::string var = argument.substr(start_pos+2, end_pos-start_pos-2); |
|---|
| 132 | const char* str = getenv(var.c_str()); |
|---|
| 133 | #if DEBUG |
|---|
| 134 | std::cout<<"for argument = ["<<argument<<"]"<<std::endl; |
|---|
| 135 | std::cout<<"Found ["<<var<<"] = "<<str<<std::endl; |
|---|
| 136 | #endif |
|---|
| 137 | argument.erase(start_pos, end_pos-start_pos+1); |
|---|
| 138 | argument.insert(start_pos, str); |
|---|
| 139 | |
|---|
| 140 | #if DEBUG |
|---|
| 141 | std::cout<<"new argument = ["<<argument<<"]"<<std::endl; |
|---|
| 142 | #endif |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | if (argumentList[0]=="run") |
|---|
| 150 | { |
|---|
| 151 | std::string runstring; |
|---|
| 152 | for(int i=1; i<argumentList.size(); ++i) |
|---|
| 153 | { |
|---|
| 154 | runstring += argumentList[i]; |
|---|
| 155 | runstring += ' '; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | std::cout<<"Running : "<<runstring<<std::endl; |
|---|
| 159 | #if DEBUG |
|---|
| 160 | fout<<"Running : "<<runstring<<std::endl; |
|---|
| 161 | #endif |
|---|
| 162 | |
|---|
| 163 | return system(runstring.c_str()); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (argumentList[0]=="file") |
|---|
| 167 | { |
|---|
| 168 | std::string runstring("dolphin"); |
|---|
| 169 | |
|---|
| 170 | for(int i=1; i<argumentList.size(); ++i) |
|---|
| 171 | { |
|---|
| 172 | runstring += ' '; |
|---|
| 173 | runstring += argumentList[i]; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | std::cout<<"File : "<<runstring<<std::endl; |
|---|
| 177 | |
|---|
| 178 | #if DEBUG |
|---|
| 179 | fout<<"File : "<<runstring<<std::endl; |
|---|
| 180 | #endif |
|---|
| 181 | |
|---|
| 182 | return system(runstring.c_str()); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | typedef std::set<std::string> StringSet; |
|---|
| 186 | StringSet fileEndings; |
|---|
| 187 | |
|---|
| 188 | for(int i=0; i<argumentList.size(); ++i) |
|---|
| 189 | { |
|---|
| 190 | fileEndings.insert(osgDB::getFileExtension(argumentList[i])); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | for(StringSet::iterator itr = fileEndings.begin(); |
|---|
| 194 | itr != fileEndings.end(); |
|---|
| 195 | ++itr) |
|---|
| 196 | { |
|---|
| 197 | std::cout<<"File ending : "<<*itr<<std::endl; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | std::string application; |
|---|
| 201 | if (fileEndings.count("p3d")) |
|---|
| 202 | { |
|---|
| 203 | application = "present3D"; |
|---|
| 204 | } |
|---|
| 205 | else if (fileEndings.count("osg") || |
|---|
| 206 | fileEndings.count("ive") || |
|---|
| 207 | fileEndings.count("osga") || |
|---|
| 208 | fileEndings.count("3ds") || |
|---|
| 209 | fileEndings.count("obj") || |
|---|
| 210 | fileEndings.count("flt")) |
|---|
| 211 | { |
|---|
| 212 | application = "osgviewer"; |
|---|
| 213 | } |
|---|
| 214 | else |
|---|
| 215 | { |
|---|
| 216 | application = "firefox"; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | std::string runstring = application; |
|---|
| 220 | for(int i=0; i<argumentList.size(); ++i) |
|---|
| 221 | { |
|---|
| 222 | runstring += ' '; |
|---|
| 223 | runstring += argumentList[i]; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | std::cout<<"Running : "<<runstring<<std::endl; |
|---|
| 227 | |
|---|
| 228 | #if DEBUG |
|---|
| 229 | fout<<"Running : "<<runstring<<std::endl; |
|---|
| 230 | #endif |
|---|
| 231 | |
|---|
| 232 | return system(runstring.c_str()); |
|---|
| 233 | } |
|---|