From 3051002fc6c543f7c1bb5403ada3a3d5a31c4023 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 20 Nov 2014 12:13:02 -0600 Subject: [PATCH] Greatly increase database generation speed from compressed PPDs where possible (cherry picked from commit 880fec09182a500b7e19b5b1c73189040cf3421a) --- kdeprint/cups/CMakeLists.txt | 4 +- kdeprint/cups/Makefile.am | 4 +- ...iver_db_cups.c => make_driver_db_cups.cpp} | 145 ++++++++++++++---- kdeprint/driverparse.c | 18 ++- kdeprint/driverparse.h | 4 +- kdeprint/lpr/make_driver_db_lpr.c | 4 +- 6 files changed, 131 insertions(+), 48 deletions(-) rename kdeprint/cups/{make_driver_db_cups.c => make_driver_db_cups.cpp} (64%) diff --git a/kdeprint/cups/CMakeLists.txt b/kdeprint/cups/CMakeLists.txt index c9f336558..20a407362 100644 --- a/kdeprint/cups/CMakeLists.txt +++ b/kdeprint/cups/CMakeLists.txt @@ -72,12 +72,12 @@ tde_add_kpart( ${target} AUTOMOC set( target make_driver_db_cups ) set( ${target}_SRCS - make_driver_db_cups.c + make_driver_db_cups.cpp ) tde_add_executable( ${target} SOURCES ${${target}_SRCS} - LINK driverparse-static kdefakes-shared ${ZLIB_LIBRARIES} ${DL_LIBRARIES} + LINK driverparse-static kdefakes-shared ${ZLIB_LIBRARIES} ${DL_LIBRARIES} ${TQT_LIBRARIES} DESTINATION ${BIN_INSTALL_DIR} ) diff --git a/kdeprint/cups/Makefile.am b/kdeprint/cups/Makefile.am index 4e21a545e..f43803c58 100644 --- a/kdeprint/cups/Makefile.am +++ b/kdeprint/cups/Makefile.am @@ -26,8 +26,8 @@ noinst_HEADERS = cupsinfos.h ipprequest.h kmcupsjobmanager.h kmcupsmanager.h kmc kmwother.h ippreportdlg.h kptagspage.h bin_PROGRAMS = make_driver_db_cups cupsdoprint -make_driver_db_cups_SOURCES = make_driver_db_cups.c -make_driver_db_cups_LDADD = ../libdriverparse.a $(top_builddir)/kdecore/libkdefakes.la $(LIBDL) $(LIBZ) +make_driver_db_cups_SOURCES = make_driver_db_cups.cpp +make_driver_db_cups_LDADD = ../libdriverparse.a $(top_builddir)/kdecore/libkdefakes.la $(LIBDL) $(LIBZ) $(LIB_QT) make_driver_db_cups_LDFLAGS = $(all_libraries) cupsdoprint_SOURCES = cupsdoprint.c diff --git a/kdeprint/cups/make_driver_db_cups.c b/kdeprint/cups/make_driver_db_cups.cpp similarity index 64% rename from kdeprint/cups/make_driver_db_cups.c rename to kdeprint/cups/make_driver_db_cups.cpp index e15f052e2..0f77380a2 100644 --- a/kdeprint/cups/make_driver_db_cups.c +++ b/kdeprint/cups/make_driver_db_cups.cpp @@ -29,7 +29,12 @@ #include #include -#include "driverparse.h" +#include +#include + +extern "C" { + #include "driverparse.h" +} #define PROCESS_PPD_FILE_CONTENTS \ memset(value,0,256); \ @@ -126,7 +131,7 @@ void initPpd(const char *dirname) snprintf(drFile, 255, "ppd:%s", buffer); if (c && strncmp(c,".ppd",4) == 0) { - addFile(drFile, ""); + addFile(drFile, "", ""); } else if (c && strncmp(c, ".gz", 3) == 0) { /* keep also compressed driver files */ @@ -136,7 +141,7 @@ void initPpd(const char *dirname) } if (*c == '.' && strncmp(c, ".ppd",4) == 0) { - addFile(drFile, ""); + addFile(drFile, "", ""); } } } @@ -160,19 +165,19 @@ void initPpd(const char *dirname) ssize_t read; while ((read = getline(&line, &len, file)) != -1) { char * pos1 = strstr(line, "\""); - if (pos1 >= 0) { + if (pos1 != NULL) { char * pos2 = strstr(pos1 + 1, "\""); - if (pos2 >= 0) { + if (pos2 != NULL) { *pos2 = 0; char * pos3 = strstr(pos1 + 1, ":"); - if (pos3 >= 0) { + if (pos3 != NULL) { char *ppduri; int n2 = strlen("compressed-ppd:")+strlen(pos3+1); ppduri = (char*)malloc(n2*sizeof(char)+1); memset(ppduri,0,n2); strcat(ppduri, "compressed-ppd:"); strcat(ppduri, pos3+1); - addFile(ppduri, dirname); + addFile(ppduri, dirname, pos2+1); free(ppduri); ppduri = NULL; } @@ -198,7 +203,7 @@ void initPpd(const char *dirname) } } -int parsePpdFile(const char *filename, const char *origin, FILE *output_file) +int parsePpdFile(const char *filename, const char *origin, const char *metadata, FILE *output_file) { gzFile ppd_file; char line[4096], value[256], langver[64] = {0}, desc[256] = {0}; @@ -223,44 +228,116 @@ int parsePpdFile(const char *filename, const char *origin, FILE *output_file) return 1; } -int parseCompressedPpdFile(const char *ppdfilename, const char *origin, FILE *output_file) +int parseCompressedPpdFile(const char *ppdfilename, const char *origin, const char *metadata, FILE *output_file) { char value[256], langver[64] = {0}, desc[256] = {0}; char *c1, *c2; int count = 0; - char *filename; - int n = strlen(origin)+strlen(" cat ")+strlen(ppdfilename); - filename = (char*)malloc(n*sizeof(char)+1); - memset(filename,0,n); - strcat(filename, origin); - strcat(filename, " cat "); - strcat(filename, ppdfilename); + bool useFallbackExtractionMethod = false; - FILE* file = popen(filename, "r"); - if (file) { - char * line = NULL; - size_t len = 0; - ssize_t read; + if (strlen(metadata) > 0) { + TQString metadataProcessed(metadata); + metadataProcessed = metadataProcessed.stripWhiteSpace(); + TQStringList metadataList = TQStringList::split(" ", metadataProcessed, TRUE); + TQLocale ppdLanguage(metadataList[0]); + TQString languageVersion = TQLocale::languageToString(ppdLanguage.language()); + metadataList = TQStringList::split("\" \"", metadataProcessed, TRUE); + TQString description = metadataList[1]; - fprintf(output_file,"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename); + int pos = metadataProcessed.find("MFG:"); + if (pos < 0) { + pos = metadataProcessed.find("MANUFACTURER:"); + } + if (pos >= 0) { + TQString manufacturer; + TQString model; + TQString modelName; + TQString pnpManufacturer; + TQString pnpModel; + TQString driver; + TQStringList metadataList = TQStringList::split(";", metadataProcessed.mid(pos), TRUE); + for (TQStringList::Iterator it = metadataList.begin(); it != metadataList.end(); ++it) { + TQStringList kvPair = TQStringList::split(":", *it, TRUE); + if ((kvPair[0].upper() == "MFG") || (kvPair[0].upper() == "MANUFACTURER")) { + manufacturer = kvPair[1]; + } + else if ((kvPair[0].upper() == "MDL") ||(kvPair[0].upper() == "MODEL")) { + modelName = kvPair[1]; + } +// else if (kvPair[0].upper() == "PNPMANUFACTURER") { +// pnpManufacturer = kvPair[1]; +// } +// else if (kvPair[0].upper() == "PNPMODEL") { +// pnpModel = kvPair[1]; +// } + else if ((kvPair[0].upper() == "DRV") || (kvPair[0].upper() == "DRIVER")) { + driver = kvPair[1]; + } + } + + TQStringList driverList = TQStringList::split(",", driver, TRUE); + driver = driverList[0]; + if (driver.startsWith("D")) { + driver = driver.mid(1); + } + model = manufacturer + " " + modelName + " " + driver; + description = description + " [" + languageVersion + "]"; - while ((read = getline(&line, &len, file)) != -1) { - PROCESS_PPD_FILE_CONTENTS + fprintf(output_file,"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename); + + fprintf(output_file,"MANUFACTURER=%s\n",manufacturer.ascii()); + fprintf(output_file,"MODELNAME=%s\n",modelName.ascii()); + fprintf(output_file,"MODEL=%s\n",model.ascii()); + if (pnpManufacturer.length() > 0) { + fprintf(output_file,"PNPMANUFACTURER=%s\n",pnpManufacturer.ascii()); + } + if (pnpModel.length() > 0) { + fprintf(output_file,"PNPMODEL=%s\n",pnpModel.ascii()); + } + if (description.length() > 0) { + fprintf(output_file,"DESCRIPTION=%s\n",description.ascii()); + } } - if (line) { - free(line); + else { + useFallbackExtractionMethod = true; } - - pclose(file); - } - else { - fprintf(stderr, "Can't open driver file : %s\n", ppdfilename); - return 0; } - free(filename); - filename = NULL; + if (useFallbackExtractionMethod) { + char *filename; + int n = strlen(origin)+strlen(" cat ")+strlen(ppdfilename); + filename = (char*)malloc(n*sizeof(char)+1); + memset(filename,0,n); + strcat(filename, origin); + strcat(filename, " cat "); + strcat(filename, ppdfilename); + + FILE* file = popen(filename, "r"); + if (file) { + char * line = NULL; + size_t len = 0; + ssize_t read; + + fprintf(output_file,"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename); + + while ((read = getline(&line, &len, file)) != -1) { + PROCESS_PPD_FILE_CONTENTS + } + if (line) { + free(line); + } + + pclose(file); + } + else { + fprintf(stderr, "Can't open driver file : %s\n", ppdfilename); + return 0; + } + + free(filename); + filename = NULL; + } return 1; } diff --git a/kdeprint/driverparse.c b/kdeprint/driverparse.c index c52134325..49f3a5ad5 100644 --- a/kdeprint/driverparse.c +++ b/kdeprint/driverparse.c @@ -30,13 +30,14 @@ char **files = NULL; char **fileorigins = NULL; +char **filemetadata = NULL; int nfiles = 0, maxfiles = 0; int nhandlers = 0, maxhandlers = 0; int nlibs = 0, maxlibs = 0; typedef struct { void (*init)(const char*); - int (*parse)(const char*, const char*, FILE*); + int (*parse)(const char*, const char*, const char*, FILE*); char *name; int namelen; } handler; @@ -60,7 +61,7 @@ void freeHandlers(void) free(handlers); } -void registerHandler(const char *name, void(*initf)(const char*), int(*parsef)(const char*, const char*, FILE*)) +void registerHandler(const char *name, void(*initf)(const char*), int(*parsef)(const char*, const char*, const char*, FILE*)) { handler *h = (handler*)malloc(sizeof(handler)); h->init = initf; @@ -110,6 +111,7 @@ void initFiles(void) maxfiles = 100; files = (char**)malloc(sizeof(char*) * maxfiles); fileorigins = (char**)malloc(sizeof(char*) * maxfiles); + filemetadata = (char**)malloc(sizeof(char*) * maxfiles); } void freeFiles(void) @@ -118,8 +120,10 @@ void freeFiles(void) for (i=0; iname, handlers[hi]->namelen) == 0) { - handlers[hi]->parse(files[i]+handlers[hi]->namelen, fileorigins[i], dbFile); + handlers[hi]->parse(files[i]+handlers[hi]->namelen, fileorigins[i], filemetadata[i], dbFile); break; } fprintf(stdout, "%d\n", i); diff --git a/kdeprint/driverparse.h b/kdeprint/driverparse.h index c23f72c00..0fa632fd7 100644 --- a/kdeprint/driverparse.h +++ b/kdeprint/driverparse.h @@ -23,9 +23,9 @@ #include -void registerHandler(const char *name, void(*initf)(const char*), int(*parsef)(const char*, const char*, FILE*)); +void registerHandler(const char *name, void(*initf)(const char*), int(*parsef)(const char*, const char*, const char*, FILE*)); void initFoomatic(void); int execute(int argc, char *argv[]); -void addFile(const char *filename, const char *origin); +void addFile(const char *filename, const char *origin, const char *metadata); #endif diff --git a/kdeprint/lpr/make_driver_db_lpr.c b/kdeprint/lpr/make_driver_db_lpr.c index 7b6a9c7fd..848eb967f 100644 --- a/kdeprint/lpr/make_driver_db_lpr.c +++ b/kdeprint/lpr/make_driver_db_lpr.c @@ -114,7 +114,7 @@ void initAps(const char *base) gsversion = 1; } snprintf(drFile, 256, "apsfilter:%s/%s", base, d->d_name); - addFile(drFile, ""); + addFile(drFile, "", ""); } closedir(apsdir); } @@ -199,7 +199,7 @@ void initIfhp(const char *base) snprintf(path, 255, "lprngtool:%s/printerdb", base); if (access(path+10, R_OK) == 0) { - addFile(path, ""); + addFile(path, "", ""); } }