Changeset 3407 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Mar 11, 2005, 10:38:56 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r3264 r3407 1 2 1 /** @file psVector.c 3 2 * … … 10 9 * @author Robert DeSonia, MHPCC 11 10 * 12 * @version $Revision: 1.3 4$ $Name: not supported by cvs2svn $13 * @date $Date: 2005-0 2-17 19:26:23$11 * @version $Revision: 1.35 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2005-03-11 20:38:56 $ 14 13 * 15 14 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 18 17 #include <string.h> // for memcpy 19 18 #include <stdlib.h> 19 #include <stdio.h> 20 20 #include <math.h> 21 21 … … 407 407 return outVector; 408 408 } 409 410 char* psVectorToString(psVector* vector, int maxLength) 411 { 412 413 if (maxLength < 5) { 414 return NULL; 415 } 416 417 char* str = psAlloc(sizeof(char)*maxLength+1); 418 419 if (vector == NULL) { 420 snprintf(str,maxLength, "NULL"); 421 return str; 422 } 423 424 int size = vector->n; 425 426 if (size == 0) { 427 snprintf(str,maxLength, "[]"); 428 return str; 429 } 430 431 char* tempStr = psAlloc(sizeof(char)*maxLength+1); 432 *str = '\0'; 433 bool full = false; 434 435 #define APPEND_ELEMENTS_CASE(TYPE, NATIVE_TYPE, FORMAT) \ 436 case PS_TYPE_##TYPE: \ 437 for (lcv=0; lcv < size && ! full; lcv++) { \ 438 snprintf(tempStr, maxLength, "%s" FORMAT, prefix, (NATIVE_TYPE) (vector->data.TYPE[lcv])); \ 439 strncat(str,tempStr,maxLength); \ 440 full = (strlen(str) > maxLength-2); \ 441 prefix = ","; \ 442 } \ 443 break; 444 445 #define APPEND_ELEMENTS_CASE_COMPLEX(TYPE,CREAL,CIMAG) \ 446 case PS_TYPE_##TYPE: \ 447 for (lcv=0; lcv < size && ! full; lcv++) { \ 448 snprintf(tempStr, maxLength, "%s%g%+gi", prefix, \ 449 CREAL(vector->data.TYPE[lcv]), \ 450 CIMAG(vector->data.TYPE[lcv])); \ 451 full = (strlen(str) > maxLength-2); \ 452 prefix = ","; \ 453 } \ 454 break; 455 456 int lcv; 457 char* prefix = "["; 458 switch(vector->type.type) { 459 APPEND_ELEMENTS_CASE(S8,char,"%hd") 460 APPEND_ELEMENTS_CASE(S16,short int,"%hd") 461 APPEND_ELEMENTS_CASE(S32,int,"%d") 462 APPEND_ELEMENTS_CASE(S64,long,"%ld") 463 APPEND_ELEMENTS_CASE(U8,unsigned char,"%hu") 464 APPEND_ELEMENTS_CASE(U16,unsigned short,"%hu") 465 APPEND_ELEMENTS_CASE(U32,unsigned int, "%u") 466 APPEND_ELEMENTS_CASE(U64,unsigned long,"%lu") 467 APPEND_ELEMENTS_CASE(F32,double,"%g") 468 APPEND_ELEMENTS_CASE(F64,double,"%g") 469 APPEND_ELEMENTS_CASE_COMPLEX(C32,crealf,cimagf) 470 APPEND_ELEMENTS_CASE_COMPLEX(C64,creal,cimag) 471 default: 472 snprintf(str,maxLength,"[...]"); 473 break; 474 } 475 476 if (full) { 477 // couldn't all fit in given string length 478 479 // remove elements until there is room for ",...]" 480 while (strlen(str) > maxLength - 5) { 481 char* lastComma = strrchr(str,','); 482 if (lastComma == NULL) { // no comma, must be first number 483 str[1] = '\0'; 484 } else { 485 *lastComma = '\0'; 486 } 487 } 488 strncat(str,",...]",maxLength); 489 } else { 490 strncat(str,"]",maxLength); 491 } 492 493 psFree(tempStr); 494 495 return str; 496 } 497
Note:
See TracChangeset
for help on using the changeset viewer.
