Private
Public Access
2
0

mraa.c: Add _contains helper functions

Signed-off-by: Michael Ring <mail@michael-ring.org>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Michael Ring
2015-04-04 18:52:35 +01:00
committed by Brendan Le Foll
parent 68e216ec3f
commit bff7b558eb
2 changed files with 78 additions and 7 deletions

View File

@@ -76,6 +76,25 @@ mraa_boolean_t mraa_file_exist(const char* filename);
*/ */
char* mraa_file_unglob(const char* filename); char* mraa_file_unglob(const char* filename);
/**
* helper function to check if file contains a given text
*
* @param filename to check
* @param content to check in file
* @return mraa_boolean_t boolean result.
*/
mraa_boolean_t mraa_file_contains(const char* filename, const char* content);
/**
* helper function to check if file contains a given text
*
* @param filename to check
* @param content to check in file
* @param content2 to check in same line of file
* @return mraa_boolean_t boolean result.
*/
mraa_boolean_t mraa_file_contains_both(const char* filename, const char* content, const char* content2);
/** /**
* helper function to find out if file that is targeted by a softlink * helper function to find out if file that is targeted by a softlink
* (partially) matches the given name * (partially) matches the given name

View File

@@ -311,6 +311,17 @@ mraa_get_pin_count()
return plat->phy_pin_count; return plat->phy_pin_count;
} }
char*
mraa_get_pin_name(int pin)
{
if (plat == NULL) {
return NULL;
}
if (pin > (plat->phy_pin_count - 1) || pin < 0)
return NULL;
return (char*) plat->pins[pin].name;
}
mraa_boolean_t mraa_boolean_t
mraa_file_exist(const char* filename) mraa_file_exist(const char* filename)
{ {
@@ -322,15 +333,56 @@ mraa_file_exist(const char* filename)
return file_found; return file_found;
} }
char* mraa_boolean_t
mraa_get_pin_name(int pin) mraa_file_contains(const char* filename, const char* content)
{ {
if (plat == NULL) { mraa_boolean_t found = 0;
return NULL; if ((filename == NULL) || (content == NULL)) {
return 0;
} }
if (pin > (plat->phy_pin_count - 1) || pin < 0)
return NULL; char* file = mraa_file_unglob(filename);
return (char*) plat->pins[pin].name; if (file != NULL) {
size_t len = 1024;
char* line = malloc(len);
FILE* fh = fopen(file, "r");
while ((getline(&line, &len, fh) != -1) && (found == 0)) {
if (strstr(line, content)) {
found = 1;
break;
}
}
fclose(fh);
free(file);
free(line);
}
return found;
}
mraa_boolean_t
mraa_file_contains_both(const char* filename, const char* content, const char* content2)
{
mraa_boolean_t found = 0;
if ((filename == NULL) || (content == NULL)) {
return 0;
}
char* file = mraa_file_unglob(filename);
if (file != NULL) {
size_t len = 1024;
char* line = malloc(len);
FILE* fh = fopen(file, "r");
while ((getline(&line, &len, fh) != -1) && (found == 0)) {
if (strstr(line, content) && strstr(line, content2)) {
found = 1;
break;
}
}
fclose(fh);
free(file);
free(line);
}
return found;
} }
char* char*