nelements is not passed anymore, its simply a return value. get() will grab the entire property.

This commit is contained in:
Dana Jansens 2003-02-17 21:44:39 +00:00
parent 590e5e2392
commit b9a7d8f9ea
2 changed files with 18 additions and 26 deletions

View file

@ -216,13 +216,11 @@ bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
{ {
assert(win != None); assert(atom != None); assert(type != None); assert(win != None); assert(atom != None); assert(type != None);
assert(size == 8 || size == 16 || size == 32); assert(size == 8 || size == 16 || size == 32);
assert(*nelements > 0);
unsigned char *c_val = 0; // value alloc'd in Xlib, must be XFree()d unsigned char *c_val = 0; // value alloc'd in Xlib, must be XFree()d
Atom ret_type; Atom ret_type;
int ret_size; int ret_size;
unsigned long ret_bytes; unsigned long ret_bytes;
int result; int result;
unsigned long maxread = *nelements;
bool ret = false; bool ret = false;
// try get the first element // try get the first element
@ -232,7 +230,7 @@ bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
ret = (result == Success && ret_type == type && ret_size == size && ret = (result == Success && ret_type == type && ret_size == size &&
*nelements > 0); *nelements > 0);
if (ret) { if (ret) {
if (ret_bytes == 0 || maxread <= *nelements) { if (ret_bytes == 0) {
// we got the whole property's value // we got the whole property's value
*value = new unsigned char[*nelements * size/8 + 1]; *value = new unsigned char[*nelements * size/8 + 1];
memcpy(*value, c_val, *nelements * size/8 + 1); memcpy(*value, c_val, *nelements * size/8 + 1);
@ -241,9 +239,7 @@ bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
XFree(c_val); XFree(c_val);
// the number of longs that need to be retreived to get the property's // the number of longs that need to be retreived to get the property's
// entire value. The last + 1 is the first long that we retrieved above. // entire value. The last + 1 is the first long that we retrieved above.
int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1; long remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
if (remain > size * (signed)maxread / 32) // dont get more than the max
remain = size * (signed)maxread / 32;
result = XGetWindowProperty(**display, win, atom, 0l, result = XGetWindowProperty(**display, win, atom, 0l,
remain, false, type, &ret_type, &ret_size, remain, false, type, &ret_type, &ret_size,
nelements, &ret_bytes, &c_val); nelements, &ret_bytes, &c_val);
@ -253,8 +249,9 @@ bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
read of it, then stop here and try again. If it shrank, then this will read of it, then stop here and try again. If it shrank, then this will
still work. still work.
*/ */
if (! ret) if (! ret) {
return get(win, atom, type, &maxread, value, size); return get(win, atom, type, nelements, value, size);
}
*value = new unsigned char[*nelements * size/8 + 1]; *value = new unsigned char[*nelements * size/8 + 1];
memcpy(*value, c_val, *nelements * size/8 + 1); memcpy(*value, c_val, *nelements * size/8 + 1);
@ -273,20 +270,23 @@ bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
bool Property::get(Window win, Atom atom, Atom type, unsigned long *value) bool Property::get(Window win, Atom atom, Atom type, unsigned long *value)
{ {
unsigned long *temp; unsigned long *temp;
unsigned long num = 1; unsigned long num;
if (! get(win, atom, type, &num, (unsigned char **) &temp, 32)) if (! get(win, atom, type, &num, (unsigned char **) &temp, 32))
return false; return false;
*value = temp[0]; if (num >= 1) {
delete [] temp; *value = temp[0];
return true; delete [] temp;
return true;
}
return false;
} }
bool Property::get(Window win, Atom atom, StringType type, ustring *value) bool Property::get(Window win, Atom atom, StringType type, ustring *value)
{ {
unsigned long n = 1; unsigned long n;
StringVect s; StringVect s;
if (get(win, atom, type, &n, &s)) { if (get(win, atom, type, &n, &s) && n > 0) {
*value = s[0]; *value = s[0];
return true; return true;
} }
@ -296,8 +296,6 @@ bool Property::get(Window win, Atom atom, StringType type, ustring *value)
bool Property::get(Window win, Atom atom, StringType type, bool Property::get(Window win, Atom atom, StringType type,
unsigned long *nelements, StringVect *strings) unsigned long *nelements, StringVect *strings)
{ {
assert(*nelements > 0);
Atom t; Atom t;
bool u; // utf8 encoded? bool u; // utf8 encoded?
switch (type) { switch (type) {
@ -307,7 +305,7 @@ bool Property::get(Window win, Atom atom, StringType type,
} }
unsigned char *value; unsigned char *value;
unsigned long elements = (unsigned) -1; unsigned long elements;;
if (!get(win, atom, t, &elements, &value, 8) || elements < 1) if (!get(win, atom, t, &elements, &value, 8) || elements < 1)
return false; return false;
@ -316,7 +314,7 @@ bool Property::get(Window win, Atom atom, StringType type,
std::string::const_iterator it = s.begin(), end = s.end(); std::string::const_iterator it = s.begin(), end = s.end();
unsigned long num = 0; unsigned long num = 0;
while(num < *nelements) { while(true) {
std::string::const_iterator tmp = it; // current string.begin() std::string::const_iterator tmp = it; // current string.begin()
it = std::find(tmp, end, '\0'); // look for null between tmp and end it = std::find(tmp, end, '\0'); // look for null between tmp and end
strings->push_back(std::string(tmp, it)); // s[tmp:it) strings->push_back(std::string(tmp, it)); // s[tmp:it)

View file

@ -230,10 +230,7 @@ public:
struct returned by Property::atoms. struct returned by Property::atoms.
@param type The Atom value of the property type. This can be found in the @param type The Atom value of the property type. This can be found in the
struct returned by Property::atoms. struct returned by Property::atoms.
@param nelements The maximum number of elements to retrieve from the @param nelements When the function returns, if it returns true, this will
property (assuming it has more than 1 value in it). To
retrieve all possible elements, use "(unsigned) -1".<br>
When the function returns, if it returns true, this will
contain the actual number of elements retrieved.<br> contain the actual number of elements retrieved.<br>
@param value If the function returns true, then this contains an array of @param value If the function returns true, then this contains an array of
retrieved values for the property.<br> retrieved values for the property.<br>
@ -282,10 +279,7 @@ public:
struct returned by Property::atoms. struct returned by Property::atoms.
@param type A member of the Property::StringType enum that specifies the @param type A member of the Property::StringType enum that specifies the
type of the string property to retrieve type of the string property to retrieve
@param nelements The maximum number of strings to retrieve from the @param nelements When the function returns, if it returns true, this will
property (assuming it has more than 1 string in it). To
retrieve all possible strings, use "(unsigned) -1".<br>
When the function returns, if it returns true, this will
contain the actual number of strings retrieved.<br> contain the actual number of strings retrieved.<br>
@param strings If the function returns true, then this contains all of the @param strings If the function returns true, then this contains all of the
strings retrieved from the property's value. strings retrieved from the property's value.