Skip to content

Commit c22fae6

Browse files
committed
Rewrite chsrc_prepend_to_file()
[GitHub #317]
1 parent 6a527db commit c22fae6

File tree

1 file changed

+11
-57
lines changed

1 file changed

+11
-57
lines changed

src/framework/core.c

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* | Mikachu2333 <[email protected]>
1212
* |
1313
* Created On : <2023-08-29>
14-
* Last Modified : <2025-10-28>
14+
* Last Modified : <2025-10-30>
1515
*
1616
* chsrc framework
1717
* ------------------------------------------------------------*/
@@ -1751,7 +1751,7 @@ chsrc_append_to_file (const char *str, const char *filename)
17511751
}
17521752

17531753
/**
1754-
* @note 本函数不会自动在 `str` 末尾添加换行符
1754+
* @note 本函数不会在 `str` 末尾添加换行符,所以你可能需要在 `str` 中手动添加
17551755
*/
17561756
static void
17571757
chsrc_prepend_to_file (const char *str, const char *filename)
@@ -1762,67 +1762,21 @@ chsrc_prepend_to_file (const char *str, const char *filename)
17621762
}
17631763

17641764
char *file = xy_normalize_path (filename);
1765-
char *dir = xy_parent_dir (file);
1766-
chsrc_ensure_dir (dir);
17671765

1768-
char *tmpfile_path = NULL;
1769-
FILE *output = chsrc_make_tmpfile ("prepend", ".txt", false, &tmpfile_path);
1770-
1771-
if (!output)
1772-
{
1773-
char *msg = ENGLISH ? xy_2strcat ("Create temp file before Write prepend failed ", file)
1774-
: xy_2strcat ("尝试在文件开头写入时创建临时文件失败:", file);
1775-
chsrc_error2 (msg);
1776-
exit (Exit_ExternalError);
1777-
}
1766+
char *file_content = xy_file_read (file);
1767+
char *content = xy_2strcat (str, file_content);
17781768

1779-
/* 先写入要插入的内容 */
1780-
fprintf (output, "%s", str);
1769+
FILE *f = fopen (file, "w");
17811770

1782-
/* 如果原文件存在,复制其内容到临时文件 */
1783-
FILE *input = fopen (file, "r");
1784-
if (input)
1771+
if (f)
17851772
{
1786-
fseek (input, 0, SEEK_END);
1787-
long file_size = ftell (input);
1788-
fseek (input, 0, SEEK_SET);
1789-
1790-
if (file_size > 0)
1791-
{
1792-
char *buffer = malloc (file_size);
1793-
if (buffer == NULL)
1794-
{
1795-
fclose (input);
1796-
fclose (output);
1797-
remove (tmpfile_path);
1798-
char *msg = ENGLISH ? "Memory allocation failed" : "内存分配失败";
1799-
chsrc_error2 (msg);
1800-
exit (Exit_ExternalError);
1801-
}
1802-
1803-
size_t bytes = fread (buffer, 1, file_size, input);
1804-
if (bytes > 0)
1805-
{
1806-
fwrite (buffer, 1, bytes, output);
1807-
}
1808-
free (buffer);
1809-
}
1810-
fclose (input);
1773+
fwrite (content, 1, strlen (content), f);
1774+
fclose (f);
18111775
}
1812-
1813-
fclose (output);
1814-
1815-
/* 删除原文件(如果存在) */
1816-
remove (file);
1817-
1818-
/* 将临时文件重命名为目标文件 */
1819-
if (rename (tmpfile_path, file) != 0)
1776+
else
18201777
{
1821-
unlink (tmpfile_path);
1822-
char *msg = ENGLISH ? xy_2strcat ("Write prepend failed to ", file)
1823-
: xy_2strcat ("在文件开头写入失败: ", file);
1824-
chsrc_error2 (msg);
1825-
exit (Exit_ExternalError);
1778+
chsrc_error2 ("文件打开失败");
1779+
exit (Exit_UserCause);
18261780
}
18271781

18281782
log_anyway:

0 commit comments

Comments
 (0)