Rewrite chsrc_append_to_file()

[GitHub #76]
This commit is contained in:
Aoran Zeng 2024-11-22 00:33:25 +08:00
parent 1c239f00b0
commit 1182e79b46
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -8,7 +8,7 @@
* | Happy Game <happygame10124@gmail.com> * | Happy Game <happygame10124@gmail.com>
* | * |
* Created On : <2023-08-29> * Created On : <2023-08-29>
* Last Modified : <2024-11-21> * Last Modified : <2024-11-22>
* *
* chsrc framework * chsrc framework
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -1078,13 +1078,39 @@ chsrc_ensure_dir (const char *dir)
chsrc_note2 (xy_2strjoin (msg, dir)); chsrc_note2 (xy_2strjoin (msg, dir));
} }
/**
* @note 便
*/
static void static void
chsrc_append_to_file (const char *str, const char *file) chsrc_append_to_file (const char *str, const char *filename)
{ {
file = xy_normalize_path (file); char *file = xy_normalize_path (filename);
char *dir = xy_parent_dir (file); char *dir = xy_parent_dir (file);
chsrc_ensure_dir (dir); chsrc_ensure_dir (dir);
FILE *f = fopen (file, "a");
if (NULL==f)
{
char *msg = xy_2strjoin ("Unable to open file to write: ", file);
chsrc_error2 (msg);
exit (Exit_UserCause);
}
char *newstr = xy_2strjoin (str, "\n");
size_t len = strlen (newstr);
size_t ret = fwrite (newstr, len, 1, f);
if (ret != 1)
{
char *msg = xy_2strjoin ("Write failed to ", file);
chsrc_error2 (msg);
exit (Exit_UserCause);
}
fclose (f);
/*
char *cmd = NULL; char *cmd = NULL;
if (xy_on_windows) if (xy_on_windows)
{ {
@ -1095,6 +1121,7 @@ chsrc_append_to_file (const char *str, const char *file)
cmd = xy_strjoin (4, "echo '", str, "' >> ", file); cmd = xy_strjoin (4, "echo '", str, "' >> ", file);
} }
chsrc_run (cmd, RunOpt_No_Last_New_Line|RunOpt_Dont_Notify_On_Success); chsrc_run (cmd, RunOpt_No_Last_New_Line|RunOpt_Dont_Notify_On_Success);
*/
} }
static void static void