99 * | BingChunMoLi <bingchunmoli@bingchunmoli.com>
1010 * |
1111 * Created On : <2023-08-28>
12- * Last Modified : <2025-12-31 >
12+ * Last Modified : <2026-03-17 >
1313 *
1414 *
1515 * xy: 襄阳、咸阳
2323#ifndef XY_H
2424#define XY_H
2525
26- #define _XY_Version "v0.2.2.0-2025/10/28 "
26+ #define _XY_Version "v0.2.2.1-2026/03/17 "
2727#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2828#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2929
@@ -261,14 +261,12 @@ xy_str_gsub (const char *str, const char *pat, const char *replace)
261261 size_t replace_len = strlen (replace );
262262 size_t pat_len = strlen (pat );
263263
264- int unit = replace_len - pat_len ;
265- if (unit <= 0 )
266- unit = 0 ;
264+ size_t unit = (replace_len > pat_len ) ? (replace_len - pat_len ) : 0 ;
267265
268266 size_t len = strlen (str );
269267
270268 const char * cur = str ;
271- int count = 0 ;
269+ size_t count = 0 ;
272270
273271 while (cur < str + len )
274272 {
@@ -284,6 +282,7 @@ xy_str_gsub (const char *str, const char *pat, const char *replace)
284282 // puti(count); DEBUG 匹配次数
285283
286284 char * ret = malloc (unit * count + len + 1 );
285+ if (!ret ) return NULL ;
287286 char * retcur = ret ;
288287
289288 cur = str ;
@@ -376,13 +375,14 @@ xy_strcat (unsigned int count, ...)
376375 {
377376 ptrdiff_t diff = cur - ret ;
378377 ret = realloc (ret , al_cur );
378+ if (NULL == ret )
379+ {
380+ _xy_internal_warn ("xy_strcat(): No availble memory to allocate!" );
381+ va_end (args );
382+ return NULL ;
383+ }
379384 cur = ret + diff ;
380385 }
381- if (NULL == ret )
382- {
383- _xy_internal_warn ("xy_strcat(): No availble memory to allocate!" );
384- return NULL ;
385- }
386386 strcpy (cur , str );
387387 // puts(ret);
388388 cur += strlen (str );
@@ -485,10 +485,9 @@ _xy_str_to_terminal_style (int style, const char *str)
485485 }
486486
487487
488- size_t len = 0 ;
489488new_str :
490489 // -2 把中间%s减掉
491- len = strlen (color_fmt_str ) - 2 ;
490+ size_t len = strlen (color_fmt_str ) - 2 ;
492491 char * buf = malloc (strlen (str ) + len + 1 );
493492 sprintf (buf , color_fmt_str , str );
494493 return buf ;
@@ -804,20 +803,28 @@ _xy_log (int level, const char *prompt, const char *content)
804803 }
805804 else if (level & _XY_Log_Success )
806805 {
807- str = xy_strcat (3 , prompt , ": " , xy_str_to_green (content ));
806+ char * colored = xy_str_to_green (content );
807+ str = xy_strcat (3 , prompt , ": " , colored );
808+ free (colored );
808809 }
809810 else if (level & _XY_Log_Info )
810811 {
811- str = xy_strcat (3 , prompt , ": " , xy_str_to_blue (content ));
812+ char * colored = xy_str_to_blue (content );
813+ str = xy_strcat (3 , prompt , ": " , colored );
814+ free (colored );
812815 }
813816 else if (level & _XY_Log_Warn )
814817 {
815- str = xy_strcat (3 , prompt , ": " , xy_str_to_yellow (content ));
818+ char * colored = xy_str_to_yellow (content );
819+ str = xy_strcat (3 , prompt , ": " , colored );
820+ free (colored );
816821 to_stderr = true;
817822 }
818823 else if (level & _XY_Log_Error )
819824 {
820- str = xy_strcat (3 , prompt , ": " , xy_str_to_red (content ));
825+ char * colored = xy_str_to_red (content );
826+ str = xy_strcat (3 , prompt , ": " , colored );
827+ free (colored );
821828 to_stderr = true;
822829 }
823830 else
@@ -881,29 +888,45 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c
881888 else if (level & _XY_Log_Success )
882889 {
883890 /* [app 成功] [app success] */
884- str = xy_strcat (6 ,
885- "[" , xy_str_to_green (prompt1 ), " " , xy_str_to_bold (xy_str_to_green (prompt2 )), "] " , xy_str_to_green (content ));
891+ char * p1 = xy_str_to_green (prompt1 );
892+ char * p2_inner = xy_str_to_green (prompt2 );
893+ char * p2 = xy_str_to_bold (p2_inner );
894+ char * c = xy_str_to_green (content );
895+ str = xy_strcat (6 , "[" , p1 , " " , p2 , "] " , c );
896+ free (p1 ); free (p2_inner ); free (p2 ); free (c );
886897 }
887898 else if (level & _XY_Log_Info )
888899 {
889900 /* [app 信息] [app info]
890901 [app 提示] [app notice]
891902 */
892- str = xy_strcat (6 ,
893- "[" , xy_str_to_blue (prompt1 ), " " , xy_str_to_bold (xy_str_to_blue (prompt2 )), "] " , xy_str_to_blue (content ));
903+ char * p1 = xy_str_to_blue (prompt1 );
904+ char * p2_inner = xy_str_to_blue (prompt2 );
905+ char * p2 = xy_str_to_bold (p2_inner );
906+ char * c = xy_str_to_blue (content );
907+ str = xy_strcat (6 , "[" , p1 , " " , p2 , "] " , c );
908+ free (p1 ); free (p2_inner ); free (p2 ); free (c );
894909 }
895910 else if (level & _XY_Log_Warn )
896911 {
897912 /* [app 警告] [app warn] */
898- str = xy_strcat (6 ,
899- "[" , xy_str_to_yellow (prompt1 ), " " , xy_str_to_bold (xy_str_to_yellow (prompt2 )), "] " , xy_str_to_yellow (content ));
913+ char * p1 = xy_str_to_yellow (prompt1 );
914+ char * p2_inner = xy_str_to_yellow (prompt2 );
915+ char * p2 = xy_str_to_bold (p2_inner );
916+ char * c = xy_str_to_yellow (content );
917+ str = xy_strcat (6 , "[" , p1 , " " , p2 , "] " , c );
918+ free (p1 ); free (p2_inner ); free (p2 ); free (c );
900919 to_stderr = true;
901920 }
902921 else if (level & _XY_Log_Error )
903922 {
904923 /* [app 错误] [app error] */
905- str = xy_strcat (6 ,
906- "[" , xy_str_to_red (prompt1 ), " " , xy_str_to_bold (xy_str_to_red (prompt2 )), "] " , xy_str_to_red (content ));
924+ char * p1 = xy_str_to_red (prompt1 );
925+ char * p2_inner = xy_str_to_red (prompt2 );
926+ char * p2 = xy_str_to_bold (p2_inner );
927+ char * c = xy_str_to_red (content );
928+ str = xy_strcat (6 , "[" , p1 , " " , p2 , "] " , c );
929+ free (p1 ); free (p2_inner ); free (p2 ); free (c );
907930 to_stderr = true;
908931 }
909932 else
@@ -969,6 +992,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
969992 if (stream == NULL )
970993 {
971994 _xy_internal_warn ("xy_run_iter_lines(): popen() failed" );
995+ free (buf );
972996 return NULL ;
973997 }
974998
@@ -980,6 +1004,8 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
9801004 if (NULL == fgets (buf , size , stream ))
9811005 break ;
9821006 /* 存在换行的总是会把换行符读出来,删掉 */
1007+ if (ret )
1008+ free (ret );
9831009 ret = xy_str_delete_suffix (buf , "\n" );
9841010 count += 1 ;
9851011 if (n == count )
@@ -991,6 +1017,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
9911017 }
9921018 }
9931019
1020+ free (buf );
9941021 pclose (stream );
9951022 return ret ;
9961023}
@@ -1018,6 +1045,7 @@ xy_run_get_status (char *cmd)
10181045 char * command = xy_quiet_cmd (cmd );
10191046
10201047 int status = system (command );
1048+ free (command );
10211049 return status ;
10221050}
10231051
@@ -1041,6 +1069,7 @@ xy_run_get_stdout (const char *cmd, char **output)
10411069 if (stream == NULL )
10421070 {
10431071 _xy_internal_warn ("xy_run_get_stdout(): popen() failed" );
1072+ free (buf );
10441073 return -1 ;
10451074 }
10461075
@@ -1052,6 +1081,14 @@ xy_run_get_stdout (const char *cmd, char **output)
10521081 {
10531082 cap *= 2 ;
10541083 char * new_buf = realloc (buf , cap );
1084+ if (NULL == new_buf )
1085+ {
1086+ _xy_internal_warn ("xy_run_get_stdout(): No available memory to allocate!" );
1087+ free (buf );
1088+ pclose (stream );
1089+ if (output ) * output = NULL ;
1090+ return -1 ;
1091+ }
10551092 buf = new_buf ;
10561093 }
10571094 }
@@ -1061,6 +1098,8 @@ xy_run_get_stdout (const char *cmd, char **output)
10611098
10621099 if (output )
10631100 * output = buf ;
1101+ else
1102+ free (buf );
10641103
10651104 return status ;
10661105}
@@ -1299,7 +1338,8 @@ xy_parent_dir (const char *path)
12991338 if (!last )
13001339 {
13011340 /* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */
1302- return "." ;
1341+ free (dir );
1342+ return xy_strdup ("." );
13031343 }
13041344 * last = '\0' ;
13051345
0 commit comments