?? avc_e00write.c
字號:
* when the directory exists (buffering issue?), and the * following if() block is sometimes executed even if it * should not, but this should not cause problems since the * arc.dir is opened with "a+b" access. *------------------------------------------------------------*/ if ( VSIStat(psInfo->pszInfoPath, &sStatBuf) == -1) { FILE *fp; char *pszArcDir; char *pszInfoDir; pszArcDir = CPLStrdup(CPLSPrintf("%s%s", psInfo->pszInfoPath, "arc.dir")); /* Remove the trailing "/" from pszInfoPath. Most OSes are * forgiving, and allow mkdir to include the trailing character, * but some UNIXes are not. [GEH 2001/05/17] */ pszInfoDir = CPLStrdup(psInfo->pszInfoPath); pszInfoDir[strlen(pszInfoDir)-1] = '\0'; VSIMkdir(pszInfoDir, 0777); fp = VSIFOpen(pszArcDir, "a+b"); CPLFree(pszArcDir); CPLFree(pszInfoDir); if (fp) { VSIFClose(fp); } else { CPLError(CE_Failure, CPLE_OpenFailed, "Unable to create (or write to) 'info' directory %s", psInfo->pszInfoPath); CPLFree(psInfo->pszCoverPath); CPLFree(psInfo->pszInfoPath); CPLFree(psInfo); return NULL; } } } /*----------------------------------------------------------------- * Init the E00 parser. *----------------------------------------------------------------*/ psInfo->hParseInfo = AVCE00ParseInfoAlloc(); psInfo->eCurFileType = AVCFileUnknown; /*----------------------------------------------------------------- * Init multibyte encoding info *----------------------------------------------------------------*/ psInfo->psDBCSInfo = AVCAllocDBCSInfo(); /*----------------------------------------------------------------- * If an error happened during the open call, cleanup and return NULL. *----------------------------------------------------------------*/ if (CPLGetLastErrorNo() != 0) { AVCE00WriteClose(psInfo); psInfo = NULL; } return psInfo;}/********************************************************************** * AVCE00WriteClose() * * Close a coverage and release all memory used by the AVCE00WritePtr * handle. **********************************************************************/void AVCE00WriteClose(AVCE00WritePtr psInfo){ CPLErrorReset(); if (psInfo == NULL) return; CPLFree(psInfo->pszCoverPath); CPLFree(psInfo->pszCoverName); CPLFree(psInfo->pszInfoPath); if (psInfo->hFile) AVCBinWriteClose(psInfo->hFile); if (psInfo->hParseInfo) AVCE00ParseInfoFree(psInfo->hParseInfo); AVCFreeDBCSInfo(psInfo->psDBCSInfo); CPLFree(psInfo);}/********************************************************************** * _IsStringAlnum() * * Scan a string, and return TRUE if it contains only valid characters, * Return FALSE otherwise. * * We used to accept only isalnum() chars, but since extended chars with * accents seem to be accepted, we will only check for chars that * could confuse the lib. **********************************************************************/static GBool _IsStringAlnum(const char *pszFname){ GBool bOK = TRUE; while(bOK && *pszFname != '\0') { if (strchr(" \t.,/\\", (unsigned char)*pszFname) != NULL) bOK = FALSE; pszFname ++; } return bOK;}/********************************************************************** * _AVCE00WriteRenameTable() * * Rename the table and the system fields in a tabledef that will * be written to a new coverage. **********************************************************************/static void _AVCE00WriteRenameTable(AVCTableDef *psTableDef, const char *pszNewCoverName){ char szOldName[40], szOldExt[40], szNewName[40], *pszTmp; char szSysId[40], szUserId[40]; int i; strcpy(szNewName, pszNewCoverName); for(i=0; szNewName[i] != '\0'; i++) szNewName[i] = toupper(szNewName[i]); /*----------------------------------------------------------------- * Extract components from the current table name. *----------------------------------------------------------------*/ strcpy(szOldName, psTableDef->szTableName); if ( !EQUAL(psTableDef->szExternal, "XX") || (pszTmp = strchr(szOldName, '.')) == NULL ) return; /* We don't deal with that table */ *pszTmp = '\0'; pszTmp++; strcpy(szOldExt, pszTmp); if ( (pszTmp = strchr(szOldExt, ' ')) != NULL ) *pszTmp = '\0'; if (strlen(szOldExt) < 3) return; /* We don't deal with that table */ /*----------------------------------------------------------------- * Look for system attributes with same name as table * If the table name extension is followed by a subclass name * (e.g. "TEST.PATCOUNTY") then this subclass is used to build * the system attributes (COUNTY# and COUNTY-ID) and thus we do * not need to rename them * Otherwise (e.g. COUNTY.PAT) the coverage name is used and then * we need to rename these attribs for the new coverage name. *----------------------------------------------------------------*/ if (strlen(szOldExt) == 3) { sprintf(szSysId, "%s#", szOldName); sprintf(szUserId, "%s-ID", szOldName); for(i=0; i<psTableDef->numFields; i++) { /* Remove trailing spaces */ if ((pszTmp=strchr(psTableDef->pasFieldDef[i].szName,' '))!=NULL) *pszTmp = '\0'; if (EQUAL(psTableDef->pasFieldDef[i].szName, szSysId)) { sprintf(psTableDef->pasFieldDef[i].szName, "%s#", szNewName); } else if (EQUAL(psTableDef->pasFieldDef[i].szName, szUserId)) { sprintf(psTableDef->pasFieldDef[i].szName, "%s-ID", szNewName); } } } /*----------------------------------------------------------------- * Build new table name *----------------------------------------------------------------*/ sprintf(psTableDef->szTableName, "%s.%s", szNewName, szOldExt);}/********************************************************************** * _AVCE00WriteCreateCoverFile() * * Create a coverage file for the specified file type. * * The main part of the work is to find the right filename to use based on * the file type, the coverage precision, etc... the rest of job is * done by AVCBinWriteCreate(). * * Returns 0 on success, or -1 if an error happened. * * AVCWriteCloseCoverFile() will eventually have to be called to release the * resources used by the AVCBinFile structure. **********************************************************************/int _AVCE00WriteCreateCoverFile(AVCE00WritePtr psInfo, AVCFileType eType, const char *pszLine, AVCTableDef *psTableDef){ char *pszPath, szFname[50]=""; int i, nStatus = 0; /* By now, new coverage precision should have been established */ CPLAssert(psInfo->nPrecision != AVC_DEFAULT_PREC); /*----------------------------------------------------------------- * Establish filename based on file type, precision, and possibly the * contents of the header line. *----------------------------------------------------------------*/ pszPath = psInfo->pszCoverPath; switch(eType) { case AVCFileARC: strcpy(szFname, "arc"); break; case AVCFilePAL: strcpy(szFname, "pal"); break; case AVCFileCNT: strcpy(szFname, "cnt"); break; case AVCFileLAB: strcpy(szFname, "lab"); break; case AVCFileTOL: if (psInfo->nPrecision == AVC_SINGLE_PREC) strcpy(szFname, "tol"); else strcpy(szFname, "par"); break; case AVCFilePRJ: strcpy(szFname, "prj"); break; case AVCFileTXT: strcpy(szFname, "txt"); break; case AVCFileTX6: /* For TX6/TX7: the filename is subclass_name.txt */ /* See bug 1261: It seems that empty subclass names are valid * for TX7. In this case we'll default the filename to txt.txt */ if (pszLine[0] == '\0') { strcpy(szFname, "txt.txt"); } else if (strlen(pszLine) > 30 || strchr(pszLine, ' ') != NULL) CPLError(CE_Failure, CPLE_IllegalArg, "Invalid TX6/TX7 subclass name \"%s\"", pszLine); else sprintf(szFname, "%s.txt", pszLine); break; case AVCFileRPL: /* For RPL and RXP: the filename is region_name.pal or region_name.rxp */ if (strlen(pszLine) > 30 || strchr(pszLine, ' ') != NULL) CPLError(CE_Failure, CPLE_IllegalArg, "Invalid RPL region name \"%s\"", pszLine); else sprintf(szFname, "%s.pal", pszLine); break; case AVCFileRXP: if (strlen(pszLine) > 30 || strchr(pszLine, ' ') != NULL) CPLError(CE_Failure, CPLE_IllegalArg, "Invalid RXP name \"%s\"", pszLine); else sprintf(szFname, "%s.rxp", pszLine); break; case AVCFileTABLE: /*------------------------------------------------------------- * For tables, Filename will be based on info in the psTableDef * but we need to rename the table and the system attributes * based on the new coverage name. *------------------------------------------------------------*/ if (psInfo->eCoverType != AVCCoverPC && psInfo->eCoverType != AVCCoverPC2) pszPath = psInfo->pszInfoPath; _AVCE00WriteRenameTable(psTableDef, psInfo->pszCoverName); break; default: CPLError(CE_Failure, CPLE_IllegalArg, "_AVCE00WriteCreateCoverFile(): Unsupported file type!"); nStatus = -1; break; } /*----------------------------------------------------------------- * V7 coverage filenames default to have a .adf extension * but PC coverage filenames (except .dbf tables) have no extensions. *----------------------------------------------------------------*/ if (psInfo->eCoverType == AVCCoverV7 && strchr(szFname, '.') == NULL) strcat(szFname, ".adf"); /*----------------------------------------------------------------- * Make sure filename is all lowercase and attempt to create the file *----------------------------------------------------------------*/ for(i=0; szFname[i] != '\0'; i++) szFname[i] = tolower(szFname[i]); if (nStatus == 0) { psInfo->eCurFileType = eType; if (eType == AVCFileTABLE) psInfo->hFile = AVCBinWriteCreateTable(pszPath, psInfo->pszCoverName, psTableDef, psInfo->eCoverType, psInfo->nPrecision, psInfo->psDBCSInfo); else psInfo->hFile = AVCBinWriteCreate(pszPath, szFname, psInfo->eCoverType, eType, psInfo->nPrecision, psInfo->psDBCSInfo); if (psInfo->hFile == NULL) { nStatus = -1; psInfo->eCurFileType = AVCFileUnknown; } } return nStatus;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -