1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
//Maplestory XML Parser
//Copyright (c) 2011 Cecil G. Bowen
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<conio.h>
#include<string>
using namespace std;
template<typename T>
int nthSubstr(int n, const basic_string<T>& s, const basic_string<T>& p, bool repeats = false)
{
string::size_type i = s.find(p);
string::size_type adv = (repeats) ? 1 : p.length( );
int j;
for (j = 1; j < n && i != basic_string<T>::npos; ++j)
i = s.find(p, i+adv);
if (j == n)
return(i);
else
return(-1);
}
int main(int nNumberofArgs, char* pszArgs[])
{
const int SIZE = 17704;
int ans, tries = 0, good_line, i = 1;
bool goodu = 0, goodp = 0; //Account Validating Booleans
string filename, txtfile; //Name of Files
string idParse = "<imgdir name=";
string nameParse = "value=";
string isName = "mapName";
string line[SIZE]; //File Array *IMPORTANT*
string id[SIZE]; //Obtained IDs Lists
string name[SIZE]; //Obtained Names List
//cout<<"\nXML Filename: ";
//cin>>filename;
//cin.clear();
//cout<<"\nTXT Filename: ";
//cin>>txtfile;
//cin.clear();
//Read Data from Text File
ifstream myWZ;
myWZ.open("map.img.xml");
if (myWZ.is_open())
{
cout<<"opened xml";
while (!myWZ.eof())
{
getline (myWZ,line[i]); //Stores Each File Line to the "line" Array
cout<<"line: "<< i <<": " <<line[i] <<"\n";
++i;
}
myWZ.close();
}
else
{
system("cls"); //Clear Screen
cout<<"File read error! Please make sure the specified file exists!";
}
//Check File for ID
int a, b; //Iterator Values for Storing IDs/Names
for(int j=0; j<i; ++j)
{
if (line[j].find(idParse) != string::npos) //If id is found on current line...
{
if (line[j].find_first_of("0123456789") != string::npos)
{
int openQ = (line[j].find('"')) + 1; //Returns Position Inside First "
int endQ = (line[j].rfind('"')) - 1; //Returns Position Inside End "
id[a] = line[j].substr(openQ, (endQ-openQ)); //Add it to the ID List
a++;
}
}
else if (line[j].find(nameParse) != string::npos) //If name is found on current line...
{
if (line[j].find(isName) != string::npos) //If "mapName" is found on same line...
{
string quote = "\"";
int openQ = nthSubstr(3, line[j],quote) + 1; //Find Position of 3rd "
int endQ = nthSubstr(4, line[j],quote) - 1; //Find Position of 4th "
name[b] = line[j].substr(openQ, (endQ-openQ)); //Add it to the Name List
b++;
}
}
}
id[a] = "/";
name[b] = "/";
//Output Text to File
ofstream myTXT;
myTXT.open("txtID.txt",ios_base::app); //Opens TXT File at End for Writing
if (myTXT.is_open())
{
int x = 0;
while (true)
{
if (id[x] == "/")
{
break;
}
myTXT <<id[x] <<" " <<name[x] <<"\n";
x++;
}
myTXT.close();
}
else
{
cout<<"\nfailed to write to file";
return 0;
}
}
|