Changeset 168 for Evidence/Evidence.cc
- Timestamp:
- 02/12/10 11:37:22 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Evidence.cc
r167 r168 227 227 228 228 // Translates DIMInfo to string (memory has to be freed by caller) 229 // DIM structures are converted to hexadecimal representation230 // For string conversion, a terminating \0 is enforced.231 229 char *EvidenceServer::ToString(DimInfo *Item) { 232 230 233 231 char *Text; 234 int R=0; 235 232 233 // Structure: print hex representation (3 characters per byte) 236 234 if (strlen(Item->getFormat()) != 1) { 237 235 if ((Text = (char *) malloc(3*Item->getSize()+1)) != NULL) { 238 236 for (int i=0; i<Item->getSize(); i++) sprintf(Text+3*i, "%02x", *((char *) Item->getData() + i)); 239 237 } 240 } 241 else { 242 switch (*(Item->getFormat())) { 243 case 'I': R = asprintf(&Text, "%d", Item->getInt()); break; 244 case 'S': R = asprintf(&Text, "%hd", Item->getShort()); break; 245 case 'F': R = asprintf(&Text, "%.5f", Item->getFloat()); break; 246 case 'D': R = asprintf(&Text, "%.5f", Item->getDouble()); break; 247 case 'X': R = asprintf(&Text, "%lld", Item->getLonglong()); break; 248 case 'C': *(Item->getString() + Item->getSize() - 1) = '\0'; 249 R = asprintf(&Text, "%s", Item->getString()); 250 break; 251 default: return NULL; 238 return Text; 239 } 240 241 // String: terminating \0 is enforced 242 if (toupper(*(Item->getFormat())) == 'C') { 243 *(Item->getString() + Item->getSize() - 1) = '\0'; 244 if (asprintf(&Text, "%s", Item->getString()) == -1) return NULL; 245 return Text; 246 } 247 248 // Number array 249 int Size; 250 switch (toupper(*(Item->getFormat()))) { 251 case 'I': 252 case 'L': Size = sizeof(int); break; 253 case 'S': Size = sizeof(short); break; 254 case 'F': Size = sizeof(float); break; 255 case 'D': Size = sizeof(double); break; 256 case 'X': Size = sizeof(long long); break; 257 default: return NULL; 258 } 259 260 int Max, Mem = Item->getSize()*Size*4+1; 261 char *Pos; 262 263 if ((Text = (char *) malloc(Mem)) == NULL) return NULL; 264 265 *Text = '\0'; 266 for (int i=0; i<Item->getSize()/Size; i++) { 267 Pos = Text+strlen(Text); 268 Max = Mem-strlen(Text); 269 270 switch (toupper(*(Item->getFormat()))) { 271 case 'I': 272 case 'L': snprintf(Pos, Max, "%d ", *((int *) Item->getData() + i)); 273 break; 274 case 'S': snprintf(Pos, Max, "%hd ", *((short *) Item->getData() + i)); 275 break; 276 case 'F': snprintf(Pos, Max, "%.5f ", *((float *) Item->getData() + i)); 277 break; 278 case 'D': snprintf(Pos, Max, "%.5f ", *((double *) Item->getData() + i)); 279 break; 280 case 'X': snprintf(Pos, Max, "%lld ", *((long long *) Item->getData() + i)); 281 break; 252 282 } 253 283 } 254 284 255 return (R == -1) ? NULL :Text;285 return Text; 256 286 } 257 287
Note:
See TracChangeset
for help on using the changeset viewer.