Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
plug_reorder_parseMap.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #ifndef __PLUGIN_REORDER_PARSEMAP_C__
00008 #define __PLUGIN_REORDER_PARSEMAP_C__
00009 
00010 #ifndef MAIN_PLUGIN_REORDER
00011 #define MAIN_PLUGIN_REORDER_PARSEMAP 
00012 #define DEBUG_PLUGIN_REORDER_PARSEMAP
00013 #endif
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 #include <stdio.h>
00156 #include <stdlib.h>
00157 #include <ctype.h>
00158 #include <string.h>
00159 
00160 #ifndef MAIN_PLUGIN_REORDER
00161 typedef struct {
00162         char classKRH;
00163         int length;
00164         } ClassInfo;
00165 #endif
00166 
00167 int *REORDER_parseMap(char *mapFile
00168                                         , int *length
00169                                         , ClassInfo **classKRH
00170                                         , int *classCount)
00171 {
00172 FILE *inf = NULL;
00173 char c; int icc ;
00174 char *sptr;
00175 char *classList=NULL;
00176 char cBuf[256] = {0};
00177 int i;
00178 int j;
00179 int k;
00180 int line;
00181 int excluded;
00182 int rawLength;
00183 int *index = NULL;
00184 int *classNum=NULL;
00185 int classStart;
00186 char currentClass;
00187 int currentClassPos;
00188 
00189 
00190 if(NULL == mapFile) {
00191         printf("!! [AFNI/reorder] NULL file name !!\n");
00192         *length = *classCount = 0;
00193         return NULL;
00194         }
00195 
00196 if(0 == mapFile[0]) {
00197         printf("!! [AFNI/reorder] Empty file name !!\n");
00198         *length = *classCount = 0;
00199         return NULL;
00200         }
00201 
00202 if(NULL == (inf = fopen(mapFile, "r"))) {
00203         printf("!! [AFNI/reorder] Trouble opening '%s' for reading !!\n", mapFile);
00204         *length = *classCount = 0;
00205         return NULL;
00206         }
00207 
00208 
00209 for(i = 0, line = 1, excluded = 0;; line++) {
00210         if(i > *length) {
00211                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00212                 printf("[parseMap] Entry count exceeds expected\n");
00213                 #endif
00214                 printf("!! [AFNI/reorder] Number of entries in 'mapFile' exceeds expected of %d !!\n"
00215                         , *length);
00216                 *length = *classCount = 0;
00217                 return NULL;
00218                 }
00219 
00220         
00221     c = icc = fgetc(inf);
00222         if(EOF == icc || feof(inf)) {
00223                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00224                 printf("[parseMap] EOF detected: ");
00225                 #endif
00226                 if(i != *length) {
00227                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00228                         printf("Abnormal\n");
00229                         #endif
00230                         printf("!! [AFNI/reorder] Unexpected EOF at line %d !!\n", line);
00231                         *length = *classCount = 0;
00232                         return NULL;
00233                         }
00234                 else {
00235                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00236                         printf("Normal\n");
00237                         #endif
00238                         break;
00239                         }
00240                 }
00241         ungetc(c, inf);
00242 
00243         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00244         printf("[parseMap] Processing line %d...\n", line);
00245         #endif
00246 
00247         
00248         fgets(cBuf, sizeof(cBuf), inf);
00249         
00250         
00251         sptr = strchr(cBuf, '\n');
00252         if(sptr) {
00253                 *sptr = 0;
00254                 }
00255   
00256         
00257         sptr = cBuf;
00258         while(isspace(*sptr)) ++sptr;
00259 
00260         
00261         if('#' == *sptr || 0 == *sptr) {
00262                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00263                 printf("[parseMap] Skipping comment or empty line.\n");
00264                 #endif
00265                 continue;
00266                 }
00267 
00268         if('-' == *sptr) { 
00269                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00270                 printf("[parseMap] Excluded point detected.\n");
00271                 #endif
00272                 ++i;
00273                 ++excluded;
00274                 continue;
00275                 }
00276 
00277         
00278         if(!isalpha(*sptr)) {
00279                 printf("!! [AFNI/reorder] Bad map entry: '%s' at line %d !!\n"
00280                         , sptr, line);
00281                 *length = *classCount = 0;
00282                 return NULL;
00283                 }
00284 
00285         if(!isdigit(*(sptr+1))) {
00286                 printf("!! [AFNI/reorder] Illformed entry '%s' at line %d !!\n"
00287                         , sptr, line);
00288                 *length = *classCount = 0;
00289                 return NULL;
00290                 }
00291 
00292         ++i;
00293         }
00294 
00295 
00296 rewind(inf);
00297 
00298 
00299 index = (int *)calloc(sizeof(int), i-excluded);
00300 if(NULL == index) {
00301         printf("!! [AFNI/reorder] Allocation error(1) !!\n");
00302         *length = *classCount = 0;
00303         return NULL;
00304         }
00305 
00306 classList = (char *)calloc(sizeof(char), i);
00307 if(NULL == classList) {
00308         printf("!! [AFNI/reorder] Allocation error(2) !!\n");
00309         free(index);
00310         *length = *classCount = 0;
00311         return NULL;
00312         }
00313 
00314 classNum = (int *)calloc(sizeof(int), i);
00315 if(NULL == classNum) {
00316         printf("!! [AFNI/reorder] Allocation error(3) !!\n");
00317         free(index);
00318         free(classList);
00319         *length = *classCount = 0;
00320         return NULL;
00321         }
00322 
00323 
00324 (*classKRH) = (ClassInfo *)calloc(sizeof(ClassInfo), 1);
00325 if(NULL == (*classKRH)) {
00326         printf("!! [AFNI/reorder] Allocation error(4) !!\n");
00327         free(index);
00328         free(classList);
00329         free(classNum);
00330         *length = *classCount = 0;
00331         return NULL;
00332         }
00333 
00334 
00335 rawLength = i;
00336 *length = i - excluded;
00337 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00338 printf("[parseMap] Return length of array is %d elements...\n", *length);
00339 #endif
00340 
00341 
00342 currentClass = 0;
00343 *classCount = 0;
00344 for(i = 0, line = 1; i < rawLength; line++) {
00345         
00346         fgets(cBuf, sizeof(cBuf), inf);
00347 
00348         
00349         sptr = strchr(cBuf, '\n');
00350         if(sptr) {
00351                 *sptr = 0;
00352                 }
00353   
00354         
00355         sptr = cBuf;
00356         while(isspace(*sptr)) ++sptr;
00357 
00358         
00359         if('#' == *sptr || 0 == *sptr) {
00360                 continue;
00361                 }
00362 
00363         if('-' == *sptr) { 
00364                 classList[i] = classNum[i] = 0;
00365                 i++;
00366                 continue;
00367                 }
00368 
00369         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00370         printf("[parseMap] Processing line %d [%s]...\n", line, sptr);
00371         #endif
00372 
00373         classList[i] = cBuf[0];
00374         classNum[i] = atoi(&cBuf[1]);
00375 
00376         
00377         if(0 == currentClass && 0 == *classCount) { 
00378                 currentClass = cBuf[0];
00379                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00380                 printf("[parseMap] First class: %c\n", currentClass);
00381                 #endif
00382                 k = classNum[i];
00383                 if(k != 1) {
00384                         printf("!! [AFNI/reorder] Invalid class numbering at line %d [Should start at 1] {1} !!\n"
00385                                 , line); 
00386                         free(classList);
00387                         free(classNum);
00388                         free(index);
00389                         free((*classKRH));
00390                         (*classKRH) = NULL;
00391                         *length = *classCount = 0;
00392                         return NULL;
00393                         }
00394 
00395                 (*classCount)++;
00396 
00397                 
00398                 (*classKRH) = (ClassInfo *)realloc((void *)(*classKRH), (*classCount)*sizeof(ClassInfo));
00399                 if(NULL == (*classKRH)) {
00400                         printf("!! [AFNI/reorder] Allocation error(4) !!\n");
00401                         free(index);
00402                         free(classList);
00403                         free(classNum);
00404                         *length = *classCount = 0;
00405                         return NULL;
00406                         }
00407                 (*classKRH)[(*classCount)-1].classKRH = currentClass;
00408                 (*classKRH)[(*classCount)-1].length = 1;
00409                 }
00410         else if(currentClass != cBuf[0]) { 
00411                 currentClass = cBuf[0];
00412                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00413                 printf("[parseMap] Next class: %c\n", currentClass);
00414                 #endif
00415                 k = classNum[i];
00416                 if(k != 1) {
00417                         printf("!! [AFNI/reorder] Invalid class numbering at line %d [Should start at 1] {2} !!\n"
00418                                 , line); 
00419                         free(classList);
00420                         free(classNum);
00421                         free(index);
00422                         free((*classKRH));
00423                         (*classKRH) = NULL;
00424                         *length = *classCount = 0;
00425                         return NULL;
00426                         }
00427 
00428                 (*classCount)++;
00429 
00430                 
00431                 (*classKRH) = (ClassInfo *)realloc((void *)(*classKRH), (*classCount)*sizeof(ClassInfo));
00432                 if(NULL == (*classKRH)) {
00433                         printf("!! [AFNI/reorder] Allocation error(4) !!\n");
00434                         free(index);
00435                         free(classList);
00436                         free(classNum);
00437                         *length = *classCount = 0;
00438                         return NULL;
00439                         }
00440                 (*classKRH)[(*classCount)-1].classKRH = currentClass;
00441                 (*classKRH)[(*classCount)-1].length = 1;
00442                 }
00443         else {
00444                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00445                 printf("[parseMap] Next entry, checking class numbering...\n");
00446                 #endif
00447                 ++k;
00448                 if(1 == classNum[i]) { 
00449                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00450                         printf("[parseMap] Contiguous class: %c\n", currentClass);
00451                         #endif
00452         
00453                         k = 1;
00454                         (*classCount)++;
00455         
00456                         
00457                         (*classKRH) = (ClassInfo *)realloc((void *)(*classKRH), (*classCount)*sizeof(ClassInfo));
00458                         if(NULL == (*classKRH)) {
00459                                 printf("!! [AFNI/reorder] Allocation error(4) !!\n");
00460                                 free(index);
00461                                 free(classList);
00462                                 free(classNum);
00463                                 *length = *classCount = 0;
00464                                 return NULL;
00465                                 }
00466                         (*classKRH)[(*classCount)-1].classKRH = currentClass;
00467                         (*classKRH)[(*classCount)-1].length = 0;
00468                         }
00469                 else if(classNum[i] != k) {
00470                         printf("!! [AFNI/reorder] Invalid class numbering at line %d {3} !!\n"
00471                                 , line); 
00472                         free(index);
00473                         free(classList);
00474                         free(classNum);
00475                         free((*classKRH));
00476                         (*classKRH) = NULL;
00477                         *length = *classCount = 0;
00478                         return NULL;
00479                         }
00480                 ++(*classKRH)[(*classCount)-1].length;
00481                 }
00482         ++i;
00483         }
00484 fclose(inf);
00485 
00486 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00487 printf("\n[parseMap] Epoch map has %d distinct classes.\n\n", *classCount);
00488 printf("[parseMap] Parsed the following:\n");
00489 for(i = 0; i < rawLength; i++) {
00490         if(classList[i]) {
00491                 printf("  [%d] Class %c %d\n", i, classList[i], classNum[i]);
00492                 }
00493         else {
00494                 printf("  [%d] Excluded.\n", i);
00495                 }
00496         }
00497 printf("\n[parseMap] Meta-sequence of classes is:\n");
00498 for(i = 0; i < *classCount; i++) {
00499         printf("  [%d] Class %c [Width %d TRs]\n", i, (*classKRH)[i].classKRH, (*classKRH)[i].length);
00500         }
00501 #endif
00502 
00503 
00504 
00505 
00506 
00507 
00508 
00509 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00510 printf("\n[parseMap] Building mapping array...\n");
00511 #endif
00512 for(i = 0, currentClass = 0; i < *length; ) {
00513         
00514         if(!currentClass) {
00515                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00516                 printf("\n  Determining next class to map...\n");
00517                 #endif
00518                 j = 0;
00519                 while(j < rawLength) { 
00520                         if(classList[j]) {
00521                                 break;
00522                                 }
00523                         ++j;
00524                         }
00525                 if(j == rawLength) {
00526                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00527                         printf("  << No more classes to map >>\n");
00528                         #endif
00529                         break; 
00530                         }
00531 
00532                 currentClass = classList[j];    
00533                 classStart = classNum[j];       
00534                 currentClassPos = j;            
00535 
00536                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00537                 printf("  Next valid class is '%c', checking to see if there is a better one...\n"
00538                         , currentClass);
00539                 #endif
00540 
00541                 
00542                 for(j = 1; j < rawLength; j++) {
00543                         if(classList[j]) {
00544                                 if(classList[j] < currentClass) {
00545                                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00546                                         printf("    Class '%c' should be done before '%c'...\n"
00547                                                 , classList[j], currentClass);
00548                                         #endif
00549                                         currentClass = classList[j];            
00550                                         classStart = classNum[j];       
00551                                         currentClassPos = j;            
00552                                         }
00553                                 }
00554                         }
00555 
00556                 #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00557                 printf("  Remapping class '%c' [starting index is %d]...\n"
00558                         , currentClass, currentClassPos);
00559                 #endif
00560                 }
00561 
00562         
00563 
00564 
00565         
00566         
00567            for(k = currentClassPos; k < rawLength; ) {
00568                 if(classList[k]) {
00569                         #ifdef DEBUG_PLUGIN_REORDER_PARSEMAP
00570                         printf("    Index[%d] <-- %d [Old index]\n", i, k);
00571                         #endif
00572                         index[i] = k;  
00573                         classList[k] = 0; 
00574                         ++i;
00575                         ++k;
00576                         }
00577                 else { 
00578                         break;
00579                         }
00580 
00581                 if(classList[k] != currentClass || 1 == classNum[k]) {
00582                         break;
00583                         }
00584                 }
00585         currentClass = 0;
00586         }
00587 
00588 
00589 free(classNum);
00590 
00591 return(index);
00592 }
00593 
00594 #ifdef MAIN_PLUGIN_REORDER_PARSEMAP
00595 main(int argc, char *argv[])
00596 {
00597 int *index = NULL;
00598 ClassInfo *classKRH = NULL;
00599 int classCount = 0;
00600 int length;
00601 int i;
00602 
00603 if(3 != argc) {
00604         printf("usage: parseMap <MapFile> <TargetCount>\n");
00605         exit(1);
00606         }
00607 
00608 length = atoi(argv[2]);
00609 if(length < 1) {
00610         printf("!! [Main] Invalid target count: %d !!\n", length);
00611         exit(1);
00612         }
00613 
00614 if(NULL == (index = REORDER_parseMap(argv[1], &length, &classKRH, &classCount))) {
00615         printf("!! [Main] Trouble parsing epoch map file !!\n");
00616         exit(1);
00617         }
00618 
00619 printf("\n[Main] Indices for epoch remapping:\n");
00620 for(i = 0; i < length; i++) {
00621         printf("  %d\n", index[i]);
00622         }
00623 
00624 printf("\n[Main] Meta-sequence of classes is:\n");
00625 for(i = 0; i < classCount; i++) {
00626         printf("  [%d] Class %c [Width %d TRs]\n", i, classKRH[i].classKRH, classKRH[i].length);
00627         }
00628 
00629 free(index);
00630 free(classKRH);
00631 }
00632 #endif
00633 
00634 #endif
00635