moved core-misc's replaceWord to global (as why2_replace)
This commit is contained in:
parent
876c24e3fb
commit
74e0453c5b
@ -34,5 +34,6 @@ unsigned long why2_count_unused_key_size(char *text, char *key); //COUNT unused_
|
|||||||
unsigned long why2_count_repeated_key_size(char *text, char *key); //COUNT repeated_key_size
|
unsigned long why2_count_repeated_key_size(char *text, char *key); //COUNT repeated_key_size
|
||||||
unsigned long why2_compare_time_micro(struct timeval startTime, struct timeval finishTime); //COMPARE TIMES IN MICROSECONDS
|
unsigned long why2_compare_time_micro(struct timeval startTime, struct timeval finishTime); //COMPARE TIMES IN MICROSECONDS
|
||||||
void why2_die(char *exit_message); //PRINTS exit_message ERROR AND EXITS WITH CODE 1
|
void why2_die(char *exit_message); //PRINTS exit_message ERROR AND EXITS WITH CODE 1
|
||||||
|
char *why2_replace(char *string, char *old, char *new); //REPLACES old IN string WITH new
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -54,42 +54,6 @@ int removeDirectory(char *path)
|
|||||||
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
|
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *replaceWord(char *string, char *old, char *new) //CODE FROM: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word
|
|
||||||
{
|
|
||||||
char *result;
|
|
||||||
int i, cnt = 0;
|
|
||||||
int newLen = strlen(new);
|
|
||||||
int oldLen = strlen(old);
|
|
||||||
|
|
||||||
for (i = 0; string[i] != '\0'; i++)
|
|
||||||
{
|
|
||||||
if (strstr(&string[i], old) == &string[i])
|
|
||||||
{
|
|
||||||
cnt++;
|
|
||||||
|
|
||||||
i += oldLen - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result = (char*) why2_malloc(i + cnt * (newLen - oldLen) + 1);
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (*string)
|
|
||||||
{
|
|
||||||
// compare the substring with the result
|
|
||||||
if (strstr(string, old) == string)
|
|
||||||
{
|
|
||||||
strcpy(&result[i], new);
|
|
||||||
i += newLen;
|
|
||||||
string += oldLen;
|
|
||||||
}
|
|
||||||
else result[i++] = *string++;
|
|
||||||
}
|
|
||||||
|
|
||||||
result[i] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum WHY2_EXIT_CODES why2_check_version(void) //! CRASHES WHEN CALLED FROM CHAT STUFF
|
enum WHY2_EXIT_CODES why2_check_version(void) //! CRASHES WHEN CALLED FROM CHAT STUFF
|
||||||
{
|
{
|
||||||
if (why2_get_flags().no_check) return WHY2_SUCCESS;
|
if (why2_get_flags().no_check) return WHY2_SUCCESS;
|
||||||
@ -222,7 +186,7 @@ enum WHY2_EXIT_CODES why2_check_version(void) //! CRASHES WHEN CALLED FROM CHAT
|
|||||||
}
|
}
|
||||||
|
|
||||||
//COUNT installCommand LENGTH & ALLOCATE IT
|
//COUNT installCommand LENGTH & ALLOCATE IT
|
||||||
installCommand = replaceWord(WHY2_UPDATE_COMMAND, "{DIR}", WHY2_UPDATE_NAME);
|
installCommand = why2_replace(WHY2_UPDATE_COMMAND, "{DIR}", WHY2_UPDATE_NAME);
|
||||||
|
|
||||||
installCode = system(installCommand); //INSTALL
|
installCode = system(installCommand); //INSTALL
|
||||||
|
|
||||||
@ -478,3 +442,39 @@ void why2_die(char *exit_msg)
|
|||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *why2_replace(char *string, char *old, char *new) //CODE FROM: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
int i, cnt = 0;
|
||||||
|
int newLen = strlen(new);
|
||||||
|
int oldLen = strlen(old);
|
||||||
|
|
||||||
|
for (i = 0; string[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
if (strstr(&string[i], old) == &string[i])
|
||||||
|
{
|
||||||
|
cnt++;
|
||||||
|
|
||||||
|
i += oldLen - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = (char*) why2_malloc(i + cnt * (newLen - oldLen) + 1);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*string)
|
||||||
|
{
|
||||||
|
// compare the substring with the result
|
||||||
|
if (strstr(string, old) == string)
|
||||||
|
{
|
||||||
|
strcpy(&result[i], new);
|
||||||
|
i += newLen;
|
||||||
|
string += oldLen;
|
||||||
|
}
|
||||||
|
else result[i++] = *string++;
|
||||||
|
}
|
||||||
|
|
||||||
|
result[i] = '\0';
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user