From 7b606ae5c126411f495bacf4a0fccd4705a91175 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Wed, 21 Nov 2001 11:14:09 +0000 Subject: [PATCH] 2001-11-21 Robert Collins * Makefile.in (OBJS): Add category.o and category_list.o. * category.h: New file. * category.cc: New file. * category_list.h: New file. * category_list.cc: New file. * choose.cc: Include category headers. (fill_missing_category): Use package_db to access global categories. (_view::insert_pkg): Ditto. (_view::insert_category): Ditto. (set_view_mode): Ditto. (getcategorybyname): Remove. * cygpackage.h: Include category_list.h to parse this correctly. (cygpackage): New virtual &Categories. * ini.h (_CategoryPackage): More C to C++ conversion. (Category): Remove. (getcategorybyname): Remove. (register_category): Remove. * iniparse.y: Include package db and category headers. (categories): Use package_db to access global categories. (category): Remove. (ncategories): Remove. (register_category): Remove. * package.h: New virtual &Categories. * package_db.cc (packagedb::packagedb): Don't read the installed db twice. (packagedb::categories): Declare. * package_db.h (packagedb): New member categories for global categories list. --- ChangeLog | 29 ++++++++++++++++++ Makefile.in | 2 ++ category.cc | 34 ++++++++++++++++++++++ category.h | 35 ++++++++++++++++++++++ category_list.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ category_list.h | 44 ++++++++++++++++++++++++++++ choose.cc | 39 +++++++++++++------------ cygpackage.cc | 72 +++++++++++++++++++++++++++------------------ cygpackage.h | 36 ++++++++++++++++------- ini.h | 18 ++---------- iniparse.y | 55 ++++++----------------------------- package.h | 3 ++ package_db.cc | 11 +++++-- package_db.h | 4 +++ 14 files changed, 337 insertions(+), 121 deletions(-) create mode 100644 category.cc create mode 100644 category.h create mode 100755 category_list.cc create mode 100755 category_list.h diff --git a/ChangeLog b/ChangeLog index add44743..c15f587e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,32 @@ +2001-11-21 Robert Collins + + * Makefile.in (OBJS): Add category.o and category_list.o. + * category.h: New file. + * category.cc: New file. + * category_list.h: New file. + * category_list.cc: New file. + * choose.cc: Include category headers. + (fill_missing_category): Use package_db to access global categories. + (_view::insert_pkg): Ditto. + (_view::insert_category): Ditto. + (set_view_mode): Ditto. + (getcategorybyname): Remove. + * cygpackage.h: Include category_list.h to parse this correctly. + (cygpackage): New virtual &Categories. + * ini.h (_CategoryPackage): More C to C++ conversion. + (Category): Remove. + (getcategorybyname): Remove. + (register_category): Remove. + * iniparse.y: Include package db and category headers. + (categories): Use package_db to access global categories. + (category): Remove. + (ncategories): Remove. + (register_category): Remove. + * package.h: New virtual &Categories. + * package_db.cc (packagedb::packagedb): Don't read the installed db twice. + (packagedb::categories): Declare. + * package_db.h (packagedb): New member categories for global categories list. + 2001-11-21 Robert Collins * choose.h: Don't include ini.h diff --git a/Makefile.in b/Makefile.in index 2044e972..bc88a3b8 100644 --- a/Makefile.in +++ b/Makefile.in @@ -84,6 +84,8 @@ OBJS = \ archive_tar.o \ archive_tar_file.o \ autoload.o \ + category.o \ + category_list.o \ choose.o \ compress.o \ compress_bz.o \ diff --git a/category.cc b/category.cc new file mode 100644 index 00000000..a24441ac --- /dev/null +++ b/category.cc @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2001, Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + * + * Written by Robert Collins + * + */ + +/* categories for packages */ + +#include +#include + +#include "category.h" + +/* normal members */ + +Category::Category ():next (0), name (0) +{ +} + +Category::Category (const char *categoryname): +next (0) +{ + /* FIXME: search the global category list for name, and reuse that pointer */ + name = strdup (categoryname); +} diff --git a/category.h b/category.h new file mode 100644 index 00000000..d1e774ed --- /dev/null +++ b/category.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2001, Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + * + * Written by Robert Collins + * + */ + +/* categories for packages */ + +#ifndef _CATEGORY_H_ +#define _CATEGORY_H_ + +class CategoryPackage; + +class Category +{ +public: + Category (); + Category (const char *); + + Category *next; /* the next category in the list */ + const char *name; /* the category */ + CategoryPackage *packages; /* the packages in this category */ +}; + + +#endif /* _CATEGORY_H_ */ diff --git a/category_list.cc b/category_list.cc new file mode 100755 index 00000000..64307022 --- /dev/null +++ b/category_list.cc @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2001, Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + * + * Written by Robert Collins + * + */ + +/* categories for packages */ + +#include +#include + +#include "category_list.h" +#include "category.h" + +CategoryList::CategoryList ():_categories (0), ncategories (0), +categoriesspace (0) +{ +} + +Category * +CategoryList::register_category (const char *catname) +{ + Category *tempcat = getcategorybyname (catname); + if (!tempcat) + { + if (ncategories == categoriesspace) + { + Category **newcategories = (Category **) realloc (_categories, + sizeof (Category + *) * + (categoriesspace + + 20)); + if (!newcategories) + { + //die + exit (100); + } + _categories = newcategories; + if (categoriesspace == 0) + _categories[0] = 0; + categoriesspace += 20; + } + tempcat = new Category (catname); + size_t n; + for (n = 0; + n < ncategories + && strcasecmp (_categories[n]->name, tempcat->name) < 0; n++); + /* insert at n */ + if (n) + _categories[n - 1]->next = tempcat; + tempcat->next = ncategories >= n ? _categories[n] : 0; + memmove (&_categories[n + 1], &_categories[n], + ncategories * sizeof (Category *)); + _categories[n] = tempcat; + ncategories++; + } + return tempcat; +} + +Category * +CategoryList::getcategorybyname (const char *name) +{ + for (size_t n = 0; n < ncategories; n++) + if (strcasecmp (_categories[n]->name, name) == 0) + return _categories[n]; + return 0; +} diff --git a/category_list.h b/category_list.h new file mode 100755 index 00000000..c5e7eca4 --- /dev/null +++ b/category_list.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2001, Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + * + * Written by Robert Collins + * + */ + +/* a list categories for packages */ + +#ifndef _CATEGORY_LIST_H_ +#define _CATEGORY_LIST_H_ + +class Category; + +class CategoryList +{ +public: + Category * register_category (const char *name); + Category *getcategorybyname (const char *name); + int categories () + { + return ncategories; + }; + Category *getfirstcategory () + { + return *_categories; + }; + CategoryList (); +private: + Category ** _categories; + size_t ncategories; + size_t categoriesspace; +}; + + +#endif /* _CATEGORY_LIST_H_ */ diff --git a/choose.cc b/choose.cc index 0393bfea..6aee9b67 100644 --- a/choose.cc +++ b/choose.cc @@ -47,6 +47,8 @@ static const char *cvsid = #include "filemanip.h" #include "io_stream.h" #include "choose.h" +#include "category.h" +#include "category_list.h" #include "package_db.h" #include "package_meta.h" @@ -567,9 +569,10 @@ set_existence () static void fill_missing_category () { + packagedb db; for (Package * pkg = package; pkg->name; pkg++) if (!pkg->exclude && !pkg->category) - add_category (pkg, register_category ("Misc")); + add_category (pkg, db.categories.register_category ("Misc")); } static actions @@ -825,12 +828,14 @@ _view::insert_pkg (Package * pkg) if (pkg->exclude) return; line.set_line (pkg); + packagedb db; if (view_mode != VIEW_CATEGORY) { if (lines == NULL) { - lines = (pick_line *) calloc (npackages + ncategories, - sizeof (pick_line)); + lines = + (pick_line *) calloc (npackages + db.categories.categories (), + sizeof (pick_line)); nlines = 0; insert_at (0, line); } @@ -873,9 +878,12 @@ _view::insert_category (Category * cat, int collapsed) line.set_line (cat); if (lines == NULL) { + packagedb db; lines = - (pick_line *) malloc ((npackages + ncategories) * sizeof (pick_line)); - memset (lines, '\0', (npackages + ncategories) * sizeof (pick_line)); + (pick_line *) malloc ((npackages + db.categories.categories ()) * + sizeof (pick_line)); + memset (lines, '\0', + (npackages + db.categories.categories ()) * sizeof (pick_line)); nlines = 0; insert_at (0, line); if (!collapsed) @@ -1098,9 +1106,13 @@ set_view_mode (HWND h, views mode) chooser->insert_pkg (pkg); break; case VIEW_CATEGORY: - /* start collapsed. TODO: make this a chooser flag */ - for (Category * cat = category; cat; cat = cat->next) - chooser->insert_category (cat, CATEGORY_COLLAPSED); + { + packagedb db; + /* start collapsed. TODO: make this a chooser flag */ + for (Category * cat = db.categories.getfirstcategory (); cat; + cat = cat->next) + chooser->insert_category (cat, CATEGORY_COLLAPSED); + } break; default: break; @@ -1375,17 +1387,6 @@ getpkgbyname (const char *pkgname) return NULL; } -/* Return a pointer to a category given the name. */ -Category * -getcategorybyname (const char *categoryname) -{ - for (Category * cat = category; cat; cat = cat->next) - if (strcasecmp (cat->name, categoryname) == 0) - return cat; - - return NULL; -} - /* Return a pointer to a category of a given package given the name. */ Category * getpackagecategorybyname (Package * pkg, const char *categoryname) diff --git a/cygpackage.cc b/cygpackage.cc index 035274a6..3be77251 100644 --- a/cygpackage.cc +++ b/cygpackage.cc @@ -26,15 +26,21 @@ static const char *cvsid = #include #include #include "concat.h" - + #include "io_stream.h" #include "compress.h" - -#include "package.h" + +#include "package.h" #include "cygpackage.h" /* this constructor creates an installed package */ -cygpackage::cygpackage (const char *pkgname) : vendor (0),packagev (0), status (package_installed), type(package_binary), listdata (0), listfile (0) +cygpackage::cygpackage (const char *pkgname): +vendor (0), +packagev (0), +status (package_installed), +type (package_binary), +listdata (0), +listfile (0) { name = strdup (pkgname); /* FIXME: query the install database for the currently installed @@ -44,27 +50,34 @@ cygpackage::cygpackage (const char *pkgname) : vendor (0),packagev (0), status ( /* create a package given explicit details - perhaps should be modified to take the filename and do it's own parsing? */ -cygpackage::cygpackage (const char *pkgname, const char *filename, size_t fs, const char *version, package_status_t newstatus, package_type_t newtype) : status (newstatus), type (newtype), listdata (0), listfile (0), filesize (fs) +cygpackage::cygpackage (const char *pkgname, const char *filename, size_t fs, + const char *version, package_status_t newstatus, + package_type_t newtype): +status (newstatus), +type (newtype), +listdata (0), +listfile (0), +filesize (fs) { name = strdup (pkgname); fn = strdup (fn); char *curr = strchr (version, '-'); if (curr) - { - char *next; - while ((next = strchr (curr+1, '-'))) - curr = next; - /* curr = last - in the version string */ - packagev = strdup (curr+1); - vendor = strdup (version); - vendor [curr - version] = '\0'; - } + { + char *next; + while ((next = strchr (curr + 1, '-'))) + curr = next; + /* curr = last - in the version string */ + packagev = strdup (curr + 1); + vendor = strdup (version); + vendor[curr - version] = '\0'; + } else - { - packagev = 0; - vendor = strdup (version); - } + { + packagev = 0; + vendor = strdup (version); + } } cygpackage::~cygpackage () @@ -80,15 +93,15 @@ cygpackage::~cygpackage () void cygpackage::destroy () { - + if (name) free (name); -if (vendor) - free (vendor); -if (packagev) - free (packagev); -if (fn) - free (fn); + if (vendor) + free (vendor); + if (packagev) + free (packagev); + if (fn) + free (fn); if (listdata) delete listdata; @@ -99,12 +112,14 @@ cygpackage::getfirstfile () { if (listdata) delete listdata; - listfile = io_stream::open (concat ("cygfile:///etc/setup/", name, ".lst.gz", 0), "rb"); + listfile = + io_stream::open (concat ("cygfile:///etc/setup/", name, ".lst.gz", 0), + "rb"); listdata = compress::decompress (listfile); if (!listdata) return 0; - + return listdata->gets (fn, sizeof (fn)); } @@ -136,8 +151,7 @@ cygpackage::Package_version () } #if 0 -package_stability_t -cygpackage::Stability () +package_stability_t cygpackage::Stability () { return stability; } diff --git a/cygpackage.h b/cygpackage.h index e35b6be2..836f9528 100644 --- a/cygpackage.h +++ b/cygpackage.h @@ -20,35 +20,51 @@ #ifndef _CYGPACKAGE_H_ #define _CYGPACKAGE_H_ -class cygpackage : public genericpackage +#include "category_list.h" + +class cygpackage:public genericpackage { public: virtual const char *Name (); virtual const char *Vendor_version (); virtual const char *Package_version (); - virtual package_status_t Status () {return status;}; - virtual package_type_t Type () {return type;}; + virtual package_status_t Status () + { + return status; + }; + virtual package_type_t Type () + { + return type; + }; + virtual CategoryList & Categories () + { + return categories; + }; /* pass the name of the package when constructing */ - cygpackage(const char *); - cygpackage(const char *, const char *, size_t, const char *, package_status_t, package_type_t); - virtual ~cygpackage (); + cygpackage (const char *); + cygpackage (const char *, const char *, size_t, const char *, + package_status_t, package_type_t); + + + virtual ~ cygpackage (); /* TODO: we should probably return a metaclass - file name & path & size & type - ie doc/script/binary */ - virtual const char *getfirstfile(); - virtual const char *getnextfile(); + virtual const char *getfirstfile (); + virtual const char *getnextfile (); private: void destroy (); char *name; char *vendor; char *packagev; char *fn; - + CategoryList categories; + // package_stability_t stability; package_status_t status; package_type_t type; - + io_stream *listdata, *listfile; size_t filesize; }; diff --git a/ini.h b/ini.h index c98e3460..a4e53966 100644 --- a/ini.h +++ b/ini.h @@ -100,19 +100,11 @@ typedef struct _Info } Info; /* +1 for TRUST_UNKNOWN */ -typedef struct _CategoryPackage -{ - struct _CategoryPackage *next; /* The next package pointer in the list */ - char *pkgname; /* This should be Package *, but the packages can move */ -} -CategoryPackage; - -class Category +class CategoryPackage { public: - Category *next; /* the next category in the list */ - char *name; /* the category */ - CategoryPackage *packages; /* the packages in this category */ + CategoryPackage *next; /* The next package pointer in the list */ + char *pkgname; /* This should be Package *, but the packages can move */ }; typedef struct _Dependency @@ -149,16 +141,12 @@ public: extern Package *package; extern int npackages; -extern Category *category; -extern int ncategories; Package *new_package (char *name); Package *getpkgbyname (const char *pkgname); void new_requirement (Package * package, char *dependson); - Category *getcategorybyname (const char *categoryname); Category *getpackagecategorybyname (Package * pkg, const char *categoryname); - Category *register_category (const char *name); void add_category (Package * package, Category * cat); #endif diff --git a/iniparse.y b/iniparse.y index 2a4e9bae..b23690a1 100644 --- a/iniparse.y +++ b/iniparse.y @@ -21,6 +21,9 @@ #include #include "win32.h" +#include "package_db.h" +#include "category.h" +#include "category_list.h" #include "ini.h" #include "iniparse.h" #include "filemanip.h" @@ -39,6 +42,8 @@ extern unsigned int setup_timestamp; extern char *setup_version; extern int yylineno; +packagedb db; + #define cpt (cp->info+trust) %} @@ -117,9 +122,9 @@ requires ; categories - : STRING { add_category (cp, register_category ($1)); + : STRING { add_category (cp, db.categories.register_category ($1)); } categories - | STRING { add_category (cp, register_category ($1)); } + | STRING { add_category (cp, db.categories.register_category ($1)); } ; %% @@ -127,8 +132,6 @@ categories Package *package = NULL; int npackages = 0; static int maxpackages = 0; -Category *category = NULL; -int ncategories = 0; Package * new_package (char *name) @@ -168,58 +171,18 @@ new_requirement (Package *package, char *dependson) cp->required = dp; } -Category * -register_category (const char *name) -{ - Category *tempcat; - if (category == NULL) - ncategories = 0; - tempcat = getcategorybyname (name); - if (!tempcat) - { - Category *sortcat = category; - tempcat = new (Category); - memset (tempcat, '\0', sizeof (Category)); - tempcat->name = strdup (name); - if (!sortcat || strcasecmp(sortcat->name, name) > 0) - { - tempcat->next = category; - category = tempcat; - } - else - { - tempcat->next = sortcat->next; - sortcat->next = tempcat; - while (sortcat->next && - strcasecmp(sortcat->next->name, tempcat->name) < 0) - { - tempcat->next = sortcat->next; - sortcat->next = tempcat; - } - } - ncategories++; - } - return tempcat; -} - void add_category (Package *pkg, Category *cat) { /* add a new record for the package list */ /* TODO: alpabetical inserts ? */ - Category *tempcat; - CategoryPackage *templink; - tempcat = new (Category); - memset (tempcat, '\0', sizeof (Category)); + Category *tempcat = new Category; tempcat->next = pkg->category; tempcat->name = cat->name; pkg->category = tempcat; - templink = new (CategoryPackage); + CategoryPackage *templink = new CategoryPackage; templink->next = cat->packages; templink->pkgname = pkg->name; cat->packages = templink; - - /* hack to ensure we allocate enough space */ - ncategories++; } diff --git a/package.h b/package.h index e0904d48..44cac0ed 100755 --- a/package.h +++ b/package.h @@ -37,6 +37,8 @@ * now consider it old. */ +class CategoryList; + typedef enum { package_invalid, @@ -76,6 +78,7 @@ public: */ virtual const char *getfirstfile () = 0; virtual const char *getnextfile () = 0; + virtual CategoryList &Categories () = 0; virtual ~ genericpackage () { diff --git a/package_db.cc b/package_db.cc index 9d177428..c60518d1 100644 --- a/package_db.cc +++ b/package_db.cc @@ -121,6 +121,7 @@ packagedb::packagedb () // unknown dbversion exit (1); } + installeddbread = 1; } db = 0; curr_package = 0; @@ -173,8 +174,14 @@ packagedb::addpackage (packagemeta & newpackage) packagemeta ** packagedb::packages = 0; -size_t packagedb::packagecount = 0; -size_t packagedb::packagespace = 0; +size_t + packagedb::packagecount = + 0; +size_t + packagedb::packagespace = + 0; int packagedb::installeddbread = 0; +CategoryList + packagedb::categories; diff --git a/package_db.h b/package_db.h index 4d9022e2..173dc47e 100644 --- a/package_db.h +++ b/package_db.h @@ -16,7 +16,9 @@ #ifndef _PACKAGE_DB_H_ #define _PACKAGE_DB_H_ +class CategoryList; class packagemeta; +class io_stream; /*TODO: add mutexs */ class packagedb @@ -28,6 +30,8 @@ public: packagemeta *getpackagebyname (const char *); /* 0 on success */ int addpackage (packagemeta &); + /* all seen categories */ + static CategoryList categories; private: /* this gets sorted */ static packagemeta **packages; -- 2.43.5