[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:
ejurgensen
2017-06-14 23:49:18 +02:00
parent 2676b9bbab
commit 283df8aa72
9 changed files with 114 additions and 372 deletions

View File

@@ -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)