Skip to content

Commit 221f4da

Browse files
author
Mikachu2333
committed
merge chsrc_log_overwrite with chsrc_log_write
read file using malloc
1 parent cdebf2f commit 221f4da

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/framework/core.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,9 @@ chsrc_init_framework ()
240240

241241

242242
void
243-
chsrc_log_write (const char *filename)
243+
chsrc_log_write (const char *filename, bool is_overwirte)
244244
{
245-
char *msg = ENGLISH ? "WRITE" : "写入";
246-
247-
xy_log_brkt (blue(App_Name), bdblue(msg), blue(filename));
248-
}
249-
250-
void
251-
chsrc_log_overwrite (const char *filename)
252-
{
253-
char *msg = ENGLISH ? "OVERWRITE" : "覆写";
245+
char *msg = is_overwrite ? (ENGLISH ? "OVERWRITE" : "覆写") : (ENGLISH ? "WRITE" : "写入");
254246

255247
xy_log_brkt (blue(App_Name), bdblue(msg), blue(filename));
256248
}
@@ -1682,7 +1674,7 @@ chsrc_append_to_file (const char *str, const char *filename)
16821674

16831675
log_anyway:
16841676
/* 输出recipe指定的文件名 */
1685-
chsrc_log_write (filename);
1677+
chsrc_log_write (filename, false);
16861678

16871679
/*
16881680
char *cmd = NULL;
@@ -1739,12 +1731,21 @@ chsrc_prepend_to_file (const char *str, const char *filename)
17391731
// 如果原文件存在,复制其内容
17401732
if (input)
17411733
{
1742-
char buffer[8192];
1743-
size_t bytes;
1744-
while ((bytes = fread (buffer, 1, sizeof(buffer), input)) > 0)
1734+
fseek (input, 0, SEEK_END);
1735+
long file_size = ftell (input);
1736+
fseek (input, 0, SEEK_SET);
1737+
1738+
char *buffer = malloc(file_size);
1739+
if (buffer == NULL)
17451740
{
1746-
fwrite (buffer, 1, bytes, output);
1741+
fclose (input);
1742+
return;
17471743
}
1744+
1745+
size_t bytes = fread(buffer, 1, file_size, input);
1746+
if (bytes > 0) fwrite(buffer, 1, bytes, output);
1747+
1748+
free (buffer);
17481749
fclose (input);
17491750
}
17501751

@@ -1765,7 +1766,7 @@ chsrc_prepend_to_file (const char *str, const char *filename)
17651766

17661767
log_anyway:
17671768
/* 输出recipe指定的文件名 */
1768-
chsrc_log_write (filename);
1769+
chsrc_log_write (filename, false);
17691770
}
17701771

17711772
static void
@@ -1803,7 +1804,7 @@ chsrc_overwrite_file (const char *str, const char *filename)
18031804

18041805
log_anyway:
18051806
/* 输出recipe指定的文件名 */
1806-
chsrc_log_overwrite (filename);
1807+
chsrc_log_write (filename, true);
18071808
}
18081809

18091810
static void

0 commit comments

Comments
 (0)