Skip to content

Commit 6bf60e5

Browse files
committed
fixed issue #1 moved to v1.0
1. Added file filter so only .maz files are displayed 2. Fixed problem on linux where selection numbers were relative to root as opposed to local directory
1 parent cfacc57 commit 6bf60e5

File tree

1 file changed

+27
-75
lines changed

1 file changed

+27
-75
lines changed

mmSim.cpp

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
//============================================================================
22
// Name : mmSim.cpp
33
// Author : Elis Pogace
4-
// Version : 0.1
4+
// Version : 1.0
55
// Created on : Jun 11, 2014
66
// Description : Micro mouse simulation software for SFHS robotics students
77
//============================================================================
88

9-
109
#include"mazeBase.h"
1110
#include"mazeConst.h"
1211
#include"gui.h"
1312
#include"studentAi.h"
1413
#include"time.h"
15-
16-
#define win32 1
14+
#include"string.h"
1715

1816
#include <dirent.h>
1917
#include<iostream>
@@ -23,31 +21,15 @@
2321

2422
using namespace cv;
2523

26-
static int filter(const struct dirent* input)
27-
{
28-
const char* ext = string(".maz").c_str();
29-
30-
if (input->d_name[0] == '.')
31-
return 0;
32-
else
33-
{
34-
size_t j = (strlen(input->d_name)-4);
35-
for (int i = 0; j < (strlen(input->d_name)); i++, j++)
36-
{
37-
if (ext[i] != (input->d_name[j]))
38-
return 0;
39-
}
40-
return 1;
41-
}
42-
}
4324

4425
int main()
4526
{
27+
4628
int userInput;
4729

4830
struct baseMapNode start[MAZE_WIDTH][MAZE_HEIGHT];
4931
struct mouseData mouse;
50-
FILE *mazeFile;
32+
FILE *mazeFile = NULL;
5133

5234
//new maze construction
5335
newMazeArea(start, MAZE_WIDTH, MAZE_HEIGHT);
@@ -103,78 +85,48 @@ int main()
10385
}
10486
else if(userInput == 'l')
10587
{
106-
#ifdef win32
10788
DIR *local = opendir(".");
10889
struct dirent *dircontents = readdir(local);
10990
int selection;
91+
int fileNum =0;
92+
vector<std::string> fileNames;
11093
std::ostringstream buff ("");
11194

95+
fileNames.clear();
11296
do{
113-
buff << telldir(local) << ": " << dircontents->d_name; //setup output string
114-
cvPrint(&guiText,buff.str());
115-
cout << buff.str() <<endl;
116-
buff.str("");//clear string buffer for next loop
117-
buff.clear();
97+
if(strstr(dircontents->d_name, ".maz")) //check to see if it is a maze file
98+
{
99+
//print selection number and filename
100+
buff << fileNum+1 << ": " << dircontents->d_name;
101+
fileNames.push_back(dircontents->d_name);
102+
cvPrint(&guiText,buff.str().c_str());
103+
fileNum++;
104+
105+
//clear string buffer for next loop
106+
buff.str("");
107+
buff.clear();
108+
}
118109
dircontents = readdir(local);
119110
}while(dircontents != NULL);
120111

121112
do {
122113
cout << endl << "Select file to load: " << endl;
123-
selection = atoi(cvIn(&guiText).c_str());
124-
rewinddir(local);
125-
seekdir(local, selection - 1);
126-
dircontents = readdir(local);
114+
selection = atoi(cvIn(&guiText).c_str())-1;
115+
if(selection >= 0 && selection < fileNum) //check if user input is within range
116+
mazeFile = fopen(fileNames.at(selection).c_str(), "r");
117+
else
118+
mazeFile = NULL;
127119

128120
if(!cvGetWindowHandle(DISPLAY_WINDOW_NAME)) break; //if the user closed the window break
129-
} while (dircontents == NULL || dircontents->d_type != 0);
121+
} while (mazeFile == NULL);
130122
cvPrint(&guiText, "");
131-
mazeFile = fopen(dircontents->d_name, "r");
132-
if(mazeFile == NULL)
133-
{
134-
exit(101);
135-
}
123+
136124
readMazeFromFile(mazeFile, start);
125+
137126
cout << "Maze loaded" << endl;
138127

139128
fclose(mazeFile);
140129
redrawMaze(&image, start);
141-
#else
142-
struct dirent **dircontents;
143-
string choices[20];
144-
int dirnum, selection;
145-
146-
cout << endl << "Select file to load: " << endl;
147-
148-
dirnum = scandir("./", &dircontents, filter, alphasort);
149-
if (dirnum >= 0)
150-
{
151-
for (int i = 0; i < dirnum; i++)
152-
{
153-
choices[i].assign(dircontents[i]->d_name);
154-
cout << i << ") " << choices[i] << endl;
155-
}
156-
}
157-
158-
cout << endl;
159-
160-
//load file
161-
cin >> selection;
162-
163-
if (selection <= dirnum && selection <= 20)
164-
{
165-
mazeFile = fopen(choices[selection].c_str(), "r");
166-
if(mazeFile != NULL)
167-
{
168-
readMazeFromFile(mazeFile, start);
169-
cout << "Maze loaded" << endl;
170-
}
171-
fclose(mazeFile);
172-
redrawMaze(&image, start);
173-
}
174-
175-
else
176-
cout << "Invalid selection, try another action" << endl;
177-
#endif
178130
}
179131

180132
else if(userInput == 'r')

0 commit comments

Comments
 (0)