mirror of
https://github.com/owntone/owntone-server.git
synced 2025-11-22 02:35:32 -05:00
[misc] Consolidate control file reader (e.g. for pairing credentials) into misc.c
- also remove requirement to enter device name in .remote file
This commit is contained in:
51
src/misc.c
51
src/misc.c
@@ -533,6 +533,57 @@ m_realpath(const char *pathname)
|
||||
return ret;
|
||||
}
|
||||
|
||||
char **
|
||||
m_readfile(const char *path, int num_lines)
|
||||
{
|
||||
char buf[256];
|
||||
FILE *fp;
|
||||
char **lines;
|
||||
char *line;
|
||||
int i;
|
||||
|
||||
// Alloc array of char pointers
|
||||
lines = calloc(num_lines, sizeof(char *));
|
||||
if (!lines)
|
||||
return NULL;
|
||||
|
||||
fp = fopen(path, "rb");
|
||||
if (!fp)
|
||||
{
|
||||
DPRINTF(E_LOG, L_MISC, "Could not open file '%s' for reading: %s\n", path, strerror(errno));
|
||||
free(lines);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_lines; i++)
|
||||
{
|
||||
line = fgets(buf, sizeof(buf), fp);
|
||||
if (!line)
|
||||
{
|
||||
DPRINTF(E_LOG, L_MISC, "File '%s' has fewer lines than expected (found %d, expected %d)\n", path, i, num_lines);
|
||||
goto error;
|
||||
}
|
||||
|
||||
lines[i] = trimwhitespace(line);
|
||||
if (strlen(lines[i]) == 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_MISC, "Line %d in '%s' is invalid\n", i+1, path);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return lines;
|
||||
|
||||
error:
|
||||
for (i = 0; i < num_lines; i++)
|
||||
free(lines[i]);
|
||||
|
||||
free(lines);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
unicode_fixup_string(char *str, const char *fromcode)
|
||||
|
||||
Reference in New Issue
Block a user