/*! * \return Error code. * \ingroup Reconstruct * \brief Given a destination directory and file body and a 3D * Woolz object, the 3D object is and exploded into a series of 2D * section files (in the destination directory) each with a name * formed from the given file body and the source plane index. A * section list is also created in the directory using the given * file body together with the '.bib' file extension. * \param dstDirStr Destination directory. * \param dstBodyStr Destination file body. * \param srcObj Source 3D woolz object. * \param srcFName Source file name, may be NULL. * \param srcFFormat Source file format, may be * WLZEFF_FORMAT_NONE. * \param eMsg Destinagtion pointer for messages. */ RecError RecExplode3DObjToFile(char *dstDirStr, char *dstBodyStr, WlzObject *srcObj, char *srcFName, WlzEffFormat srcFFormat, char **eMsg) { int objIdx, planeIdx, objVecCount, fileStrSpace; char *fileStr = NULL, *fileTemplateStr = NULL; FILE *fP; WlzObject **objVec = NULL; RecSectionList *secList = NULL; RecError errFlag = REC_ERR_NONE; struct stat statBuf; const char *errDirAccessStr = "Failed to access directory", *errDirCreateStr = "Failed to create directory", *errExplodeStr = "Failed to explode 3D object", *errWriteWoolzStr = "Failed to write 2D section file", *errListAppendStr = "Failed to build section list", *errWriteListStr = "Failed to write section list to file"; const char *errStr = NULL; REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_1), ("RecExplode3DObjToFile FE 0x%lx 0x%lx 0x%lx 0x%lx %d 0x%lx\n", (unsigned long )dstDirStr, (unsigned long )dstBodyStr, (unsigned long )srcObj, (unsigned long )srcFName, srcFFormat, (unsigned long )eMsg)); if((dstDirStr == NULL) || (strlen(dstDirStr) == 0) || (dstBodyStr == NULL) || (strlen(dstBodyStr) == 0) || (srcObj == NULL) || (srcObj->type != WLZ_3D_DOMAINOBJ) || (srcObj->domain.core == NULL)) { errFlag = REC_ERR_FUNC; } if(errFlag == REC_ERR_NONE) /* Check for/create destination directory */ { if(stat(dstDirStr, &statBuf) == 0) /* Directory exists */ { #ifdef LINUX2 if((statBuf.st_mode & (__S_IFDIR|__S_IREAD|__S_IWRITE|__S_IEXEC)) == 0) #else /* LINUX2 */ if((statBuf.st_mode & (S_IFDIR | S_IRWXU)) == 0) #endif /* LINUX2 */ { errStr = errDirAccessStr; errFlag = REC_ERR_WRITE; /* Can't read/write/search directory */ } } else { if(mkdir(dstDirStr, S_IRWXU)) { errStr = errDirCreateStr; errFlag = REC_ERR_WRITE; } } REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2), ("RecExplode3DObjToFile 01 %d\n", (int )errFlag)); } if(errFlag == REC_ERR_NONE) /* Explode the 3D Woolz object */ { if((errFlag = RecErrorFromWlz( WlzExplode3D(&objVecCount, &objVec, srcObj))) != REC_ERR_NONE) { errStr = errExplodeStr; } REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2), ("RecExplode3DObjToFile 02 %d\n", (int )errFlag)); } if(errFlag == REC_ERR_NONE) /* Create a section list */ { if(((secList = RecSecNewSectionList(NULL)) == NULL) || ((secList->list = HGUDlpListCreate(NULL)) == NULL)) { errFlag = REC_ERR_MALLOC; } else { secList->reconstruction.fileName = srcFName; secList->reconstruction.fileFormat = srcFFormat; } REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2), ("RecExplode3DObjToFile 03 %d\n", (int )errFlag)); } if(errFlag == REC_ERR_NONE) /* Write 2D files, build/write section list */ { fP = NULL; objIdx = 0; planeIdx = srcObj->domain.p->plane1; fileStrSpace = (int )strlen(dstDirStr) + (int )strlen(dstBodyStr) + 64; if(((fileStr = AlcMalloc(sizeof(char) * fileStrSpace)) == NULL) || ((fileTemplateStr = AlcMalloc(sizeof(char) * fileStrSpace)) == NULL)) { errFlag = REC_ERR_MALLOC; } else { (void )sprintf(fileTemplateStr, "%s/%s", dstDirStr, dstBodyStr); } while((errFlag == REC_ERR_NONE) && (objIdx < objVecCount)) { (void )sprintf(fileStr, "%s_%06d.wlz", fileTemplateStr, planeIdx); if((fP = fopen(fileStr, "w")) == NULL) { errStr = errWriteWoolzStr; errFlag = REC_ERR_WRITE; } if(errFlag == REC_ERR_NONE) { if((errFlag = RecErrorFromWlz( WlzWriteObj(fP, *(objVec + objIdx)))) != REC_ERR_NONE) { errStr = errWriteWoolzStr; } fclose(fP); } if(errFlag == REC_ERR_NONE) { errFlag = RecSecAppendListFromFiles(secList->list, NULL, &fileStr, 1, planeIdx, 1); if(errFlag != REC_ERR_NONE) { errStr = errListAppendStr; } } if(errFlag == REC_ERR_NONE) { ++objIdx; ++planeIdx; } } if(objVec && (objVecCount > 0)) { for(objIdx = 0; objIdx < objVecCount; ++objIdx) { if(*(objVec + objIdx)) { (void )WlzFreeObj(*(objVec + objIdx)); } } free(objVec); } if(errFlag == REC_ERR_NONE) { (void )sprintf(fileStr, "%s.bib", fileTemplateStr); if((fP = fopen(fileStr, "w")) == NULL) { errStr = errWriteListStr; errFlag = REC_ERR_WRITE; } else { errFlag = RecFileSecListWrite(fP, secList, objVecCount, eMsg); fclose(fP); } } if(fileStr) { free(fileStr); } if(fileTemplateStr) { free(fileTemplateStr); } if(secList) { HGUDlpListDestroy(secList->list); AlcFree(secList); } REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2), ("RecExplode3DObjToFile 04 %d\n", (int )errFlag)); } if((errFlag != REC_ERR_NONE) && eMsg && (*eMsg == NULL) && (errStr != NULL)) { *eMsg = AlcStrDup(errStr); } REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_1), ("RecExplode3DObjToFile FX %d\n", (int )errFlag)); return(errFlag); }
int main(int argc, char *argv[]) { int option, numSec, absMode = 1, ok = 1, usage = 0; char *inFile, *outFile; FILE *fP = NULL; RecError recErr = REC_ERR_NONE; RecSectionList *secList = NULL; char *errMsg; static char optList[] = "ahro:", defFile[] = "-", defErrMsg[] = "none."; opterr = 0; errMsg = defErrMsg; inFile = outFile = defFile; while((usage == 0) && ((option = getopt(argc, argv, optList)) != -1)) { switch(option) { case 'a': absMode = 1; break; case 'r': absMode = 0; break; case 'o': outFile = optarg; break; case 'h': /* FALLTHROUGH */ default: usage = 1; break; } } if((usage == 0) && (optind < argc)) { if((optind + 1) != argc) { usage = 1; } else { inFile = *(argv + optind); } } ok = !usage; /* Read input bibfile. */ if(ok) { if((secList = RecSecNewSectionList(&recErr)) == NULL) { ok = 0; (void )fprintf(stderr, "%s: Failed to memory for allocate section list.\n", *argv); } } if(ok) { if((inFile == NULL) || (*inFile == '\0') || ((fP = (strcmp(inFile, "-")? fopen(inFile, "r"): stdin)) == NULL)) { ok = 0; (void )fprintf(stderr, "%s: Failed to open input file %s\n", *argv, (inFile)? inFile: "(null)"); } } if(ok) { if(RecFileSecListRead(secList, &numSec, fP, &errMsg) != REC_ERR_NONE) { ok = 0; (void )fprintf(stderr, "%s: Failed to read section list from file %s.\n" "Error message - %s\n", *argv, inFile, errMsg); } } if(fP && (strcmp(inFile, "-") == 0)) { (void )fclose(fP); } /* Convert transforms. */ if(ok) { if(absMode) { recErr = RecSecListAbsFromRel(secList); } else { recErr = RecSecListRelFromAbs(secList); } if(recErr != REC_ERR_NONE) { ok = 0; (void )fprintf(stderr, "%s: Failed to convert the section list from %s to\n" "%s transforms.\n", *argv, (absMode)? "relative": "absolute", (absMode)? "absolute": "relative"); } } /* Write output bibfile. */ if(ok) { if((outFile == NULL) || (*outFile == '\0') || ((fP = (strcmp(outFile, "-")? fopen(outFile, "w"): stdout)) == NULL)) { ok = 0; (void )fprintf(stderr, "%s: Failed to open output file %s\n", *argv, (inFile)? inFile: "(null)"); } } if(ok) { if(RecFileSecListWrite(fP, secList, numSec, &errMsg) != REC_ERR_NONE) { (void )fprintf(stderr, "%s: Failed to write section list to file %s.\n" "Error message - %s\n", *argv, inFile, errMsg); ok = 0; } } if(fP && (strcmp(outFile, "-") == 0)) { (void )fclose(fP); } if(usage) { (void )fprintf(stderr, "Usage: %s [-a] [-h] [-r] [-o outfile] [<infile>]\n" "Reads a reconstruction bibfile and writes it to the output file\n" "with either absolute or relative transforms.\n" "Version: %s\n" "Options:\n" " -a Output bibfile with absolute transforms (%s).\n" " -h Help - print this usage information.\n" " -r Output bibfile with relative transforms (%s).\n" " -o Output file name.\n" "By default %s reads from it's standard input and\n" "writes to it's standard output.\n", *argv, WlzVersion(), (absMode != 0)? "set": "not set", (absMode == 0)? "set": "not set", *argv); } exit(!ok); }