libmp/src/package/keep.c

70 lines
1.3 KiB
C
Raw Normal View History

2024-07-02 01:44:07 +00:00
#include "../../include/package.h"
#include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <string.h>
size_t lm_package_keep_count(lm_pkg_t *pkg){
size_t index = 0;
if(NULL == pkg->keeps)
return index;
while(pkg->keeps[index] != NULL)
index++;
return index;
}
bool lm_package_keep_add(lm_pkg_t *pkg, char *path){
if(NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps){
pkg->keeps = malloc(sizeof(char*)*2);
pkg->keeps[0] = strdup(path);
pkg->keeps[1] = NULL;
return true;
}
size_t count = lm_package_depend_count(pkg);
pkg->keeps = realloc(pkg->depends, sizeof(char*)*(count+2));
pkg->keeps[count++] = strdup(path);
pkg->keeps[count] = NULL;
return true;
}
bool lm_package_keep_contains(lm_pkg_t *pkg, char *path){
if(NULL == pkg || NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps)
return false;
for(int i = 0; NULL != pkg->keeps[i]; i++)
if(eq(pkg->keeps[i], path))
return true;
return false;
}
void lm_package_keep_free(lm_pkg_t *pkg){
if(NULL == pkg)
return;
if(NULL == pkg->keeps)
return;
for(int i = 0; pkg->keeps[i] != NULL; i++)
free(pkg->keeps[i]);
free(pkg->keeps);
pkg->keeps = NULL;
}