Skip to content

Commit 2057111

Browse files
committed
clang_format added, filesystem updated
1 parent 9c9db76 commit 2057111

8 files changed

+199
-27
lines changed

CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,20 @@ target_link_libraries(heap_and_stack_memory_layout_of_C_programs)
425425
add_executable(memory_checking src/memory_checking.cpp)
426426
target_link_libraries(memory_checking)
427427

428-
429428
add_executable(tricky_questions src/tricky_questions.cpp)
430429
target_link_libraries(tricky_questions)
431430

432431
add_executable(metaprogramming src/metaprogramming.cpp)
433432
target_link_libraries(metaprogramming)
434433

434+
add_executable(regex_mathch_search src/regex_mathch_search.cpp)
435+
target_link_libraries(regex_mathch_search)
436+
437+
add_executable(filesystem src/filesystem.cpp)
438+
target_link_libraries(filesystem)
439+
440+
add_executable(optional src/optional.cpp)
441+
target_link_libraries(optional)
435442

436443
if( ${CMAKE_GNU_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION} GREATER_EQUAL 13)
437444
add_executable(printing_with_format src/printing_with_format.cpp)

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
FROM ubuntu:20.04
44

5-
6-
MAINTAINER Behnam Asadi [email protected]
5+
LABEL maintainer="Behnam Asadi [email protected]"
76

87

98
# this is for timezone config

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
5555
* [Algorithms library](src/algorithms_library.cpp)
5656
* [Assert](docs/assert.md)
5757
* [Attribute [[ attribute-list ]] ](docs/attribute.md)
58-
* [Basic IO Operation, filesystem, Streams, Reading/Writing Files, Formating Output, cin, scanf, gets, getline, printf](docs/basic_IO_operation.md)
58+
* [Basic IO Operation, Streams, Reading/Writing Files, Formating Output, cin, scanf, gets, getline, printf](docs/basic_IO_operation.md)
5959
* [Bitset, Bit field, Bitwise Operations](docs/bitset_bit_field_bitwise_operations.md)
6060
* [Callbacks, Callable Objects, std::function, std::bind, std::invoke, Lambda](docs/callbacks.md)
6161
* [Clock, Date, Time](src/date_time.cpp)
@@ -76,6 +76,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
7676
* [Error Handling](docs/error_handling.md)
7777
* [Exception Handling, noexcept](docs/exception_handling.md)
7878
* [Extern Variables, Extern Functions](src/extern/variable/main.cpp)
79+
* [File System ](src/filesystem.cpp)
7980
* [Forking](docs/fork.md)
8081
* [Forwarding Reference](docs/forward.md)
8182
* [Functions, Extern Function, Function Objects, Function Pointer, Inline Functions](docs/functions.md)
@@ -88,8 +89,10 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
8889
* [Macro](src/macro.cpp)
8990
* [Memory Error Detection With Memory Address Sanitizer and Valgrind](docs/memory_leaking_valgrind.md)
9091
* [Nested Namespaces, Unnamed/Anonymous Namespaces](docs/nested_unnamed_anonymous_namespaces.md)
92+
* [Optional](src/optional.cpp)
9193
* [Parameter Pack Expansion ...](docs/parameter_pack_expansion_(...).md)
9294
* [Register Keyword](docs/register.md)
95+
* [Regex](src/regex_mathch_search.cpp)
9396
* [Pseudo-random Number Generation, Distributions](docs/random_number.md)
9497
* [Raw Pointers, Smart (Shared, Unique, Weak) Pointers, Reference, addressof, reference_wrapper, std::ref](docs/pointers.md)
9598
* [Return, Abort, Exit](docs/return_abort_exit.md)

clang_format.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Regexp for grep to only choose some file extensions for formatting
2+
exts="\.\(cpp\|hpp\|cc\|hh\|c\|h\)$"
3+
4+
# The formatter to use
5+
formatter=`clang-format.exe`
6+
7+
# Check availability of the formatter
8+
if [ -z "$formatter" ]
9+
then
10+
1>&2 echo "$formatter not found. Pre-commit formatting will not be done."
11+
exit 0
12+
fi
13+
14+
# Format staged files
15+
for file in `git diff --cached --name-only --diff-filter=ACMR | grep $exts`
16+
do
17+
echo "Formatting $file"
18+
# Get the file from index
19+
git show ":$file" > "$file.tmp"
20+
# Format it
21+
"$formatter" -i "$file.tmp"
22+
# Create a blob object from the formatted file
23+
hash=`git hash-object -w "$file.tmp"`
24+
# Add it back to index
25+
git update-index --add --cacheinfo 100644 "$hash" "$file"
26+
27+
# Formatting original file
28+
# echo "Formatting original file "."$file"
29+
"$formatter" -i "$file"
30+
31+
# Remove the tmp file
32+
rm "$file.tmp"
33+
done
34+
35+
# If no files left in index after formatting - fail
36+
ret=0
37+
if [ ! "`git diff --cached --name-only`" ]; then
38+
1>&2 echo "No files left after formatting"
39+
exit 1
40+
fi

