static DFNode *imageWithFilename(WordGetData *get, const char *filename, double widthPts, DFNode *concrete) { const char *concretePath = get->conv->concretePath; const char *abstractPath = get->conv->abstractPath; char *abstractImagesPath = DFAppendPathComponent(abstractPath,"images"); char *lastComponent = DFPathBaseName(filename); char *srcImagePath = DFAppendPathComponent(concretePath,filename); char *dstImagePath = DFAppendPathComponent(abstractImagesPath,lastComponent); if (DFFileExists(dstImagePath)) DFDeleteFile(dstImagePath,NULL); DFError *error = NULL; DFNode *imageNode = NULL; if (!DFFileExists(abstractImagesPath) && !DFCreateDirectory(abstractImagesPath,1,&error)) { WordConverterWarning(get->conv,"Create %s: %s",abstractImagesPath,DFErrorMessage(&error)); DFErrorRelease(error); imageNode = createAbstractPlaceholder(get,"[Error reading image]",concrete); } else if (!DFCopyFile(srcImagePath,dstImagePath,&error)) { WordConverterWarning(get->conv,"Copy %s to %s: %s",srcImagePath,dstImagePath,DFErrorMessage(&error)); DFErrorRelease(error); imageNode = createAbstractPlaceholder(get,"[Error reading image]",concrete); } else { imageNode = WordConverterCreateAbstract(get,HTML_IMG,concrete); DFFormatAttribute(imageNode,HTML_SRC,"images/%s",lastComponent); double contentWidthPts = WordSectionContentWidthPts(get->conv->mainSection); if (contentWidthPts > 0) { double widthPct = widthPts/contentWidthPts*100.0; CSSProperties *properties = CSSPropertiesNew(); char buf[100]; CSSPut(properties,"width",DFFormatDoublePct(buf,100,widthPct)); char *propertiesText = CSSPropertiesCopyDescription(properties); DFSetAttribute(imageNode,HTML_STYLE,propertiesText); free(propertiesText); CSSPropertiesRelease(properties); } } free(abstractImagesPath); free(lastComponent); free(srcImagePath); free(dstImagePath); return imageNode; }
static int processIncludes(TextPackage *package, const char *input, DFBuffer *output, const char *path, DFError **error) { int ok = 1; const char **lines = DFStringSplit(input,"\n",0); for (int lineno = 0; lines[lineno] && ok; lineno++) { const char *line = lines[lineno]; if (DFStringHasPrefix(line,"#include \"") && DFStringHasSuffix(line,"\"")) { char *inclRelPath = DFSubstring(line,10,strlen(line)-1); char *inclAbsPath = DFAppendPathComponent(path,inclRelPath); char *inclDirName = DFPathDirName(inclAbsPath); char *inclContent = DFStringReadFromFile(inclAbsPath,error); if (inclContent == NULL) { DFErrorFormat(error,"%s: %s",inclRelPath,DFErrorMessage(error)); ok = 0; } else if (!processIncludes(package,inclContent,output,inclDirName,error)) { ok = 0; } free(inclRelPath); free(inclAbsPath); free(inclDirName); free(inclContent); } else { DFBufferFormat(output,"%s\n",line); } } free(lines); return ok; }
int DFUnzip(const char *zipFilename, DFStorage *storage, DFError **error) { unsigned char *buf; DFextZipHandleP zipHandle; int i; zipHandle = DFextZipOpen(zipFilename); if (!zipHandle) return zipError(error,"Cannot open file"); for (i = 0; i < zipHandle->zipFileCount; i++) { if ( (buf = DFextZipReadFile(zipHandle, &zipHandle->zipFileEntries[i])) == NULL) return zipError(error, "Cannot read file in zip"); DFBuffer *content = DFBufferNew(); DFBufferAppendData(content, (void *)buf, zipHandle->zipFileEntries[i].uncompressedSize); free(buf); if (!DFBufferWriteToStorage(content, storage, zipHandle->zipFileEntries[i].fileName, error)) { DFBufferRelease(content); return zipError(error, "%s: %s", zipHandle->zipFileEntries[i].fileName, DFErrorMessage(error)); } DFBufferRelease(content); } DFextZipClose(zipHandle); return 1; }
int textPackageList(const char *filename, DFError **error) { char *filePath = DFPathDirName(filename); char *value = DFStringReadFromFile(filename,error); int result = 0; if (value == NULL) DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); else if (!textPackageListRecursive(value,filePath,error,0)) DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); else result = 1; free(filePath); free(value); return result; }
static int prettyPrintODFFile(const char *filename, DFError **error) { int ok = 0; char *odf = NULL; DFStorage *storage = NULL; storage = DFStorageOpenZip(filename,error); if (storage == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); goto end; } /* odf = ODF_toPlain(storage,NULL); printf("%s",odt); */ printf("ODF file support has not been implemented yet.\n"); ok = 1; end: free(odf); DFStorageRelease(storage); return ok; }
static char *Word_toPlainFromDir(DFStorage *storage, DFHashTable *parts, DFError **error) { char *documentPath = NULL; DFHashTable *rels = DFHashTableNew((DFCopyFunction)xstrdup,(DFFreeFunction)free); DFBuffer *output = DFBufferNew(); char *relsPathRel = NULL; DFDocument *relsDoc = NULL; int ok = 0; documentPath = findDocumentPath(storage,error); if (documentPath == NULL) { DFErrorFormat(error,"findDocumentPath: %s",DFErrorMessage(error)); goto end; } relsPathRel = computeDocumentRelsPath(documentPath); if (DFStorageExists(storage,relsPathRel) && ((relsDoc = DFParseXMLStorage(storage,relsPathRel,error)) == NULL)) { DFErrorFormat(error,"%s: %s",relsPathRel,DFErrorMessage(error)); goto end; } parseDocumentRels(documentPath,relsDoc,rels,error); if (!processParts(parts,documentPath,relsDoc,rels,output,storage,error)) goto end; ok = 1; end: free(relsPathRel); free(documentPath); DFHashTableRelease(rels); DFDocumentRelease(relsDoc); if (!ok) { DFBufferRelease(output); return NULL; } else { char *result = xstrdup(output->data); DFBufferRelease(output); return result; } }
char *Word_toPlain(DFStorage *rawStorage, DFHashTable *parts) { DFError *error = NULL; char *result = Word_toPlainFromDir(rawStorage,parts,&error); if (result == NULL) { result = DFFormatString("%s\n",DFErrorMessage(&error)); DFErrorRelease(error); } return result; }
static DFBuffer *readData(const char *filename, DFError **error) { if ((filename == NULL) || !strcmp(filename,"-")) filename = "/dev/stdin";; DFBuffer *buffer = DFBufferReadFromFile(filename,error); if (buffer == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return NULL; } return buffer; }
int main(int argc, const char * argv[]) { int r = 0; DFError *dferr = NULL; if (!runCommand(argc,argv,&dferr)) { fprintf(stderr,"%s\n",DFErrorMessage(&dferr)); DFErrorRelease(dferr); r = 1; } return r; }
static int writeData(DFBuffer *buf, const char *filename, DFError **error) { if ((filename == NULL) || !strcmp(filename,"-")) { fwrite(buf->data,buf->len,1,stdout); return 1; } else if (!DFBufferWriteToFile(buf,filename,error)) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return 0; } else { return 1; } }
TextPackage *TextPackageNewWithFile(const char *filename, DFError **error) { char *contents = DFStringReadFromFile(filename,error); if (contents == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return NULL; } char *path = DFPathDirName(filename); TextPackage *result = TextPackageNewWithString(contents,path,error); free(path); free(contents); return result; }
int diffFiles(const char *filename1, const char *filename2, DFError **error) { DFDocument *doc1 = DFParseHTMLFile(filename1,0,error); if (doc1 == NULL) { DFErrorFormat(error,"%s: %s",filename1,DFErrorMessage(error)); return 0; } DFDocument *doc2 = DFParseHTMLFile(filename1,0,error); if (doc2 == NULL) { DFErrorFormat(error,"%s: %s",filename2,DFErrorMessage(error)); DFDocumentRelease(doc1); return 0; } DFComputeChanges(doc1->root,doc2->root,HTML_ID); char *changesStr = DFChangesToString(doc1->root); printf("%s",changesStr); free(changesStr); DFDocumentRelease(doc1); DFDocumentRelease(doc2); return 1; }
int normalizeFile(const char *filename, DFError **error) { DFDocument *doc = DFParseHTMLFile(filename,0,error); if (doc == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return 0; } HTML_normalizeDocument(doc); HTML_safeIndent(doc->docNode,0); char *str = DFSerializeXMLString(doc,0,0); printf("%s",str); free(str); DFDocumentRelease(doc); return 1; }
static int saveXMLDocument(DFStorage *storage, const char *filename, DFDocument *doc, NamespaceID defaultNS, DFError **error) { char *parentPath = DFPathDirName(filename); int ok = 0; if (!DFSerializeXMLStorage(doc,defaultNS,0,storage,filename,error)) { DFErrorFormat(error,"serialize %s: %s",filename,DFErrorMessage(error)); goto end; } ok = 1; end: free(parentPath); return ok; }
static void test_create(void) { DFError *error = NULL; DFStorage *htmlStorage = DFStorageNewMemory(DFFileFormatHTML); DFDocument *htmlDoc = TestCaseGetHTML(htmlStorage,&error); DFStorageRelease(htmlStorage); if (htmlDoc == NULL) { DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); DFErrorRelease(error); return; } HTML_normalizeDocument(htmlDoc); char *latex = HTMLToLaTeX(htmlDoc); DFBufferFormat(utgetoutput(),"%s",latex); free(latex); DFDocumentRelease(htmlDoc); }
static int addRelatedDoc(DFHashTable *parts, DFHashTable *documentRels, const char *relName, const char *filename, DFBuffer *output, DFHashTable *includeTypes, DFStorage *storage, DFError **error) { const char *relPath = DFHashTableLookup(documentRels,relName); if (relPath == NULL) return 1;; DFDocument *doc = DFParseXMLStorage(storage,relPath,error); if (doc == NULL) { DFErrorFormat(error,"%s: %s",relPath,DFErrorMessage(error)); return 0; } if (doc->root->first != NULL) { addStrippedSerializedDoc(output,doc,filename); DFHashTableAdd(includeTypes,relName,""); } DFDocumentRelease(doc); return 1; }
static int prettyPrintWordFile(const char *filename, DFError **error) { int ok = 0; char *plain = NULL; DFStorage *storage = NULL; storage = DFStorageOpenZip(filename,error); if (storage == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); goto end; } plain = Word_toPlain(storage,NULL); printf("%s",plain); ok = 1; end: free(plain); DFStorageRelease(storage); return ok; }
int testCSS(const char *filename, DFError **error) { char *input = DFStringReadFromFile(filename,error); if (input == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return 0; } CSSSheet *styleSheet = CSSSheetNew(); CSSSheetUpdateFromCSSText(styleSheet,input); char *text = CSSSheetCopyText(styleSheet); printf("%s",text); free(text); printf("================================================================================\n"); char *cssText = CSSSheetCopyCSSText(styleSheet); printf("%s",cssText); free(cssText); CSSSheetRelease(styleSheet); free(input); return 1; }
int DFZip(const char *zipFilename, DFStorage *storage, DFError **error) { const char **allPaths = NULL; DFBuffer *content = NULL; int ok = 0; DFextZipHandleP zipHandle = NULL; allPaths = DFStorageList(storage,error); if (allPaths == NULL || !(zipHandle = DFextZipCreate(zipFilename))) { DFErrorFormat(error,"Cannot create file"); } else { for (int i = 0; allPaths[i]; i++) { const char *path = allPaths[i]; DFBufferRelease(content); content = DFBufferReadFromStorage(storage,path,error); if (content == NULL) { DFErrorFormat(error,"%s: %s",path,DFErrorMessage(error)); goto end; } if (!zipAddFile(zipHandle, path, content, error)) goto end; } ok = 1; } end: DFBufferRelease(content); free(allPaths); if (zipHandle != NULL) DFextZipClose(zipHandle); return ok; }
int textPackageGet(const char *filename, const char *itemPath, DFError **error) { char *value = DFStringReadFromFile(filename,error); if (value == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return 0; } const char **components = DFStringSplit(itemPath,"/",0); for (size_t i = 0; components[i]; i++) { const char *name = components[i]; char *filePath = DFPathDirName(filename); TextPackage *package = TextPackageNewWithString(value,filePath,error); free(filePath); if (package == NULL) { free(value); free(components); return 0; } free(value); value = xstrdup(DFHashTableLookup(package->items,name)); if (value == NULL) { DFErrorFormat(error,"%s: Item %s not found",filename,itemPath); TextPackageRelease(package); free(value); free(components); return 0; } TextPackageRelease(package); } free(components); printf("%s",value); free(value); return 1; }
static char *findDocumentPath(DFStorage *storage, DFError **error) { int ok = 0; DFDocument *relsDoc = NULL; char *result = NULL; relsDoc = DFParseXMLStorage(storage,"/_rels/.rels",error); if (relsDoc == NULL) { DFErrorFormat(error,"_rels/.rels: %s",DFErrorMessage(error)); goto end; } for (DFNode *child = relsDoc->root->first; child != NULL; child = child->next) { if (child->tag != REL_RELATIONSHIP) continue; const char *type = DFGetAttribute(child,NULL_Type); const char *target = DFGetAttribute(child,NULL_TARGET); if ((type == NULL) || (target == NULL)) continue; if (strcmp(type,"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")) continue; result = xstrdup(target); ok = 1; break; } end: DFDocumentRelease(relsDoc); if (ok) return result; free(result); return NULL; }
int main(int argc, const char **argv) { DFError *error = NULL; if ((argc == 4) && !strcmp(argv[1],"get")) { if (DFGetFile(argv[2],argv[3],&error)) return 0; } else if ((argc == 4) && !strcmp(argv[1],"put")) { if (DFPutFile(argv[2],argv[3],&error)) return 0; } else if ((argc == 4) && !strcmp(argv[1],"create")) { if (DFCreateFile(argv[2],argv[3],&error)) return 0; } else { usage(); return 0; } fprintf(stderr,"Error: %s\n",DFErrorMessage(&error)); DFErrorRelease(error); return 1; }
static int internalPut2(WordPutData *put, DFNode *abstract, DFNode *concrete, int isNew, const char *htmlSrc, const char *htmlPath) { int imageChanged = 0; int sizeChanged = 0; if (!DFFileExists(htmlPath)) { WordConverterWarning(put->conv,"HTML image %s does not exist",htmlSrc); return 0; } PixelSize htmlFileSize = PixelSizeZero; if (!getImageFile(put->conv,htmlSrc,&htmlFileSize)) { WordConverterWarning(put->conv,"Could not get aspect ratio of image: %s",htmlSrc); return 0; } PointsSize htmlSize = pointsSizeFromHTMLImg(put->conv,abstract,htmlFileSize); OPCRelationship *rel = NULL; const char *drawingId = NULL; if (isNew) { imageChanged = 1; sizeChanged = 1; } else { ImageInfo *wordInfo = getImageInfo(concrete); if (wordInfo == NULL) return 0; rel = OPCRelationshipSetLookupById(put->conv->package->documentPart->relationships,wordInfo->rId); if ((wordInfo != NULL) && (wordInfo->widthPts > 0) && (wordInfo->heightPts > 0) && (rel != NULL)) { const char *wordSrc = rel->target; char *wordPath = DFAppendPathComponent(put->conv->concretePath,wordSrc); if (!DFFileExists(wordPath)) { WordConverterWarning(put->conv,"Word image %s does not exist",wordSrc); free(wordPath); ImageInfoFree(wordInfo); return 0; } if (!DFPathContentsEqual(htmlPath,wordPath)) { rel->needsRemoveCheck = 1; imageChanged = 1; } if (fabs(wordInfo->widthPts - htmlSize.widthPts) >= 0.1) sizeChanged = 1; free(wordPath); } ImageInfoFree(wordInfo); if (concrete->tag == WORD_DRAWING) drawingId = DrawingInfoDrawingId(concrete); } if (imageChanged || sizeChanged) { if (imageChanged || (rel == NULL)) { // FIXME: is the rel == NULL needed? DFError *error = NULL; rel = addImageRelationship(put->conv,htmlSrc,&error); if (rel == NULL) { WordConverterWarning(put->conv,"%s",DFErrorMessage(&error)); return 0; } } if (drawingId == NULL) drawingId = WordObjectsAddDrawing(put->conv->objects)->drawingId; populateDrawingElement(put->conv,concrete,htmlSize.widthPts,htmlSize.heightPts, drawingId,rel->rId); } return 1; }