/***************************************************************************************/ /* File: csvFiles.c */ /* Author: Garth Brown */ /* Version: 1.0 */ /* * File access for arrays, save to and read from comma delimited files */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef OK #define OK 0 /* no problems */ #endif #ifndef ERROR #define ERROR -1 #endif /***********************************************************************************/ /* * (aSub has 21 inputs and 21 outputs) */ static char num2name[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U' }; /***********************************************************************************/ long saveCsvASubInit(struct aSubRecord *psub); long saveCsvASubProc(struct aSubRecord *psub); long loadCsvASubInit(struct aSubRecord *psub); long loadCsvASubProc(struct aSubRecord *psub); #if EPICS_VERSION>=3 && EPICS_REVISION>=14 epicsRegisterFunction(saveCsvASubInit) ; epicsRegisterFunction(saveCsvASubProc) ; epicsRegisterFunction(loadCsvASubInit) ; epicsRegisterFunction(loadCsvASubProc) ; #endif /**************************************************************************/ /* aSub Function INAM: loadCsvASubInit */ /**************************************************************************/ long loadCsvASubInit(struct aSubRecord *psub) { /* We don't do anything here because we concern any type, size and value change during fly */ if (psub->tpro) printf("\n!!! loadCsvASubInit %s was called !!!\n", psub->name); /* We don't really need dpvt in this case */ psub->dpvt = NULL; return OK; } /**************************************************************************/ /* aSub Function SNAM: loadCsvASubProc */ /**************************************************************************/ /* In this function, we read a CSV file into the output arrays ... */ /* */ /* A: name of file */ /* FTA: string */ /* NOVx: max size array to load */ /* FTVx: type of output, must match input text */ /* VALx: output to array */ /* */ /**************************************************************************/ long loadCsvASubProc(struct aSubRecord *psub) { void ** pOutputs = (void **) &(psub->vala); /* where is my output vector */ epicsEnum16 * pFtOutputs = &(psub->ftva); /* what is the output type */ epicsUInt32 * pNoOutputs = &(psub->nova); /* number of elements for output */ FILE *f = fopen(psub->a, "rt"); char Line[1024]; unsigned int row = 0, column = 0; char *pch, *context; unsigned int sizeOfElement = 0; long longVal; /* a temporary place to store a value between file and array */ unsigned long ulongVal; if (psub->tpro) printf("\n!!! loadCsvASubProc [%s] was called !!!\n", psub->name); if (f) { if (psub->tpro) printf("Reading %s\n", (char *) psub->a); } else { printf("Error opening %s\n", (char *) psub->a); return ERROR; } while (fgets(Line, sizeof(Line), f)) { if (psub->tpro) printf("Line %u: parsing string %s", row, Line); for (pch = (char *) strtok_r(Line, ",", &context), column = strspn(Line, ","); (pch != NULL) && (column < 21); pch = (char *) strtok_r(NULL, ",", &context), column++) { if (row >= pNoOutputs[column]) { if ((psub->tpro) && (row == pNoOutputs[column])) printf( "Skipping \"%s\" in %s line %u column %u: output %c max array length is %u\n", pch, (char *) psub->a, row, column, num2name[column], pNoOutputs[column]); continue; } else { /* if (psub->tpro) printf("%s->", pch); */ } /* copy to the next array element */ switch (pFtOutputs[column]) { case DBF_CHAR: /* If the character is quoted, read the character not the quotes */ if (sscanf(pch, "'%c'", &((char *) pOutputs[column])[row]) != 1) { if (sscanf(pch, "\"%c\"", &((char *) pOutputs[column])[row]) != 1) { sscanf(pch, "%hd", (short *) &((char *) pOutputs[column])[row]); } } /* if (psub->tpro) printf("(char) %c, ", ((char *) pOutputs[column])[row]); */ break; case DBF_UCHAR: /* If the character is quoted, read the character not the quotes */ if (sscanf(pch, "'%c'", &((unsigned char *) pOutputs[column])[row]) != 1) { if (sscanf(pch, "\"%c\"", &((unsigned char *) pOutputs[column])[row]) != 1) { sscanf( pch, "%hu", (unsigned short *) &((unsigned char *) pOutputs[column])[row]); } } /* if (psub->tpro) printf("(uchar) %c, ", ((unsigned char *) pOutputs[column])[row]); */ break; case DBF_SHORT: sscanf(pch, "%hd", &((short *) pOutputs[column])[row]); /* if (psub->tpro) printf("(short) %hd, ", ((short *) pOutputs[column])[row]); */ break; case DBF_USHORT: case DBF_ENUM: sscanf(pch, "%hu", &((unsigned short *) pOutputs[column])[row]); /* if (psub->tpro) printf("(unsigned short) %hu, ", ((unsigned short *) pOutputs[column])[row]); */ break; case DBF_LONG: sizeOfElement = 4; sscanf(pch, "%ld", &longVal); memcpy((void *) ((char *) pOutputs[column] + row * sizeOfElement), (void *) &longVal, sizeOfElement); /* if (psub->tpro) printf("(long) %ld, ", longVal); */ break; case DBF_ULONG: sizeOfElement = 4; sscanf(pch, "%lu", &ulongVal); memcpy((void *) ((char *) pOutputs[column] + row * sizeOfElement), (void *) &ulongVal, sizeOfElement); /* if (psub->tpro) printf("(unsigned long) %lu, ", ulongVal); */ break; case DBF_FLOAT: sscanf(pch, "%f", &((float *) pOutputs[column])[row]); /* if (psub->tpro) printf("(float) %.8g, ", ((float *) pOutputs[column])[row]); */ break; case DBF_DOUBLE: sscanf(pch, "%lf", &((double *) pOutputs[column])[row]); /* if (psub->tpro) printf("(double) %.18lg, ", ((double *) pOutputs[column])[row]); */ break; default: recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("Illegal type %s for [%s.FTV%c]!\n", pamapdbfType[pFtOutputs[column]].strvalue, psub->name, num2name[column]); continue; } } /* if (psub->tpro) printf("\n"); */ row++; } fclose(f); if (psub->tpro) printf("\nloadCsv was done for %s\n", psub->name); return OK; } /**************************************************************************/ /* aSub Function INAM: saveCsvASubInit */ /**************************************************************************/ long saveCsvASubInit(struct aSubRecord *psub) { /* We don't do anything here because we concern any type, size and value change during fly */ if (psub->tpro) printf("\n!!! saveCsvASubInit %s was called !!!\n", psub->name); /* We don't really need dpvt in this case */ psub->dpvt = NULL; return OK; } /**************************************************************************/ /* aSub Function SNAM: saveCsvASubProc */ /**************************************************************************/ /* In this function, we write a CSV file from the input arrays ... */ /* */ /* A: name of file */ /* NOx: size array to save */ /* FTx: type of input */ /* x: input array to save */ /* */ /**************************************************************************/ long saveCsvASubProc(struct aSubRecord *psub) { unsigned long lastRow = 0, lastCol = 0; void ** pInputs = (void **) &(psub->b); /* where is my Input vector */ epicsEnum16 * pFtInputs = &(psub->ftb); /* what is the Input type */ epicsUInt32 * pNoInputs = &(psub->nob); /* number of elements for Input */ FILE *f = fopen(psub->a, "wt"); unsigned int row = 0, column = 0; unsigned int sizeOfElement = 0; long longVal; /* a temporary place to store a value between file and array */ unsigned long ulongVal; if (psub->tpro) printf("\n!!! saveCsvASubProc [%s] was called !!!\n", psub->name); if (f) { if (psub->tpro) printf("Writing %s\n", (char *) psub->a); } else { printf("Error opening %s\n", (char *) psub->a); recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); return ERROR; } for (column = 0; column < 20; column++) { if ((pNoInputs[column] != 0) && (DBF_CHAR <= pFtInputs[column]) && (pFtInputs[column] <= DBF_ENUM)) { lastCol = column; lastRow = pNoInputs[column] > lastRow ? pNoInputs[column] : lastRow; } } if (psub->tpro) printf("%lu columns, %lu rows\n", lastCol+1, lastRow); for (row = 0; row < lastRow; row++, fprintf(f, "\n")) { for (column = 0; column <= lastCol; column++) { if (row >= pNoInputs[column]) { if ((psub->tpro) && (row == pNoInputs[column])) printf( "Skipping %s lines after %u: Input %c max array length is %u\n", (char *) psub->a, row, num2name[column + 1], pNoInputs[column]); fprintf(f, " "); continue; } /* copy to the next array element */ switch (pFtInputs[column]) { case DBF_CHAR: /* printable characters in quotes, others as ASCII index */ if (isprint(((char *) pInputs[column])[row])) fprintf(f, "\"%c\"", ((char *) pInputs[column])[row]); else fprintf(f, "%hd", (short) ((char *) pInputs[column])[row]); break; case DBF_UCHAR: if (isprint(((unsigned char *) pInputs[column])[row])) fprintf(f, "\"%c\"", ((unsigned char *) pInputs[column])[row]); else fprintf( f, "%hu", (unsigned short) ((unsigned char *) pInputs[column])[row]); break; case DBF_SHORT: fprintf(f, "%hd", ((short *) pInputs[column])[row]); break; case DBF_USHORT: case DBF_ENUM: fprintf(f, "%hu", ((unsigned short *) pInputs[column])[row]); break; case DBF_LONG: sizeOfElement = 4; memcpy((void *) &longVal, (void *) ((char *) pInputs[column] + row * sizeOfElement), sizeOfElement); fprintf(f, "%ld", longVal); break; case DBF_ULONG: sizeOfElement = 4; memcpy((void *) &ulongVal, (void *) ((char *) pInputs[column] + row * sizeOfElement), sizeOfElement); fprintf(f, "%lu", ulongVal); break; case DBF_FLOAT: fprintf(f, "%.8g", ((float *) pInputs[column])[row]); break; case DBF_DOUBLE: fprintf(f, "%.18lg", ((double *) pInputs[column])[row]); break; default: recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("Illegal type %s for [%s.FT%c]!\n", pamapdbfType[pFtInputs[column]].strvalue, psub->name, num2name[column]); return ERROR; } if (column < lastCol) fprintf(f, ","); } } fclose(f); if (psub->tpro) printf("\nsaveCsv was done for %s\n", psub->name); return OK; }