src/basic_IO_operation_filesystem_streams_reading_writing_files_formating_output_cin_cout_scanf_printf_gets_puts_getline.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -695,27 +695,4 @@ void fastIO() {
695695
slower because it forces a flushing stream, which is usually unnecessary
696696
*/
697697
}
698-
699-
void filesystemEample() {
700-
701-
const auto FilePath{"FileToCopy"};
702-
703-
// If any filepath exists
704-
if (std::filesystem::exists(FilePath)) {
705-
const auto FileSize{std::filesystem::file_size(FilePath)};
706-
707-
std::filesystem::path tmpPath{"/tmp"};
708-
709-
// If filepath is available or not
710-
if (std::filesystem::space(tmpPath).available > FileSize) {
711-
712-
// Create Directory
713-
std::filesystem::create_directory(tmpPath.append("example"));
714-
715-
// Copy File to file path
716-
std::filesystem::copy_file(FilePath, tmpPath.append("newFile"));
717-
}
718-
}
719-
}
720-
721698
int main() {}

src/filesystem.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <algorithm>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
6+
void lexicallyNormalRelativePath() {
7+
8+
std::filesystem::path foo =
9+
std::filesystem::path(std::filesystem::current_path());
10+
11+
std::cout << foo.lexically_normal() << std::endl;
12+
// std::cout << foo.lexically_proximate() << std::endl;
13+
std::cout << std::filesystem::path(std::filesystem::current_path() / "../../")
14+
.lexically_normal()
15+
<< std::endl;
16+
}
17+
18+
void filesystemExample() {
19+
20+
const auto FilePath{"FileToCopy"};
21+
22+
// If any filepath exists
23+
if (std::filesystem::exists(FilePath)) {
24+
const auto FileSize{std::filesystem::file_size(FilePath)};
25+
26+
std::filesystem::path tmpPath{"/tmp"};
27+
28+
// If filepath is available or not
29+
if (std::filesystem::space(tmpPath).available > FileSize) {
30+
31+
// Create Directory
32+
std::filesystem::create_directory(tmpPath.append("example"));
33+
34+
// Copy File to file path
35+
std::filesystem::copy_file(FilePath, tmpPath.append("newFile"));
36+
}
37+
}
38+
39+
// fs::path("a/./b/..").lexically_normal() == "a/"
40+
}
41+
42+
void directoryIterator() {
43+
44+
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
45+
"foo/dir1");
46+
47+
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
48+
"foo/dir1/child1");
49+
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
50+
"foo/dir2");
51+
52+
std::filesystem::current_path(std::filesystem::temp_directory_path() / "foo");
53+
54+
std::ofstream{std::filesystem::current_path() / "foo/dir1/file1.txt"};
55+
std::ofstream{std::filesystem::current_path() / "foo/dir2/file2.txt"};
56+
57+
for (auto const &iter :
58+
std::filesystem::directory_iterator{std::filesystem::current_path()}) {
59+
60+
/*
61+
Socket (S_IFSOCK)
62+
Symbolic link (S_IFLNK)
63+
Regular File (S_IFREG)
64+
Block special file (S_IFBLK)
65+
Directory (S_IFDIR)
66+
Character device (S_IFCHR)
67+
FIFO (named pipe) (S_IFIFO)
68+
*/
69+
70+
std::cout << iter.path() << std::endl;
71+
72+
if (std::filesystem::is_directory(iter)) {
73+
std::cout << iter.path().filename().string() << " is a directory"
74+
<< std::endl;
75+
}
76+
77+
if (std::filesystem::is_regular_file(iter)) {
78+
std::cout << iter.path().filename().string() << " is a file" << std::endl;
79+
}
80+
}
81+
82+
std::cout << "Iterating " << std::filesystem::current_path() << " recursively"
83+
<< '\n';
84+
85+
for (auto const &dir_entry : std::filesystem::recursive_directory_iterator{
86+
std::filesystem::current_path()}) {
87+
std::cout << dir_entry << '\n';
88+
}
89+
90+
std::ranges::for_each(
91+
std::filesystem::directory_iterator{std::filesystem::current_path()},
92+
[](const auto &dir_entry) { std::cout << dir_entry << '\n'; });
93+
94+
try {
95+
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
96+
"foo/dir1/child1");
97+
98+
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
99+
"foo/dir1");
100+
101+
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
102+
"foo/dir2");
103+
104+
} catch (std::error_code e) {
105+
std::cout << e.message() << std::endl;
106+
} catch (...) {
107+
std::cout << "exception" << std::endl;
108+
}
109+
}
110+
111+
int main(int argc, char **argv) {
112+
//
113+
114+
// directoryIterator();
115+
lexicallyNormalRelativePath();
116+
}

src/optional.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <optional>
2+
#include <string>
3+
#include <iostream>
4+
5+
std::optional<std::string> found(bool found =false)
6+
{
7+
if(found)
8+
return "something found!";
9+
else
10+
return std::nullopt;
11+
}
12+
13+
int main(int argc, char **argv)
14+
{
15+
std::cout << found(false).value_or("empty") << '\n';
16+
if( found(true).has_value() )
17+
{
18+
std::cout << found(true).value() << '\n';
19+
}
20+
}

src/regex_mathch_search.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <regex>
2+
3+
4+
5+
6+
7+
int main(int argc, char **argv)
8+
{
9+
10+
}

0 commit comments

Comments
 (0)