Szukam dwóch funkcji w C a pochodzą one z Php.
file_get_contents() in C:
int file_get_contents(const char *filePath, char **outputBuffer, unsigned long *outLength)
{
FILE *infile = fopen(filePath, "rb");
if(infile == NULL)
return -1;
*outLength = 0;
void *buf = NULL;
int bytesRead;
do
{
buf = realloc(buf, *outLength + 4096);
if(buf != NULL)
{
bytesRead = fread((char *)(buf + *outLength), sizeof(char), 4096, infile); // offset the buffer write location to the current position, then append the data into current memory
*outLength += bytesRead;
}
else
{
return ENOMEM;
}
} while(bytesRead != 0);
*outputBuffer = malloc(*outLength); // allocate output buffer
memcpy(*outputBuffer, buf, *outLength); // write data to output buffer
free(buf); // free temporary buffer, then return
return 0;
}
explode() in C:
char *strdup(const char *src)
{
char *tmp = malloc(strlen(src) + 1);
if(tmp)
strcpy(tmp, src);
return tmp;
}
void explode(const char *src, const char *tokens, char ***list, size_t *len)
{
if(src == NULL || list == NULL || len == NULL)
return;
char *str, *copy, **_list = NULL,** tmp;
*list = NULL;
*len = 0;
copy = strdup(src);
if(copy == NULL)
return;
str = strtok(copy, tokens);
if(str == NULL)
goto free_and_exit;
_list = realloc(NULL, sizeof *_list);
if(_list == NULL)
goto free_and_exit;
_list[*len] = strdup(str);
if(_list[*len] == NULL)
goto free_and_exit;
(*len)++;
while((str = strtok(NULL, tokens)))
{
tmp = realloc(_list, (sizeof *_list) * (*len + 1));
if(tmp == NULL)
goto free_and_exit;
_list = tmp;
_list[*len] = strdup(str);
if(_list[*len] == NULL)
goto free_and_exit;
(*len)++;
}
free_and_exit:
*list = _list;
free(copy);
}
Znalazłem takie funkcje w internecie, możecie powiedzieć mi czy one spełnią swoje zadanie (będą działać jak ich wersje w PHP) ?