FreeNOS
DirectoryScanner.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <dirent.h>
21#include <string.h>
22#include <errno.h>
23#include <String.h>
24#include "ExternalTest.h"
25#include "TestSuite.h"
26#include "DirectoryScanner.h"
27
29{
30 m_argc = argc;
31 m_argv = argv;
32}
33
38
39int DirectoryScanner::scan(const char *path)
40{
41 DIR *d;
42 struct dirent *dent;
43 char subPath[255];
44
45 // Attempt to open the target directory.
46 if (!(d = opendir(path)))
47 {
48 printf("%s: failed to open '%s': %s\r\n",
49 m_argv[0], path, strerror(errno));
50 return EXIT_FAILURE;
51 }
52 // Read directory.
53 while ((dent = readdir(d)))
54 {
55 snprintf(subPath, sizeof(subPath), "%s/%s", path, dent->d_name);
56 String str = subPath;
57
58 // Check filetype
59 switch (dent->d_type)
60 {
61 // Directory
62 case DT_DIR:
63 if (dent->d_name[0] != '.')
64 scan(subPath);
65 break;
66
67 // Regular file
68 case DT_REG:
69 if (str.endsWith((const char *)"Test"))
70 {
71 ExternalTest *test = new ExternalTest(subPath, m_argc, m_argv);
72 if (!m_externalTests.insert(test))
73 {
74 printf("%s: failed to add test '%s' to internal Index\n",
75 m_argv[0], path);
76 return EXIT_FAILURE;
77 }
78 }
79 break;
80
81 default:
82 break;
83 }
84 }
85 // Close it.
86 closedir(d);
87 return EXIT_SUCCESS;
88}
char ** m_argv
Program argument values.
int scan(const char *path)
Scan filesystem path for tests.
DirectoryScanner(int argc, char **argv)
Constructor.
Index< ExternalTest, MaximumExternalTests > m_externalTests
External tests that are detected.
int m_argc
Program argument count.
~DirectoryScanner()
Destructor.
Represents external test program.
void deleteAll()
Removes and delete()'s all items.
Definition Index.h:166
virtual bool insert(Size &position, T *item)
Adds the given item, if possible.
Definition Index.h:60
Abstraction of strings.
Definition String.h:42
bool endsWith(const String &suffix) const
Tests if this String ends with the specified suffix.
Definition String.cpp:210
#define DT_DIR
This is a directory.
Definition dirent.h:44
C struct dirent * readdir(DIR *dirp)
Read a directory.
Definition readdir.cpp:21
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int errno
The lvalue errno is used by many functions to return error values.
#define EXIT_SUCCESS
Successful termination.
Definition stdlib.h:33
#define EXIT_FAILURE
Unsuccessful termination.
Definition stdlib.h:36
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
#define DT_REG
This is a regular file.
Definition dirent.h:50
C DIR * opendir(const char *dirname)
Open directory associated with file descriptor.
Definition opendir.cpp:27
C int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition snprintf.cpp:22
C int closedir(DIR *dirp)
Close a directory stream.
Definition closedir.cpp:22
A type representing a directory stream.
Definition dirent.h:95
Represents a directory entry.
Definition dirent.h:65
u8 d_type
Type of file.
Definition dirent.h:70
char d_name[DIRLEN]
Name of entry.
Definition dirent.h:67