00001 #include <string>
00002 #include <stdio.h>
00003 #include <map>
00004 #include <Base64.h>
00005 #include <Uid.h>
00006 #include <vector>
00007 #include "rowo.h"
00008 #include "robotworld.h"
00009
00010
00011 void parse_file(const std::string& filename);
00012 const std::string& GetCodestr();
00013 std::map<std::string, int>& GetCodemap();
00014 std::vector<VARIABLE *>& GetVariables();
00015 bool GetFailed();
00016
00017
00018 std::string GetMyUid()
00019 {
00020 std::string myuid;
00021 FILE *fil;
00022 if ((fil = fopen("myuid.txt","rt")) != NULL)
00023 {
00024 char slask[1000];
00025 fgets(slask,1000,fil);
00026 slask[strlen(slask) - 1] = 0;
00027 fclose(fil);
00028 myuid = slask;
00029 }
00030 else
00031 {
00032 Uid uid;
00033 myuid = uid.GetUid();
00034 fil = fopen("myuid.txt","wt");
00035 fprintf(fil,"%s\n",myuid.c_str());
00036 fclose(fil);
00037 }
00038 return myuid;
00039 }
00040
00041 int main(int argc,char *argv[])
00042 {
00043 std::string myuid = GetMyUid();
00044
00045 if (argc < 2)
00046 printf("Usage: %s <.r file(s)>\n",*argv);
00047 for (int i = 1; i < argc; i++)
00048 {
00049 std::string filename = argv[i];
00050 unsigned char buf[10000];
00051 if (filename.substr(filename.size() - 2) != ".r")
00052 {
00053 printf("'%s' is not a .r file\n",argv[i]);
00054 }
00055 else
00056 {
00057 std::string robot_name = filename.substr(0,filename.size() - 2);
00058 parse_file(argv[i]);
00059
00060 if (!GetFailed())
00061 {
00062 size_t proglen = GetCodestr().size() / 2;
00063 for (size_t i = 0; i < GetCodestr().size(); i += 2)
00064 {
00065 char c1 = GetCodestr()[i];
00066 char c2 = GetCodestr()[i + 1];
00067 buf[i / 2] = (c1 - 48 - ((c1 > '9') ? 7:0) - ((c1 > 'F') ? 32:0)) * 16 +
00068 (c2 - 48 - ((c2 > '9') ? 7:0) - ((c2 > 'F') ? 32:0));
00069 }
00070 Base64 bb;
00071 std::string codestr64;
00072 bb.encode(buf, proglen, codestr64, false);
00073
00074
00075 FILE *fil;
00076 filename = robot_name + ".xml";
00077 if ((fil = fopen(filename.c_str(),"wt")) != NULL)
00078 {
00079 fprintf(fil,"<?xml version='1.0'?>\n");
00080 fprintf(fil,"<robot>\n");
00081 fprintf(fil,"<name value='%s' />\n",robot_name.c_str());
00082 fprintf(fil,"<owner value='%s' />\n",myuid.c_str());
00083 fprintf(fil,"<code value=\"%s\" />\n",codestr64.c_str());
00084 fprintf(fil,"<methods>\n");
00085 for (std::map<std::string,int>::iterator it = GetCodemap().begin(); it != GetCodemap().end(); it++)
00086 {
00087 std::string method = (*it).first;
00088 int ptr = (*it).second;
00089 fprintf(fil,"<method name='%s' entry='%d' />\n",method.c_str(),ptr);
00090 }
00091 fprintf(fil,"</methods>\n");
00092 fprintf(fil,"<variables>\n");
00093 for (std::vector<VARIABLE *>::iterator it = GetVariables().begin(); it != GetVariables().end(); it++)
00094 {
00095 VARIABLE *p = *it;
00096 fprintf(fil,"<variable name='%s' type='%d' index='%d' />\n",
00097 p -> name.c_str(),p -> type,p -> nr);
00098 }
00099 fprintf(fil,"</variables>\n");
00100
00101 fprintf(fil,"</robot>\n");
00102 fclose(fil);
00103 }
00104 }
00105 }
00106 }
00107 }
00108
00109