Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple Convert is an application which converts map files into a
- format suitible for San Andreas Multiplayer. Copyright (C) 2010
- Kyoshiro aka Kyosaur.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, version 3 of the license.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <map>
- #include <windows.h>
- #include <tchar.h>
- using namespace std;
- //type def incase i want to use it
- typedef const char cchar;
- //window dimensions (Both windows)
- #define WINDOW_WIDTH 300
- #define WINDOW_HEIGHT 300
- #define CUSTOM_WIDTH 340
- #define CUSTOM_HEIGHT 250
- //function overloading for itos
- inline void itos(char str[], float number) { sprintf(str, "%f", number); }
- inline void itos(char str[], int number) { sprintf(str, "%d", number); }
- inline void itos(TCHAR str[], float number) { wsprintf(str, _T("%f"), number); }
- inline void itos(TCHAR str[], int number) { wsprintf(str, _T("%d"), number); }
- //macro to just shorten the code...
- #define SPECIFIER_CHECK(p) if(!str.compare(pos,(end-pos)+1,"{" #p "}")){ itos(tmp,p); str.replace(pos,(end-pos)+1,tmp); continue;}
- //enum for our menu's and "events"
- enum E_EVENT_MENU
- {
- E_MENU_MENUBAR,
- E_MENU_FILE,
- E_MENU_OPEN,
- E_MENU_SAVE,
- E_MENU_QUIT,
- E_MENU_OBJECTS,
- E_MENU_VEHICLES,
- E_MENU_FO_CREATEOBJECT,
- E_MENU_FO_INCOGNITO,
- E_MENU_FO_XSTREAMER,
- E_MENU_FO_XOBJECTS,
- E_MENU_FO_CUSTOM,
- E_MENU_FO_NONE,
- E_MENU_FO_ADDSTATICV,
- E_MENU_FO_ADDSTATICVEX,
- E_MENU_FO_CREATEVEHICLE,
- E_MENU_FO_VCUSTOM,
- E_MENU_FI_MTARACE,
- E_MENU_FI_MTA1,
- E_EVENT_TEXTBOX,
- E_EVENT_CONVERT,
- E_EVENT_OBJFORMAT,
- E_EVENT_VEHFORMAT,
- E_EVENT_CONTINUE
- };
- //structer for our g_Selected array
- struct hSelect
- {
- HMENU menu;
- int id;
- };
- //Array for menu selection tracking
- hSelect g_Selected[3] = {NULL};
- //Handles for our two windows
- HWND
- Main,
- Other;
- //map for out custom formats
- map<int,wstring> g_Formats;
- //This tracks our textbox message (assign a start message)
- wstring
- g_TextMessage = _T("Please paste your MTA map here, or open it with the file menu!\
- \r\n- You can chose your input and output format by changing them in the options menu.\
- \r\n- This is just a beta version, i will be making improvements!");
- //Our class for converting
- class Convert
- {
- private:
- int
- Model,
- Interior,
- Dimension;
- float
- X,
- Y,
- Z,
- RX,
- RY,
- RZ;
- public:
- Convert();
- void ParseInput(string,string,string,bool);
- string FormatOutput(string);
- };
- //Convert constructor
- Convert::Convert()
- {
- Model = Interior = Dimension = X = Y = Z = RX = RY = RZ = NULL;
- }
- //Function to parse data from our textbox
- void Convert::ParseInput(string Input, string ObjFormat, string VehFormat, bool Race)
- {
- //Defines for sscanf (replacement string specifier)
- #define SP_s "%[a-z-A-Z-0-9-_-.-(-)- ]"
- #define SP_S "%*[a-z-A-Z-0-9-_-.-(-)- ]"
- //Create our pointer to track tokens, and 2 string objects
- char* tok = NULL;
- string str[2];
- /* Since strtok asks for a char pointer, we have to dynamically
- allocate some memory of equal size to our string size, plus
- room for the terminating null character ('\0'). Then i copy
- the Input string to the newly created buf.
- */
- char* buf = new char[Input.size()+1];
- strcpy(buf,Input.c_str());
- //Strtok to split our map into tokens (using < and > as delimiters)
- tok = strtok(buf, "<>");
- //While there are tokens left
- while(tok)
- {
- //Chose the parser based on our race variable
- if(Race)
- {
- //Race parser comming soon
- /*
- <object name="object (16)">
- <position>-2252.4368 -1656.4774 455.7871</position>
- <rotation>-0.196349540625 0.69000021102425 0</rotation>
- <model>18450</model>
- </object>
- */
- }
- else
- {
- //We use sscanf to parse our object data, if the format matches
- int Obj = sscanf(tok,"object id=\"" SP_S "\" model=\"%d\" interior=\"%d\" dimension=\"%d\" posX=\"%f\" posY=\"%f\" posZ=\"%f\" rotX=\"%f\" rotY=\"%f\" rotZ=\"%f\" /",&Model,&Interior,&Dimension,&X,&Y,&Z,&RX,&RY,&RZ);
- //if it does match (9 = number of specifiers that ARENT "quiet")
- if(Obj == 9)
- {
- //Format the parsed data into the user's format and add it to str[0].
- str[0] += FormatOutput(ObjFormat) += "\r\n";
- }
- else
- {
- /*
- if our sscanf check failed, its not an appropriate object.
- We then check if the user specified if they wanted any vehicles
- by checking our vehicle menu tracker.
- */
- if(g_Selected[2].id != E_MENU_FO_NONE)
- {
- //If they wanted vehicles, we check the token again to see if its a vehicle.
- int veh = sscanf(tok,"vehicle id=\"" SP_S "\" paintjob=\"%*d\" model=\"%d\" plate=\"" SP_S "\" interior=\"%d\" dimension=\"%d\" posX=\"%f\" posY=\"%f\" posZ=\"%f\" rotX=\"%f\" rotY=\"%f\" rotZ=\"%f\" /",&Model,&Interior,&Dimension,&X,&Y,&Z,&RX,&RY,&RZ);
- //if it matches (again)
- if(veh == 9)
- {
- //Format the parsed vehicle data into the user's format.
- str[1] += FormatOutput(VehFormat) += "\r\n";
- }
- }
- }
- }
- /*
- When strtok's str param is NULL, it continues going through
- the tokens, which is exactly what we want to do in order to
- convert the entire map.
- */
- tok = strtok(NULL, "<>");
- }
- //When the conrting process is over we delete our allocated memory (buf)
- delete []buf;
- //Empty our global string that tracks E_EVENT_TEXTBOX since its about to change
- g_TextMessage.clear();
- //Add the vehicle string to out object string
- str[0] += "\r\n";
- str[0] += str[1];
- //Assign the object string to our E_EVENT_TEXTBOX tracking string.
- //We only add the object string since it now contains the vehicles too.
- g_TextMessage.assign(str[0].begin(),str[0].end());
- //Update our edit control E_EVENT_TEXTBOX
- SetDlgItemText(Main,E_EVENT_TEXTBOX,g_TextMessage.c_str());
- }
- //Yucky FormatOutput for converting
- string Convert::FormatOutput(string format)
- {
- //Create a temporary string so we dont mod format.
- string str = format;
- //Create 2 cars for tracking positions of specifiers.
- size_t
- pos = NULL,
- end = NULL;
- //char used for itos (in the macros below).
- char tmp[41];
- //search the string for "{"
- while((pos = str.find("{",pos+1)) != string::npos)
- {
- //while there's a "{", search for a "}"
- if((end = str.find("}",pos)) != string::npos)
- {
- /*
- if we found a closing brace we need
- to check if the content between matches
- one of our specifiers. If it does replace
- that part of the string with the correct
- value.
- I use a macro to cut down the code drastically.
- */
- SPECIFIER_CHECK(Model);
- SPECIFIER_CHECK(X);
- SPECIFIER_CHECK(Y);
- SPECIFIER_CHECK(Z);
- SPECIFIER_CHECK(RX);
- SPECIFIER_CHECK(RY);
- SPECIFIER_CHECK(RZ);
- SPECIFIER_CHECK(Interior);
- SPECIFIER_CHECK(Dimension);
- }
- }
- return str;
- }
- //function prototype for our Window procedures (aka "forwards")
- LRESULT CALLBACK MainProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK OtherProc(HWND, UINT, WPARAM, LPARAM);
- //function prototype for saving/opening files (aka a "forward")
- int OpenFileDialog(HWND hWnd, int item, const TCHAR* filter, bool save = false, bool append = false);
- string WstrToStr(wstring);
- //Heart of the code (main)
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- //Creating our WNDCLASS and MSG structures.
- MSG msg = {NULL};
- WNDCLASS wc = {NULL};
- //Fill out information for our windows
- wc.lpszClassName = _T("Main");
- wc.lpfnWndProc = (WNDPROC) MainProc;
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.hCursor = LoadCursor(hInstance,IDC_ARROW);
- wc.hIcon = (HICON) LoadImage(NULL, _T("Resources/Icon/icon.ico"), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
- //register and create our first window structure
- RegisterClass(&wc);
- Main = CreateWindow(wc.lpszClassName,_T("Simple Convert"),WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,100,100,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);
- //Change the class name and procedure pointer for the second window
- wc.lpszClassName = _T("Other");
- wc.lpfnWndProc = (WNDPROC) OtherProc;
- //Regester and create our second window
- RegisterClass(&wc);
- Other = CreateWindow(wc.lpszClassName,_T("Simple Covert"),WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,100,100,CUSTOM_WIDTH,CUSTOM_HEIGHT,Main,NULL,hInstance,NULL);
- //Here we get messages from windows, translate them,
- //and then send them to the window procedure(s).
- while(GetMessage(&msg,NULL,NULL,NULL))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
- //Callback for our second window
- LRESULT CALLBACK OtherProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
- {
- switch(Msg)
- {
- case WM_CREATE:
- {
- //Text for our static control
- const TCHAR* Text = _T("Please enter or load a custom format! For help \r\nand available specifiers see the help menu.");
- //Create our formats for our g_Formats map
- g_Formats[E_MENU_FO_CREATEOBJECT] = _T("CreateObject({Model},{X},{Y},{Z},{RX},{RY},{RZ});");
- g_Formats[E_MENU_FO_INCOGNITO] = _T("CreateDynamicObject({Model},{X},{Y},{Z},{RY},{RY},{RZ},{Dimension},{Interior},-1,100.0);");
- g_Formats[E_MENU_FO_XOBJECTS] = _T("{{Model},{X},{Y},{Z},{RX},{RY},{RZ},100.0},");
- g_Formats[E_MENU_FO_XSTREAMER] = _T("CreateStreamedObject({Model},{X},{Y},{Z},{RX},{RY},{RZ},100.0);");
- g_Formats[E_MENU_FO_CUSTOM] = _T("Please enter your object format!");
- g_Formats[E_MENU_FO_ADDSTATICV] = _T("AddStaticVehicle({Model},{X},{Y},{Z},{RZ},-1,-1);");
- g_Formats[E_MENU_FO_ADDSTATICVEX] = _T("AddStaticVehicleEx({Model},{X},{Y},{Z},{RZ},-1,-1,60);");
- g_Formats[E_MENU_FO_CREATEVEHICLE] = _T("CreateVehicle({Model},{X},{Y},{Z},{RZ},-1,-1,60);");
- g_Formats[E_MENU_FO_VCUSTOM] = _T("Please enter your vehicle format!");
- //create the static text control
- CreateWindow(_T("static"),Text, WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 5,CUSTOM_WIDTH-5,100,hWnd,NULL,NULL,NULL);
- //Our static text and edit controls for the second window
- CreateWindow(_T("static"),_T("Object Data:"), WS_VISIBLE | WS_CHILD | SS_LEFT,10,50,120,20,hWnd,NULL,NULL,NULL);
- CreateWindow(_T("Edit"),g_Formats[g_Selected[0].id].c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,10,70,310,20,hWnd,(HMENU) E_EVENT_OBJFORMAT,NULL,NULL);
- //Second static text and edit control for the second window
- CreateWindow(_T("static"),_T("Vehicle Data:"), WS_VISIBLE | WS_CHILD | SS_LEFT,10,105,120,20,hWnd,NULL,NULL,NULL);
- CreateWindow(_T("Edit"),g_Formats[g_Selected[2].id].c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,10,125,310,20,hWnd,(HMENU) E_EVENT_VEHFORMAT,NULL,NULL);
- //The continue button
- CreateWindow(_T("Button"),_T("Continue"), WS_VISIBLE | WS_CHILD | WS_BORDER,10, 170, 310, 35,hWnd, (HMENU) E_EVENT_CONTINUE,NULL,NULL);
- return 0;
- }
- case WM_COMMAND:
- {
- switch(LOWORD(wParam))
- {
- case E_EVENT_CONTINUE:
- {
- //Create our "Convert" object
- Convert Map;
- //Create 2 wide strings, and 3 ints
- wstring Str[2];
- int len[3];
- //Store the length of our 3 windows
- //(E_EVENT_TEXTBOX,E_EVENT_OBJFORMAT, and E_EVENT_VEHFORMAT)
- len[0] = GetWindowTextLength(GetDlgItem(Main, E_EVENT_TEXTBOX));
- len[1] = GetWindowTextLength(GetDlgItem(Other, E_EVENT_OBJFORMAT));
- len[2] = GetWindowTextLength(GetDlgItem(Other, E_EVENT_VEHFORMAT));
- //Resize our string object to a size thats appropriate for storing
- //Our three window messages.
- g_TextMessage.clear();
- g_TextMessage.resize(len[0], NULL);
- Str[0].resize(len[1], NULL);
- Str[1].resize(len[2], NULL);
- /*
- Here we get our three window messages. Since the third parameter
- wants a LPWSTR (long pointer to a wide string) and .c_str() returns
- a LPCWSTR (long pointer to a constant wide string) we have to type
- cast.
- */
- GetDlgItemText(Main,E_EVENT_TEXTBOX, (LPWSTR) g_TextMessage.c_str(), len[0]+1);
- GetDlgItemText(Other,E_EVENT_OBJFORMAT, (LPWSTR) Str[0].c_str(), len[1]+1);
- GetDlgItemText(Other,E_EVENT_VEHFORMAT, (LPWSTR) Str[1].c_str(), len[2]+1);
- /*
- We use our "Map" object's member function ParseInput to convert our map
- that is now inside g_TextMessage. We pass the map, and the object and
- vehicle formats, and finally a bool to check if its a Race map or a 1.0 map
- */
- Map.ParseInput(WstrToStr(g_TextMessage), WstrToStr(Str[0]), WstrToStr(Str[1]), (g_Selected[1].id == E_MENU_FI_MTARACE)?(true):(false));
- //After the map is done we hide our second window
- ShowWindow(Other, SW_HIDE);
- return 0;
- }
- }
- return 0;
- }
- case WM_CLOSE:
- {
- //Hide our second window if someone closes it
- ShowWindow(Other,SW_HIDE);
- return 0;
- }
- }
- return DefWindowProc(hWnd,Msg,wParam,lParam);
- }
- //callback for our first window
- LRESULT CALLBACK MainProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
- {
- switch(Msg)
- {
- case WM_CREATE:
- {
- //Creating the menus
- HMENU hMenuBar = CreateMenu();
- HMENU hFile = CreateMenu();
- HMENU hOptions = CreateMenu();
- HMENU hVehOrObj = CreateMenu();
- HMENU hVehFormat = CreateMenu();
- HMENU hObjFormat = CreateMenu();
- HMENU hInputFormat = CreateMenu();
- //Appending itemps to the hFile menu
- AppendMenu(hFile, MF_STRING, E_MENU_OPEN, _T("Open Map"));
- AppendMenu(hFile, MF_STRING, E_MENU_SAVE, _T("Save Map"));
- AppendMenu(hFile, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hFile, MF_STRING, E_MENU_QUIT, _T("Quit"));
- //Append items to hInputFormat
- AppendMenu(hInputFormat, MF_STRING, E_MENU_FI_MTA1, _T("MTA 1.0"));
- AppendMenu(hInputFormat, MF_STRING, E_MENU_FI_MTARACE, _T("MTA Race"));
- //Appending items to hObjFormat
- AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_CREATEOBJECT, _T("CreateObject"));
- AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_INCOGNITO, _T("Streamer plugin (incognito)"));
- AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_XSTREAMER, _T("xStreamer plugin"));
- AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_XOBJECTS, _T("xObjects"));
- AppendMenu(hObjFormat, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_CUSTOM, _T("Custom format"));
- //Appending itemps to hVehFormat
- AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_NONE, _T("No vehicles"));
- AppendMenu(hVehFormat, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_ADDSTATICV, _T("AddStaticVehicle"));
- AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_ADDSTATICVEX, _T("AddStaticVehicleEx"));
- AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_CREATEVEHICLE, _T("CreateVehicle"));
- AppendMenu(hVehFormat, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_VCUSTOM, _T("Custom format"));
- //Appending items to menu hVehOrObj
- AppendMenu(hVehOrObj, MF_POPUP, (UINT_PTR) hObjFormat, _T("Objects"));
- AppendMenu(hVehOrObj, MF_POPUP, (UINT_PTR) hVehFormat, _T("Vehicles"));
- //Append the hInputFormat and hVehOrObj menus to hOptions
- AppendMenu(hOptions, MF_POPUP, (UINT_PTR) hInputFormat, _T("Input"));
- AppendMenu(hOptions, MF_POPUP, (UINT_PTR) hVehOrObj, _T("Output"));
- //Append hFile and hOptions to the hMenuBar
- AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hFile, _T("File"));
- AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hOptions, _T("Options"));
- //Apply the menu to our winodw
- SetMenu(hWnd,hMenuBar);
- //Assign our starting menu selection data to our tracking arrays
- g_Selected[0].menu = hObjFormat;
- g_Selected[0].id = E_MENU_FO_CREATEOBJECT;
- g_Selected[1].menu = hInputFormat;
- g_Selected[1].id = E_MENU_FI_MTA1;
- g_Selected[2].menu = hVehFormat;
- g_Selected[2].id = E_MENU_FO_ADDSTATICV;
- //Actually "select" our menu items
- CheckMenuItem(hObjFormat,E_MENU_FO_CREATEOBJECT, MF_CHECKED);
- CheckMenuItem(hInputFormat,E_MENU_FI_MTA1, MF_CHECKED);
- CheckMenuItem(hVehFormat,E_MENU_FO_ADDSTATICV, MF_CHECKED);
- //Create our textbox and button
- CreateWindow(_T("Edit"),g_TextMessage.c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_MULTILINE|ES_WANTRETURN|WS_VSCROLL,0,0,WINDOW_WIDTH-5,200,hWnd,(HMENU) E_EVENT_TEXTBOX,NULL,NULL);
- CreateWindow(_T("Button"),_T("Convert!"), WS_CHILD | WS_VISIBLE | WS_BORDER,0,200,WINDOW_WIDTH-4,53,hWnd,(HMENU) E_EVENT_CONVERT,NULL,NULL);
- return 1;
- }
- case WM_COMMAND:
- {
- int val = LOWORD(wParam);
- switch(val)
- {
- case E_MENU_FO_CREATEOBJECT:
- case E_MENU_FO_INCOGNITO:
- case E_MENU_FO_XSTREAMER:
- case E_MENU_FO_XOBJECTS:
- case E_MENU_FO_CUSTOM:
- {
- CheckMenuItem(g_Selected[0].menu, g_Selected[0].id, MF_UNCHECKED);
- CheckMenuItem(g_Selected[0].menu, val, MF_CHECKED);
- SetDlgItemText(Other,E_EVENT_OBJFORMAT,g_Formats[val].c_str());
- g_Selected[0].id = val;
- return 0;
- }
- case E_MENU_FO_NONE:
- case E_MENU_FO_ADDSTATICV:
- case E_MENU_FO_ADDSTATICVEX:
- case E_MENU_FO_VCUSTOM:
- case E_MENU_FO_CREATEVEHICLE:
- {
- CheckMenuItem(g_Selected[2].menu, g_Selected[2].id, MF_UNCHECKED);
- CheckMenuItem(g_Selected[2].menu, val, MF_CHECKED);
- SetDlgItemText(Other,E_EVENT_VEHFORMAT,g_Formats[val].c_str());
- g_Selected[2].id = val;
- return 0;
- }
- case E_MENU_FI_MTARACE:
- case E_MENU_FI_MTA1:
- {
- CheckMenuItem(g_Selected[1].menu, g_Selected[1].id, MF_UNCHECKED);
- CheckMenuItem(g_Selected[1].menu, val, MF_CHECKED);
- g_Selected[1].id = val;
- return 0;
- }
- case E_MENU_QUIT:
- {
- PostQuitMessage(0);
- return 0;
- }
- case E_EVENT_TEXTBOX:
- {
- if(HIWORD(wParam) == EN_CHANGE)
- {
- //change save variable and give a quit warning
- }
- return 0;
- }
- case E_EVENT_CONVERT:
- {
- //Show second window, hide main window
- ShowWindow(Other,SW_SHOW);
- return 0;
- }
- case E_MENU_SAVE:
- {
- OpenFileDialog(Main, E_EVENT_TEXTBOX, _T("All files (*.*)\0*.*\0\0"),true,false);
- return 0;
- }
- case E_MENU_OPEN:
- {
- OpenFileDialog(Main, E_EVENT_TEXTBOX, _T("MTA Map files (*.map)\0*.map\0\0"));
- return 0;
- }
- }
- return 0;
- }
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- }
- }
- return DefWindowProc(hWnd,Msg,wParam,lParam);
- }
- int OpenFileDialog(HWND hWnd, int item, const TCHAR filter[], bool save, bool append)
- {
- //Declare our OPENFILENAME object and fill it out
- OPENFILENAME OFN = {0};
- TCHAR szFileName[MAX_PATH] = {0};
- OFN.lStructSize = sizeof(OPENFILENAME);
- OFN.Flags = OFN_EXPLORER;
- OFN.lpstrFilter = filter;
- OFN.lpstrFile = szFileName;
- OFN.nMaxFile = MAX_PATH;
- OFN.hwndOwner = hWnd;
- wifstream iFile;
- wofstream oFile;
- if(save)
- {
- //If save is true, open the save dialog (passing our OFN object's address)
- if(GetSaveFileName(&OFN))
- {
- //Open the file that the user specified in the save dialog
- iFile.open(szFileName);
- //Get the size of the window
- int len = GetWindowTextLength(GetDlgItem(hWnd,item));
- wstring Text;
- Text.resize(len+2);
- //Get the specified item's text.
- GetDlgItemText(hWnd, item, (LPWSTR) Text.c_str(),len+1);
- //Check if its open (to see if it exists or not)
- if(iFile.is_open())
- {
- //close the file, cause we dont need it
- iFile.close();
- //if user doesnt want append to the existing file
- if(!append)
- {
- //Ask if he wants to overwrite the existing file
- int YON = MessageBox(hWnd,_T("This file already exists! Do you want to overwrite it?"),_T("Warning!"),MB_YESNO);
- switch(YON)
- {
- //If he doesnt, return false and get out.
- case IDNO:
- {
- return 0;
- }
- //If he does
- case IDYES:
- {
- //Open our file for writing, write the text, and close it.
- oFile.open(szFileName);
- oFile << Text;
- oFile.close();
- return 1;
- }
- }
- }
- else
- {
- //We open a file for appending, append text to it, and close it
- oFile.open(szFileName, ios::app);
- oFile << Text;
- oFile.close();
- return 1;
- }
- }
- else
- {
- //close iFile, we dont need it anymore
- iFile.close();
- //Open our file, write to it, and close it
- oFile.open(szFileName, ios::out);
- oFile << Text;
- oFile.close();
- return 1;
- }
- }
- }
- else
- {
- OFN.Flags = OFN_PATHMUSTEXIST | OFN_READONLY;
- if(GetOpenFileName(&OFN))
- {
- iFile.open(szFileName);
- wstring
- tmp,
- Text;
- while(!iFile.eof())
- {
- getline(iFile, tmp);
- Text += tmp;
- }
- SetDlgItemText(hWnd,item,Text.c_str());
- return 1;
- }
- }
- return 0;
- }
- //Convert a wide string (wstring) to string
- string WstrToStr(wstring str)
- {
- string tmp;
- tmp.resize(str.size(),NULL);
- tmp.assign(str.begin(),str.end());
- return tmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement