Skip to content

Instantly share code, notes, and snippets.

@wangmuy
Created September 13, 2016 10:30
Show Gist options
  • Save wangmuy/64e5519ddd548827b48257dc6acd18bb to your computer and use it in GitHub Desktop.
Save wangmuy/64e5519ddd548827b48257dc6acd18bb to your computer and use it in GitHub Desktop.
android ndk getProperty
// 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