Created
September 13, 2016 10:30
-
-
Save wangmuy/64e5519ddd548827b48257dc6acd18bb to your computer and use it in GitHub Desktop.
android ndk getProperty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/26722040/replacement-for-system-property-get-in-android-l-ndk | |
#include <string> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <errno.h> | |
int property_get(const char* key, char* out, char* def) | |
{ | |
std::string cmd = "getprop "; | |
cmd += key; | |
FILE* file = popen(cmd.c_str(), "r"); | |
if(!file) { | |
return -1; | |
} | |
int n = fread(out, 1024, 1, file); | |
if(ferror(file)) { | |
strcpy(out, def); | |
return -2; | |
} | |
pclose(file); | |
return 0; | |
} | |
bool getFloatProperty(const char* key, float* pRet) | |
{ | |
char str[256] = {0}; | |
char* endptr = NULL; | |
int ret = property_get(key, str, (char*)""); | |
if(ret < 0) | |
return false; | |
errno = 0; | |
float val = strtof(str, &endptr); | |
if((char*)str==endptr || errno) { | |
return false; | |
} | |
*pRet = val; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment