"foo.cpp"for example, the assumption should be that this is in the current directory, named ".", and that's the directory to open a DirStream for, then iterate looking for "foo.cpp".
If you specify a filename of
"108/code/coolstuff/foo.cpp"then presumably the directory is "108/code/coolstuff/" and the filename is foo.cpp. Again, open a DirStream object and iterate looking for foo.cpp. This approach works, it requires iterating a directory for a filename [of course if the user passes a directory name, then no iteration is needed].
You can use the DirStream::fail() member function to determine if opening/constructing a DirStream succeeds. If the open fails, the probably the user has specified a file. You can strip off everything after the last "/" as the filename and everything before the last "/" as the directory name.
The string function rfind is useful for this, the code below should print 10, the index of the last slash.
The key idea here is that to get a DirEntry object for a file, you must iterate over the directory in which the file is located. There's no other way to do this using DirStream and DirEntry classes provided. You can certainly get the file directly by exploiting the implementation of the DirStream and DirEntry classes, but who wants to do this?
Iterate my friends
Use the default copy constructor which will work, e.g.,
dirptr.cpp shows this approach.
The listFiles function is the function that will approximate the functionality of the scandir functions shown in the assignment.