Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FileSystem
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Wichmann
FileSystem
Commits
dc932fe2
Commit
dc932fe2
authored
6 months ago
by
Jan Wichmann
Browse files
Options
Downloads
Plain Diff
merge form workingdir
parents
45883bbf
e13f15ce
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+0
-0
0 additions, 0 deletions
.gitignore
FileSystem.cpp
+38
-12
38 additions, 12 deletions
FileSystem.cpp
FileSystem.h
+2
-6
2 additions, 6 deletions
FileSystem.h
Tests/FileSystemTest.cpp
+55
-6
55 additions, 6 deletions
Tests/FileSystemTest.cpp
with
95 additions
and
24 deletions
.gitignore
-324 B (-30%)
View file @
dc932fe2
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
This diff is collapsed.
Click to expand it.
FileSystem.cpp
+
38
−
12
View file @
dc932fe2
...
...
@@ -40,15 +40,39 @@ void FileSystem::listContentDir() {
}
}
void
FileSystem
::
changeDir
(
std
::
string
dir_name
)
{
if
(
dir_name
[
dir_name
.
size
()
-
1
]
==
'/'
)
{
dir_name
.
pop_back
();
}
auto
target_dir
=
currentDirectory
->
subdirs
.
find
(
dir_name
);
if
(
target_dir
!=
currentDirectory
->
subdirs
.
end
())
{
currentDirectory
=
target_dir
->
second
;
}
else
{
std
::
cout
<<
"Directory '"
<<
dir_name
<<
"' dosen't exists!"
<<
std
::
endl
;
void
FileSystem
::
changeDir
(
const
std
::
string
&
dir_name
)
{
std
::
vector
<
std
::
string
>
path_to_process
=
this
->
splitInput
(
dir_name
,
'/'
);
//Absoulte Search
if
(
!
path_to_process
.
empty
())
{
if
(
path_to_process
[
0
]
==
"root"
)
{
Directory
*
backup_dir
=
currentDirectory
;
currentDirectory
=
root
;
for
(
int
i
=
1
;
i
<
path_to_process
.
size
();
i
++
)
{
auto
target_dir
=
currentDirectory
->
subdirs
.
find
(
path_to_process
[
i
]);
if
(
target_dir
!=
currentDirectory
->
subdirs
.
end
())
{
currentDirectory
=
target_dir
->
second
;
}
else
{
std
::
cout
<<
"Directory '"
<<
dir_name
<<
"' dosen't exists!"
<<
std
::
endl
;
currentDirectory
=
backup_dir
;
break
;
}
}
}
//Relative Search
else
{
for
(
auto
&
d
:
path_to_process
)
{
if
(
d
==
".."
)
{
go_to_parent_dir
();
}
else
{
auto
target_dir
=
currentDirectory
->
subdirs
.
find
(
d
);
if
(
target_dir
!=
currentDirectory
->
subdirs
.
end
())
{
currentDirectory
=
target_dir
->
second
;
}
else
{
std
::
cout
<<
"Directory '"
<<
dir_name
<<
"' dosen't exists!"
<<
std
::
endl
;
}
}
}
}
}
}
...
...
@@ -79,7 +103,7 @@ void FileSystem::processCmd(const std::string &command) {
currentDirectory
=
root
;
}
}
else
if
(
cmd
==
"cd.."
)
{
else
if
(
cmd
==
"cd
.."
)
{
go_to_parent_dir
();
}
else
if
(
cmd
==
"exit"
)
{
...
...
@@ -107,7 +131,9 @@ std::vector<std::string> FileSystem::splitInput(const std::string &input, char d
// Eingabe in Wörter (Tokens) aufteilen
while
(
std
::
getline
(
stream
,
token
,
delimiter
))
{
tokens
.
push_back
(
token
);
if
(
!
token
.
empty
())
{
tokens
.
push_back
(
token
);
}
}
return
tokens
;
}
...
...
@@ -116,9 +142,9 @@ void FileSystem::go_to_parent_dir() {
currentDirectory
=
currentDirectory
->
parent
;
}
FileSystem
::
Directory
*
FileSystem
::
getCurrentDirectory
()
const
{
return
currentDirectory
;
}
This diff is collapsed.
Click to expand it.
FileSystem.h
+
2
−
6
View file @
dc932fe2
...
...
@@ -25,10 +25,6 @@ private:
Directory
*
root
;
Directory
*
currentDirectory
;
public
:
Directory
*
getCurrentDirectory
()
const
;
private
:
bool
running
=
true
;
public
:
...
...
@@ -37,11 +33,11 @@ public:
void
createRoot
();
void
createSubDir
(
const
std
::
string
&
dir_name
);
void
listContentDir
();
void
changeDir
(
std
::
string
dir_name
);
void
changeDir
(
const
std
::
string
&
dir_name
);
void
go_to_parent_dir
();
void
text_interface
();
bool
directoryExists
(
const
std
::
string
&
dir_name
);
Directory
*
getCurrentDirectory
()
const
;
};
...
...
This diff is collapsed.
Click to expand it.
Tests/FileSystemTest.cpp
+
55
−
6
View file @
dc932fe2
...
...
@@ -16,6 +16,7 @@ bool FileSystem::directoryExists(const std::string& dir_name) {
}
TEST_F
(
FileSystemTest
,
CreateRootTest
)
{
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root"
);
}
...
...
@@ -25,12 +26,62 @@ TEST_F(FileSystemTest, CreateSubDirectoryTest) {
ASSERT_TRUE
(
fs
->
directoryExists
(
"testDir"
));
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryTest
)
{
fs
->
createSubDir
(
"testDir"
);
fs
->
changeDir
(
"testDir"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/testDir"
);
// cd
TEST_F
(
FileSystemTest
,
ChangeDirectoryValidPaths
)
{
fs
->
createSubDir
(
"a/b/c"
);
fs
->
changeDir
(
"a/b"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b"
);
// Absoluter Pfad prüfen
fs
->
changeDir
(
"../b/c"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b/c"
);
// Absoluter Pfad prüfen
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryAbsolutePaths
)
{
fs
->
createSubDir
(
"a/b/c"
);
fs
->
changeDir
(
"root/a"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a"
);
// Absoluter Pfad zur Root-Struktur
fs
->
changeDir
(
"root/a/b/c"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b/c"
);
// Absoluter Pfad in tiefere Struktur
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryRootDirectory
)
{
fs
->
createSubDir
(
"a/b/c"
);
fs
->
changeDir
(
"a/b/c"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b/c"
);
// Absoluter Pfad in tiefes Verzeichnis
fs
->
changeDir
(
"root"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root"
);
// Wechsel zurück in Root
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryUsingDot
)
{
fs
->
createSubDir
(
"a/b"
);
fs
->
changeDir
(
"a/b/."
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b"
);
// Wechsel mit `.` bleibt im gleichen Verzeichnis
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryUsingDoubleDot
)
{
fs
->
createSubDir
(
"a/b"
);
fs
->
changeDir
(
"a/b"
);
fs
->
changeDir
(
".."
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a"
);
// Wechsel zu übergeordnetem Verzeichnis
fs
->
changeDir
(
".."
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root"
);
// Wechsel zurück zur Root
}
TEST_F
(
FileSystemTest
,
ChangeDirectoryDeepRelativePaths
)
{
fs
->
createSubDir
(
"a/b/c/d/e"
);
fs
->
changeDir
(
"a/b/c/d"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b/c/d"
);
// Wechsel in tiefes Verzeichnis
fs
->
changeDir
(
"../../b/c"
);
ASSERT_EQ
(
fs
->
getCurrentDirectory
()
->
name
,
"root/a/b/c"
);
// Relativer Pfad zurück und nach vorne
}
// ls
TEST_F
(
FileSystemTest
,
ListContentTest
)
{
fs
->
createSubDir
(
"dir1"
);
fs
->
createSubDir
(
"dir2"
);
...
...
@@ -43,10 +94,8 @@ TEST_F(FileSystemTest, ListContentTest) {
std
::
cout
.
rdbuf
(
oldCoutBuffer
);
std
::
string
output
=
outputBuffer
.
str
();
std
::
string
expectedOutput
=
"dir1
\n
"
"dir2
\n
"
;
ASSERT_EQ
(
output
,
expectedOutput
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment