#include "../../include/package.h" #include "../../include/error.h" #include "../../include/types.h" #include "../../include/util.h" #include #include size_t lm_package_depend_count(lm_pkg_t *pkg){ size_t index = 0; if(NULL == pkg->depends) return index; while(pkg->depends[index] != NULL) index++; return index; } bool lm_package_depend_add(lm_pkg_t *pkg, char *depend){ if(NULL == depend){ lm_error_set(LM_ERR_ArgNULL); return false; } if(!package_name_valid(depend)){ lm_error_set(LM_ERR_PkgBadName); return false; } if(NULL == pkg->depends){ pkg->depends = malloc(sizeof(char*)*2); pkg->depends[0] = strdup(depend); pkg->depends[1] = NULL; return true; } size_t count = lm_package_depend_count(pkg); pkg->depends = realloc(pkg->depends, sizeof(char*)*(count+2)); pkg->depends[count++] = strdup(depend); pkg->depends[count] = NULL; return true; } void lm_package_depend_free(lm_pkg_t *pkg){ if(NULL == pkg) return; if(NULL == pkg->depends) return; for(int i = 0; pkg->depends[i] != NULL; i++) free(pkg->depends[i]); free(pkg->depends); pkg->depends = NULL; } size_t lm_package_depend_strlen(lm_pkg_t *pkg){ size_t len = 1; if(NULL == pkg->depends) return len; for(int i = 0; NULL != pkg->depends[i]; i++) len += strlen(pkg->depends[i])+1; return len; } char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ if(NULL == pkg){ lm_error_set(LM_ERR_ArgNULL); return NULL; } if(NULL == buffer){ size_t buflen = lm_package_depend_strlen(pkg)+1; buffer = malloc(buflen); bzero(buffer, buflen); } if(NULL == pkg->depends){ buffer[0] = 0; return buffer; } size_t bufsz = 0, depsz = 0; for(int i = 0; NULL != pkg->depends[i]; i++){ depsz = strlen(pkg->depends[i]); if(i == 0){ memcpy(buffer, pkg->depends[i], depsz); bufsz += depsz; continue; } buffer[bufsz++] = ','; memcpy(buffer+bufsz, pkg->depends[i], depsz); bufsz += depsz; } buffer[bufsz] = 0; return buffer; } bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){ if(NULL == pkg){ lm_error_set(LM_ERR_ArgNULL); return false; } lm_package_depend_free(pkg); if(NULL == buffer) return true; char *save = NULL, *dep = NULL; dep = strtok_r(buffer, ",", &save); if(NULL == dep) return true; do { if(!lm_package_depend_add(pkg, dep)) return false; }while((dep = strtok_r(NULL, ",", &save)) != NULL); return true; } bool lm_package_depends(lm_pkg_t *pkg, char *dep){ if(NULL == pkg || NULL == dep){ lm_error_set(LM_ERR_ArgNULL); return false; } if(NULL == pkg->depends) return false; for(int i = 0; pkg->depends[i] != NULL; i++) if(eq(pkg->depends[i], dep)) return true; return false; }