mkdir and rmdir basic implementation

This commit is contained in:
vaclavt 2022-03-21 17:46:32 +01:00
parent 2856043feb
commit 2607a50986
8 changed files with 44 additions and 43 deletions

View File

@ -65,7 +65,7 @@ utils/local_install.sh
### KNOWNN BUGS
- server/client problem in std::string TcpNet::read_from_socket(int sockfd) - reading dows not know lenght to read
- server/client problem in std::string TcpNet::read_from_socket(int sockfd) - reading does not know lenght to read
- (read-url "https://api.nasdaq.com/api/calendar/dividends/") ; hangs in sslclient.cpp line 132
### TODO

View File

@ -92,6 +92,8 @@
|`(ls-dir dir)`|List a dir|List of directory entries|`>>> (ls-dir "/tmp") => ("." ".." "vscode-ipc-cccbe1dd-8c71-4028-a863-df975ad5887b.sock" "vscode-ipc-1163bb52-d088-41dc-80a5-81b6a7c7fa36.sock" "vscode-ipc-630f21df-26b5-43d4-8b2e-5175d53ce317.sock")`|IO|
|`(is-file? filename)`|Returns true if passed filename is a file|`>>> (is-file? "/tmp") => nil`|IO|
|`(is-dir? filename)`|Returns true if passed filename is a directory|`>>> (is-dir? "/tmp") => #t`|IO|
|`(mk-dir dirname)`|Creates directory wirh dirname. Does not create missing parent directories. Returns true if creation successfull|`>>> (mk-dir "/tmp/testdir") => #t`|IO|
|`(rm-dir dirname)`|Removes directory wirh dirname. Does not create missing parent directories. Returns true if removal successfull|`>>> (rm-dir "/tmp/testdir") => #t`|IO|
|`(tcp-server port handler)`|Starts listening on port and when request comes calls passed lambda and writes its returned value back to client. Lambda must return either string or two element list where first element is boolean and second string.When first element is true it closes listening socker.|(`tcp-server 7777 (lambda (str) (list #t (string-upcase str))))`|IO|
|`(tcp-client address port data)`|Opens connection to server on port, writes there data and returns response.|`(print (tcp-client "127.0.0.1" 7777 "abcd"))`|IO|
|`(parse-csv string)`|Parse CSV string|`>>> (parse-csv "A,B\n1,1\n2,2") => ((1 1) (2 2))`|String manipulation|

20
ml.cpp
View File

@ -1362,6 +1362,24 @@ MlValue is_dir(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue(is_path_dir(args[0].as_string()));
}
MlValue mk_dir(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 1)
throw MlError(MlValue("mk-dir", mk_dir), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue((bool)mk_path_dir(args[0].as_string()));
}
MlValue rm_dir(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 1)
throw MlError(MlValue("rm-dir", rm_dir), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue((bool)rm_path_dir(args[0].as_string()));
}
// starts tcp listening server
MlValue tcp_server(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@ -2226,6 +2244,8 @@ std::map<const std::string, Builtin> builtin_funcs
std::make_pair("ls-dir", builtin::ls_dir),
std::make_pair("is-file?", builtin::is_file),
std::make_pair("is-dir?", builtin::is_dir),
std::make_pair("mk-dir", builtin::mk_dir),
std::make_pair("rm-dir", builtin::rm_dir),
std::make_pair("tcp-server", builtin::tcp_server),
std::make_pair("tcp-client", builtin::tcp_client),
std::make_pair("get-env", builtin::get_env),

4
ml.h
View File

@ -7,9 +7,9 @@
#include <exception>
#ifdef NDEBUG
const std::string VERSION = "ml 0.5 (" __DATE__ " " __TIME__ "), Release";
const std::string VERSION = "ml 0.5.1 (" __DATE__ " " __TIME__ "), Release";
#else
const std::string VERSION = "ml 0.5 (" __DATE__ " " __TIME__ "), Debug";
const std::string VERSION = "ml 0.5.1 (" __DATE__ " " __TIME__ "), Debug";
#endif
const std::string STDLIB_LOADER =

View File

@ -1,6 +1,7 @@
#include "ml_io.h"
#include <cstring>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
@ -65,12 +66,22 @@ bool is_path_dir(const std::string &path) {
return (bool) S_ISDIR(buf.st_mode);
}
int mk_path_dir(const std::string &path) {
return ::mkdir(path.c_str(), 0755);
bool mk_path_dir(const std::string &path) {
auto r = ::mkdir(path.c_str(), 0755);
if (r == -1) {
std::string err_msg = std::strerror(errno);
std::cerr << "mkdir failed: " << std::strerror(errno) << std::endl;
}
return r == 0;
}
int rm_path_dir(const std::string &path) {
return ::rmdir(path.c_str());
bool rm_path_dir(const std::string &path) {
auto r = ::rmdir(path.c_str());
if (r == -1) {
std::string err_msg = std::strerror(errno);
std::cerr << "rmdir failed: " << std::strerror(errno) << std::endl;
}
return r == 0;
}
MlValue exec_system_cmd(const std::string &cmd) {

View File

@ -17,8 +17,8 @@ bool is_path_file(const std::string &path);
bool is_path_dir(const std::string &path);
int mk_path_dir(const std::string &path);
bool mk_path_dir(const std::string &path);
int rm_path_dir(const std::string &path);
bool rm_path_dir(const std::string &path);
MlValue exec_system_cmd(const std::string &path);

View File

@ -121,6 +121,8 @@
(ut::define-test "result of (is-dir? \"/tmp/file_whichnotex_ists\")" '(ut::assert-false (is-dir? "/tmp/file_whichnotex_ists")))
(ut::define-test "result of (is-dir? \"/tmp\"" '(ut::assert-true (is-dir? "/tmp")))
(ut::define-test "result of (is-file? \"/tmp\"" '(ut::assert-false (is-file? "/tmp")))
(ut::define-test "result of (mk-dir \"/tmp/testdir\"" '(ut::assert-true (mk-dir "/tmp/testdir")))
(ut::define-test "result of (rm-dir \"/tmp/testdir\"" '(ut::assert-true (rm-dir "/tmp/testdir")))
(ut::define-test "result of (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))" '(ut::assert-equal '(1 2 3 4 5 61 66) (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))))
(ut::define-test "result of (flatten '(1 2 (3 3 (4)) 5 6))" '(ut::assert-equal '(1 2 3 3 4 5 6) (flatten (1 2 (3 3 (4)) 5 6))))

View File

@ -1,34 +0,0 @@
#!/bin/bash
#https://github.com/PythonicNinja/jetbrains-reset-trial-mac-osx/blob/master/runme.sh
for product in CLion; do
echo "Closing $product"
ps aux | grep -i MacOs/$product | cut -d " " -f 5 | xargs kill -9
echo "Resetting trial period for $product"
echo "removing evaluation key..."
rm -rf ~/Library/Preferences/$product*/eval
# Above path not working on latest version. Fixed below
rm -rf ~/Library/Application\ Support/JetBrains/$product*/eval
echo "removing all evlsprt properties in options.xml..."
sed -i '' '/evlsprt/d' ~/Library/Preferences/$product*/options/other.xml
# Above path not working on latest version. Fixed below
sed -i '' '/evlsprt/d' ~/Library/Application\ Support/JetBrains/$product*/options/other.xml
echo
done
echo "removing additional plist files..."
rm -f ~/Library/Preferences/com.apple.java.util.prefs.plist
rm -f ~/Library/Preferences/com.jetbrains.*.plist
rm -f ~/Library/Preferences/jetbrains.*.*.plist
echo "restarting cfprefsd"
killall cfprefsd
echo
echo "That's it, enjoy ;)"