Use wstring throughout (except internally in struct cpe_name).

Add a typedef for struct cpe_name and use it throughout.
Fix inverted logic in cpe_upgrade().
This commit is contained in:
Dag-Erling Smørgrav 2014-11-01 20:15:45 +00:00 committed by des
parent a587a25d45
commit e4dd25fa79
5 changed files with 48 additions and 33 deletions

View file

@ -65,18 +65,25 @@ enum cpe23_attributes {
cpe23_nattr cpe23_nattr
}; };
struct cpe_name { typedef struct cpe_name {
int ver; int ver;
int nattr; int nattr;
wchar_t *attr[]; wchar_t *attr[];
}; } cpe_name;
struct cpe_name *cpe_upgrade(const struct cpe_name *); cpe_name *cpe_new(void);
struct cpe_name *cpe_from_string(const wchar_t *); void cpe_destroy(cpe_name *);
wchar_t *cpe_to_string(const struct cpe_name *); cpe_name *cpe_clone(const cpe_name *);
struct cpe_name *cpe_from_string(const wchar_t *);
wchar_t *cpe_to_string(const struct cpe_name *); cpe_name *cpe_upgrade(const cpe_name *);
struct cpe_name *cpe_from_string(const wchar_t *);
wchar_t *cpe_to_string(const struct cpe_name *); cpe_name *cpe_unbind_fs(const wstring *);
wstring *cpe_bind_to_fs(const cpe_name *);
cpe_name *cpe_unbind_uri(const wstring *);
wstring *cpe_bind_to_uri(const cpe_name *);
cpe_name *cpe_from_wfn(const wchar_t *);
wchar_t *cpe_to_wfn(const cpe_name *);
#endif #endif

View file

@ -30,15 +30,17 @@
#include "cryb/impl.h" #include "cryb/impl.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include <cryb/wstring.h>
#include <cryb/cpe.h> #include <cryb/cpe.h>
/* /*
* Free all memory allocated to a cpe structure. * Free all memory allocated to a cpe structure.
*/ */
void void
cpe_destroy(struct cpe_name *cpe) cpe_destroy(cpe_name *cpe)
{ {
if (cpe == NULL) if (cpe == NULL)
@ -54,7 +56,7 @@ cpe_destroy(struct cpe_name *cpe)
* corresponding attribute in the destination will be an empty string. * corresponding attribute in the destination will be an empty string.
*/ */
static int static int
cpe_copy_attr(struct cpe_name *dst, const struct cpe_name *src, int base, int nattr) cpe_copy_attr(cpe_name *dst, const cpe_name *src, int base, int nattr)
{ {
if (base < 0 || nattr < 0 || base + nattr > src->nattr || if (base < 0 || nattr < 0 || base + nattr > src->nattr ||
@ -77,10 +79,10 @@ cpe_copy_attr(struct cpe_name *dst, const struct cpe_name *src, int base, int na
* Duplicate a cpe structure. Any attributes that are NULL in the source * Duplicate a cpe structure. Any attributes that are NULL in the source
* will be empty strings in the destination. * will be empty strings in the destination.
*/ */
struct cpe_name * cpe_name *
cpe_clone(const struct cpe_name *cpe) cpe_clone(const cpe_name *cpe)
{ {
struct cpe_name *ncpe; cpe_name *ncpe;
ncpe = calloc(1, sizeof *ncpe + ncpe = calloc(1, sizeof *ncpe +
cpe->nattr * sizeof *ncpe->attr); cpe->nattr * sizeof *ncpe->attr);
@ -96,10 +98,10 @@ cpe_clone(const struct cpe_name *cpe)
/* /*
* Allocate a new cpe structure. * Allocate a new cpe structure.
*/ */
struct cpe_name * cpe_name *
cpe_new(void) cpe_new(void)
{ {
struct cpe_name *ncpe; cpe_name *ncpe;
if ((ncpe = calloc(1, sizeof *ncpe)) == NULL) if ((ncpe = calloc(1, sizeof *ncpe)) == NULL)
return (NULL); return (NULL);
@ -111,10 +113,10 @@ cpe_new(void)
/* /*
* Upgrade a cpe 2.2 structure to the latest supported version. * Upgrade a cpe 2.2 structure to the latest supported version.
*/ */
struct cpe_name * cpe_name *
cpe_upgrade22(const struct cpe_name *cpe) cpe_upgrade22(const cpe_name *cpe)
{ {
struct cpe_name *ncpe; cpe_name *ncpe;
if ((ncpe = cpe_new()) == NULL) if ((ncpe = cpe_new()) == NULL)
return (NULL); return (NULL);
@ -140,15 +142,15 @@ cpe_upgrade22(const struct cpe_name *cpe)
/* /*
* Upgrade a cpe structure to the latest supported version. * Upgrade a cpe structure to the latest supported version.
*/ */
struct cpe_name * cpe_name *
cpe_upgrade(const struct cpe_name *cpe) cpe_upgrade(const cpe_name *cpe)
{ {
switch (cpe->ver) { switch (cpe->ver) {
case cpe22_ver: case cpe23_ver:
/* already latest */ /* already latest */
return (cpe_clone(cpe)); return (cpe_clone(cpe));
case cpe23_ver: case cpe22_ver:
return (cpe_upgrade22(cpe)); return (cpe_upgrade22(cpe));
default: default:
return (NULL); return (NULL);

View file

@ -29,20 +29,22 @@
#include "cryb/impl.h" #include "cryb/impl.h"
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include <cryb/wstring.h>
#include <cryb/cpe.h> #include <cryb/cpe.h>
struct cpe * cpe_name *
cpe_unbind_fs(const wchar_t *str) cpe_unbind_fs(const wstring *str)
{ {
(void)str; (void)str;
return (NULL); return (NULL);
} }
wchar_t * wstring *
cpe_bind_to_fs(const struct cpe *cpe) cpe_bind_to_fs(const cpe_name *cpe)
{ {
(void)cpe; (void)cpe;

View file

@ -29,20 +29,22 @@
#include "cryb/impl.h" #include "cryb/impl.h"
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include <cryb/wstring.h>
#include <cryb/cpe.h> #include <cryb/cpe.h>
struct cpe * cpe_name *
cpe_unbind_uri(const wchar_t *str) cpe_unbind_uri(const wstring *str)
{ {
(void)str; (void)str;
return (NULL); return (NULL);
} }
wchar_t * wstring *
cpe_bind_to_uri(const struct cpe *cpe) cpe_bind_to_uri(const cpe_name *cpe)
{ {
(void)cpe; (void)cpe;

View file

@ -29,11 +29,13 @@
#include "cryb/impl.h" #include "cryb/impl.h"
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include <cryb/wstring.h>
#include <cryb/cpe.h> #include <cryb/cpe.h>
struct cpe * cpe_name *
cpe_from_wfn(const wchar_t *str) cpe_from_wfn(const wchar_t *str)
{ {
@ -42,7 +44,7 @@ cpe_from_wfn(const wchar_t *str)
} }
wchar_t * wchar_t *
cpe_to_wfn(const struct cpe *cpe) cpe_to_wfn(const cpe_name *cpe)
{ {
(void)cpe; (void)cpe;