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