/* VIDARR diode map unpacker */ /* Input: text database. First column - APB channel number, first row - APB location */ /* Output: array diode[sfp][channel] with cable tags (YX coordinates) */ #include /* Standard input/output definitions */ #include /* Standard libraries definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #include #define boards 21 /* APBs per side */ #define channels 64 /* channels per APB */ #define dHor 38 /* diode columns per side */ #define dVer 35 /* diode rows per side */ #define wSize 4 /* database entry format */ int main(void) { int ncol = boards+1, nrow = channels+1; /* larger by 1 for index / labels */ char map[ncol][nrow][wSize+1]; /* additional NULL character to terminate string */ for (int i = 0; i < ncol; i++) for (int j = 0; j < nrow; j++) strncpy(map[i][j], "XXXX", wSize+1); char apbname[boards][wSize/2+1]; /* additional NULL character to terminate string */ int apbind[boards]; for (int i = 0; i < boards; i++) {strncpy(apbname[i], "NC", wSize/2+1); apbind[i] = 99;} char diode[boards][channels][wSize+1]; /* additional NULL character to terminate string */ for (int i = 0; i < boards; i++) for (int j = 0; j < channels; j++) strncpy(diode[i][j], "NCNC", wSize+1); int mask[dHor][dVer]; for (int i = 0; i < dHor; i++) for (int j = 0; j < dVer; j++) mask[i][j] = 0; /* Open file for read */ FILE *fp = fopen("mapB.txt", "r"); if (fp == NULL) {printf("no ini file found\n"); return (0);} fseek (fp, 0, SEEK_END); long tSize = ftell(fp); rewind(fp); // printf("file size in bytes %ld\n", tSize); /* allocate buffer for the entire file */ char *text = (char *) malloc(sizeof(char) *tSize); /* memory for the entire file */ char **lines = (char **)malloc(sizeof(void *)*tSize); /* memory for lines */ char **words = (char **)malloc(sizeof(void *)*tSize); /* memory for words */ if (text == NULL || lines == NULL || words == NULL) {perror("Unable to allocate memory for data\n"); exit(1);} /* copy the entire file into buffer */ fread(text, 1, tSize, fp); fclose(fp); /* get rid of these pesky CRs (this will result in empty lines) */ char *index = text; for (int i = 0; i < tSize; i++) if (*(index + i) == '\r') *(index + i) = '\n'; /* int i will be casted to long */ /* split text into lines and get rid of empty ones */ int lindex = 0, nscan = 0; while ((( *(lines + lindex) = strsep(&text, "\n")) != NULL)) { char *ltoken = strsep((lines + lindex), "\n"); if (*ltoken != 0x00 && nscan < nrow) { const char delimiters[] = " ,;:\t"; int windex = 0, nrun = 0; while ((( *(words + windex) = strsep(<oken, delimiters)) != NULL)) { char *wtoken = strsep((words + windex), delimiters); if (*wtoken != 0x00 && nrun < ncol) { // printf("col %02d row %02d %s\n", nrun, nscan, wtoken); strncpy(map[nrun][nscan], wtoken, wSize); /* just replace content without touching NULL */ nrun++; } /* if wtoken */ windex++; } /* while windex */ if (nrun < ncol) ncol = nrun; nscan++; } /* if ltoken */ lindex++; } /* while lindex */ if (nscan < nrow) nrow = nscan; /* free up memory */ free (text); free (lines); free (words); /* wrap ncol / nrow values to avoid segmentation fault */ // printf("columns %d rows %d\n", ncol, nrow); if (ncol > boards+1) ncol = boards+1; if (nrow > channels+1) nrow = channels+1; /* check for repeating entries */ for (int i = 1; i < ncol; i++) for (int j = 1; j < nrow; j++) { char *element = map[i][j]; // printf("%s\n", element); for (int m = i; m < ncol; m++) { int origin = (i == m) ? j : 1; for (int n = origin; n < nrow; n++) { char *comp = map[m][n]; // printf ("%02d %02d element %s %02d %02d comp %s\n", i, j, element, m, n, comp); if (i != m || j != n) if (strcmp(element, "NCNC") != 0) if (strcmp(element, comp) == 0) printf("collision %s %02d %02d %02d %02d\n", element, i, j, m, n); } /* for n */ } /* for m */ } /* for i, j */ /* unpack APB location */ for (int i = 1; i < ncol; i++) { char *element = map[i][0]; char ai[] = "99", ac[] = "NC"; char *an = apbname[i - 1]; strncpy(ai, element, wSize/2); /* just replace content without touching NULL */ strncpy(ac, element + wSize/2, wSize/2); /* just replace content without touching NULL */ strncpy(an, ac, wSize/2+1); /* copy the whole string in case *an is not assigned before */ apbind[i - 1] = atoi(ai); if (apbind[i-1] < 0 || apbind[i-1] > boards-1) printf("APB %s index %02d out of range\n", apbname[i-1], apbind[i-1]); } /* for i */ /* unpack XY coordinates of diodes for checks */ for (int i = 1; i < ncol; i++) /* loop over SFPs (APBs) */ { int apbi = apbind[i-1]; char *apbn = apbname[i-1]; // printf("APB %02d%s\n", apbi, apbn); for (int j = 1; j < nrow; j++) /* loop over channels */ { char *element = map[i][j]; char ax[] = "99", ay[] = "99"; char *point = diode[i-1][j-1]; // int apbi = apbind[i-1]; // char *apbn = apbname[i-1]; char *nibble = map[0][j]; int apbchan = atoi(nibble); if (apbchan < 0 || apbchan > channels-1) printf("APB channel number %02d out of range\n", apbchan); strncpy(ay, element, wSize/2); strncpy(ax, element + wSize/2, wSize/2); int x = (strcmp(element, "NCNC") != 0) ? atoi(ax) : 99; int y = (strcmp(element, "NCNC") != 0) ? atoi(ay) : 99; /* test range for XY coordinates */ if (strcmp(element, "NCNC") != 0 && (x < 0 || x > dHor-1 || y < 0 || y > dVer-1)) printf("map XY values out of range %02d %02d Col %02d Row %02d\n", x, y, i, j); else if (strcmp(element, "NCNC") != 0) mask[x][y] = mask[x][y] + 1; // printf("APB %02d%s physical channel %02d diode XY: %02d %02d\n", apbi, apbn, apbchan, x, y); // printf("physical channel %02d diode XY: %02d %02d\n", apbchan, x, y); /* fill output array */ strncpy(point, element, wSize+1); } /* for j */ } /* for i */ /* diode mask (check missing cables) */ /* printf(" "); for (int i = 0; i < dHor; i++) printf(" %02d", i); printf("\n"); for (int j = 0; j < dVer; j++) { printf("%02d", j); for (int i = 0; i < dHor; i++) printf(" %d", mask[i][j]); printf("\n"); } /* for j,i */ /* for (int j = 0; j < channels; j++) { for (int i = 0; i < boards; i++) printf("%s ", diode[i][j]); printf("\n"); } /* for j,i */ return 1; } /* main */