sophuwu.site > crls
added sorting, list stuff and too many types!
sophuwu sophie@skisiel.com
Sat, 24 Feb 2024 02:32:15 +0100
commit

27c9b90d6994fbee24f1fcf75e7992798fe436dc

parent

7099447056135ced0574ee8f5c7224811667b15b

3 files changed, 128 insertions(+), 119 deletions(-)

jump to
M CMakeLists.txtCMakeLists.txt

@@ -3,4 +3,5 @@ project(rls)

set(CMAKE_CXX_STANDARD 17) -add_executable(rls main.cpp) +add_executable(rls main.cpp + rls.h)
M main.cppmain.cpp

@@ -6,56 +6,120 @@ #include <sys/stat.h>

#include <vector> #include "rainbow.h" -typedef unsigned char byte; +typedef std::filesystem::path PATH; +typedef std::filesystem::directory_entry DIRENT; +typedef std::vector<DIRENT> DIRENTS; -struct PATH { - std::filesystem::path path; - byte verify(); - std::filesystem::path current(byte abs = 0) { - if (paths[i].is_absolute()) return paths[i]; - if (abs) return std::filesystem::absolute(paths[i]); - return std::filesystem::relative(paths[i]); +rainbow r; +void prnt(std::string str); +struct FLAGS; +extern FLAGS flags; + +struct ENTLIST { + DIRENTS list; + void printall() { + std::string printer; + for (int i = 0; i < list.size(); i++) { + printer = "\t"; + printer += list[i].path().filename().string(); + + } } }; +struct DIRLIST { + DIRENTS files; + DIRENTS dirs; + void sorter(DIRENTS& list, DIRENTS& newlist); + ENTLIST all() { + DIRENTS all; + sorter(dirs, all); + sorter(files, all); + return ENTLIST{all}; + }; + int charcmp(char a, char b); + int strcomp(std::string a, std::string b); +}; + +int DIRLIST::charcmp(char a, char b) { + if (a > 96) a-=32; + if (b > 96) b-=32; + if (b == a) return 2; + return a>b; +} + +int DIRLIST::strcomp(std::string a, std::string b) { + int len = a.length(); + if (b.length()<len) len=b.length(); + char c[len]; + c[0] = 0; + strncat(c, a.c_str(),len); + char d[len]; + d[0] = 0; + strncat(d,b.c_str(),len); + int cmp; + for (int i = 0; i < len; i++) { + cmp = charcmp(c[i], d[i]); + if (cmp != 2) return cmp; + } + return b.length()<a.length(); +} + +void DIRLIST::sorter(DIRENTS& list, DIRENTS& newlist) { + std::string comp; + for (;list.size()!=0;) { + int ii=0; + comp = list[0].path().filename().string(); + for (int i = 1; i < list.size(); i++) { + if (strcomp(comp, list[i].path().filename().string())) { + ii = i; + comp = list[i].path().filename().string(); + } + } + newlist.push_back(list[ii]); + list.erase(list.begin()+ii); + } +} + struct PATHS { size_t i = 0;; std::vector<PATH> paths; - void add(std::string path) {paths.push_back(PATH{path});} - PATH operator ()() {return paths[i];} + void add(std::string str) {paths.push_back(std::filesystem::path(str));} + PATH path() const {return paths[i];} + PATH operator ()() {return path();} void next() {i++;} - byte end() {return i >= paths.size();} + bool end() {return i >= paths.size();} + bool isValid() const {return std::filesystem::exists(path()) && std::filesystem::is_directory(path());}; + PATH abs() const {return std::filesystem::absolute(path());} + DIRLIST read(); }; -byte PATHS::verify() { - if (!std::filesystem::exists(path)) { - prnt(path+" not found.\n"); - return 0; +DIRLIST PATHS::read() { + DIRLIST list; + for (const auto& entry : std::filesystem::directory_iterator(path())) { + if (std::filesystem::is_directory(entry)) list.dirs.push_back(entry); + else list.files.push_back(entry); } - if (!std::filesystem::is_directory(path)) { - prnt(path+" is not a directory.\n"); - return 0; - } - return 1; + return list; } struct FLAGS { - byte color = 1; - byte list = 0; - byte all = 0; - byte number = 0; - byte help = 0; + bool color = true; + bool list = false; + bool all = true; + bool number = false; + bool help = false; PATHS path; void parse(int argc, char* argv[]); void toggle(std::string arg); -} flags; +}; void FLAGS::toggle(std::string arg) { - if (arg == "nocolor" || arg == "c") color = 0; - else if (arg == "list" || arg == "l") list = 1; - else if (arg == "all" || arg == "a") all = 1; - else if (arg == "help" || arg == "h") help = 1; - else if (arg == "number" || arg == "n") number = 1; + if (arg == "nocolor" || arg == "c") color = false; + else if (arg == "list" || arg == "l") list = true; + else if (arg == "all" || arg == "a") all = true; + else if (arg == "help" || arg == "h") help = true; + else if (arg == "number" || arg == "n") number = true; else printf("Unknown flag: %s\n", arg.c_str()); }

