Skip to content

Commit

Permalink
Throw if file not usable
Browse files Browse the repository at this point in the history
  • Loading branch information
rosidae committed Sep 20, 2021
1 parent c224f7b commit c70711b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
30 changes: 13 additions & 17 deletions include/utl/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ namespace Files {
File.close();
return true;
}
class File_Unusable {}; void Throw_If_Not_Usable(
std::string File_Name
) {
if(!Usable_File(File_Name)) {throw File_Unusable();};
}
std::string Read_File(
std::string File_Name
) {
Throw_If_Not_Usable(File_Name);
std::ifstream File(File_Name);
std::string Line;
std::string Text;
Expand All @@ -28,7 +34,7 @@ namespace Files {
std::vector<std::string> Read_File_Lines(
std::string File_Name
) {
if(!Usable_File(File_Name)) { return std::vector<std::string>{""}; }
Throw_If_Not_Usable(File_Name);
std::ifstream File(File_Name);
std::string Line;
std::vector<std::string> Lines;
Expand All @@ -42,9 +48,10 @@ namespace Files {
int Write_File(
std::string File_Name,
std::string Output,
bool Append = false
bool Append = false,
bool Throw = false
) {
if(!Usable_File(File_Name)) { return -1; }
if(Throw) Throw_If_Not_Usable(File_Name);
std::ofstream File;
if(Append) {
File.open(File_Name, std::ios::app);
Expand All @@ -55,22 +62,11 @@ namespace Files {
File.close();
return 0;
}
int Find_And_Replace_File(
std::string File_Name,
std::string Find,
std::string Replace
) {
if(!Usable_File(File_Name)) { return -1; }
std::string Content = Read_File(File_Name);
Strings::Find_And_Replace_All(&Content, &Find, &Replace);
Write_File(File_Name, Content);
return 0;
}
double Size_Of_File_Double(
std::string File_Name,
int Size_Notation
) {
if(!Usable_File(File_Name)) { return -1; }
Throw_If_Not_Usable(File_Name);
std::ifstream File(File_Name, std::ios::binary | std::ios::ate);
double Size = File.tellg();
File.close();
Expand All @@ -85,7 +81,7 @@ namespace Files {
std::string File_Name,
int Size_Notation
) {
if(!Usable_File(File_Name)) { return -1; }
Throw_If_Not_Usable(File_Name);
std::ifstream File(File_Name, std::ios::binary | std::ios::ate);
int Size = File.tellg();
File.close();
Expand All @@ -100,7 +96,7 @@ namespace Files {
std::string File_Name,
int Count
) {
if(!Usable_File(File_Name)) { return ""; }
Throw_If_Not_Usable(File_Name);
std::ofstream File(File_Name);
int Index = 0;
while(Index != Count) {
Expand Down
3 changes: 0 additions & 3 deletions include/utl/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
#include <regex>

namespace Strings {
void Find_And_Replace_All(std::string* Original, std::string* Find, std::string* Replace) {
Original->replace(Original->find(Find->c_str())+1, Replace->c_str());
}
}
34 changes: 8 additions & 26 deletions include/utl/vecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// the datatype needs to be known at COMPILATION time

namespace Vecs {
template <typename T>
bool Includes(
template <typename T> bool Includes(
std::vector<T> Vec,
T Search
) {
Expand All @@ -16,14 +15,12 @@ namespace Vecs {
}
return false;
}
template <typename T>
std::vector<T> Head(
template <typename T> std::vector<T> Head(
std::vector<T> Vec
) {
return std::vector<T>{Vec[0]};
}
template <typename T>
std::vector<T> Tail(
template <typename T> std::vector<T> Tail(
std::vector<T> Vec
) {
std::vector<T> _Vec;
Expand All @@ -33,20 +30,17 @@ namespace Vecs {
}
return _Vec;
}
template <typename T>
std::vector<T> Last(
template <typename T> std::vector<T> Last(
std::vector<T> Vec
) {
return std::vector<T>{Vec[Vec.size()-1]};
}
template <typename T>
std::vector<T> Indexical_Size(
template <typename T> std::vector<T> Indexical_Size(
std::vector<T> Vec
) {
return Vec.size()-1;
}
template <typename T>
int Match_Count(
template <typename T> int Match_Count(
std::vector<T> Vec,
T To_Match
) {
Expand All @@ -56,8 +50,7 @@ namespace Vecs {
}
return _Count;
}
template <typename T>
bool Same(
template <typename T> bool Same(
std::vector<T> Origin,
std::vector<T> Compare
) {
Expand All @@ -72,8 +65,7 @@ namespace Vecs {
}
return true;
}
template <typename T>
std::vector<T> Concatenate(
template <typename T> std::vector<T> Concatenate(
std::vector<T> First,
std::vector<T> Last
) {
Expand All @@ -82,14 +74,4 @@ namespace Vecs {
for(T _Val: Last) _Result.push_back(_Val);
return _Result;
}
template <typename T>
class Extended {
private:
int _size;
public:
std::vector<T> Vector;
int Size() {
return _size;
}
};
}
8 changes: 2 additions & 6 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <utl/vecs.h>
#include <utl/files.h>
#include <string>

int main() {
std::vector<int> v{1,1,1,1,1,1,4,5};
std::vector<int> a{1238,321,321};
for(int val: Vecs::Concatenate(v,a)) {
std::cout << val << "\n";
}
Files::Read_File("a");
}

0 comments on commit c70711b

Please sign in to comment.