@@ -72,117 +136,53 @@ else path.add(arg);

} } -rainbow r; - void prnt(std::string str) { - if (flags.color) {r.print2d(str);r.next();} - else printf("%s", str.c_str()); + if (flags.color) {r.print2d(str+"\n");r.next();} + else printf("%s\n", str.c_str()); } void runHelp(std::string name) { - prnt("Usage: "+name+" [OPTIONS] <PATH>\n"); - prnt("List content of PATH in rainbow.\n"); - prnt("Options:\n"); - prnt(" -h, --help\t\tdisplay this help message\n"); - prnt(" -c, --nocolor\t\tdisable color output\n"); - prnt(" -l, --list\tmore info.\n"); - prnt(" -a, --all\tlist all files\n"); - prnt(" -n, --number\tcount files\n"); + prnt("Usage: "+name+" [OPTIONS] <PATH>"); + prnt("List content of PATH in rainbow."); + prnt("Options:"); + prnt(" -h, --help\t\tdisplay this help message"); + prnt(" -c, --nocolor\t\tdisable color output"); + prnt(" -l, --list\tmore info."); + prnt(" -a, --all\tlist all files"); + prnt(" -n, --number\tcount files"); exit(0); } -struct diritem { - std::string name; - std::string size; - std::string str() {return size + name + "\n";} -}; - std::string convsize(unsigned long n) { if (n == 0) return " 0.00 "; double f = n; char c[] = " KMGT"; unsigned long i = 0; - for (;f > 999; i++) f /= 1000; + for (; f > 999; i++) f /= 1000; if (i > 4) return " >1.00 P "; - n = 100*f; + n = 100 * f; std::string s = ""; - for (; n > 0; n /= 10) s = std::to_string(n%10) + s; + for (; n > 0; n /= 10) s = std::to_string(n % 10) + s; for (; s.length() < 5; s = " " + s); return s.substr(0, 3) + "." + s.substr(3, 2) + " " + c[i] + " "; } -int getdir(std::vector<diritem>& files, std::vector<diritem>& folders, std::string path) { - int len = 0, i= 0; - for (const auto& entry : std::filesystem::directory_iterator(path)) { - diritem item; - item.name = entry.path().filename().string(); - len += item.name.length(); - i++; - try { - if (std::filesystem::is_directory(entry.path())) { - item.name += "/"; - item.size = "Folder "; - folders.push_back(item); - continue; - } - if (std::filesystem::exists(entry.path()) && std::filesystem::is_regular_file(entry.path())) - item.size = convsize(std::filesystem::file_size(entry.path())); - else item.size = convsize(0); - } catch (std::exception& e) { - item.size = " 0.00 "; - } - files.push_back(item); - } - len /= i; - return (5+len)*5; -} - -int charcmp(char a, char b) { - if (a > 96) a-=32; - if (b > 96) b-=32; - if (b == a) return 2; - return a>b; -} - -int strcomp(std::string a, std::string b) { - int len = a.length(); - if (b.length()<len) len=b.length(); - char c[len]; - c[0] = 0; - strncat(c, a.c_str(),len); - char d[len]; - d[0] = 0; - strncat(d,b.c_str(),len); - int cmp; - for (int i = 0; i < len; i++) { - cmp = charcmp(c[i], d[i]); - if (cmp != 2) return cmp; - } - return b.length()<a.length(); -} - -void sortdir(std::vector<diritem> list, std::vector<std::string>& newlist){ - std::string comp; - for (;list.size()!=0;) { - int ii=0; - comp = list[0].name; - for (int i = 1; i < list.size(); i++) { - if (strcomp(comp, list[i].name)) { - ii = i; - comp = list[i].name; - } - } - newlist.push_back(list[ii].str()); - list.erase(list.begin()+ii); - } -} +void br(){printf("\n");} int main(int argc, char* argv[]) { r.init(30); flags.parse(argc, argv); if (flags.help) runHelp(argv[0]); for (;!flags.path.end();flags.path.next()) { - + if (!flags.path.isValid()) { + prnt(flags.path.path().string()+" not valid"); + br(); + continue; + } + prnt(flags.path.path().string()); + ENTLIST list = flags.path.read().all(); + list.printall(); + br(); } return 0; /*
A rls.h

@@ -0,0 +1,8 @@

+// +// Created by sophuwu on 24/02/2024. +// + +#ifndef RLS_RLS_H +#define RLS_RLS_H + +#endif //RLS_RLS_H