summaryrefslogtreecommitdiff
path: root/revdep/pkg.cpp
blob: f79a872e76354403ff7c071c4a6a6a848c8e5310 (plain)
    1 // Copyright (C) 2016 James Buren
    2 //
    3 // This file is part of revdep.
    4 //
    5 // revdep is free software: you can redistribute it and/or modify
    6 // it under the terms of the GNU General Public License as published by
    7 // the Free Software Foundation, either version 3 of the License, or
    8 // (at your option) any later version.
    9 //
   10 // revdep is distributed in the hope that it will be useful,
   11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 // GNU General Public License for more details.
   14 //
   15 // You should have received a copy of the GNU General Public License
   16 // along with revdep.  If not, see <http://www.gnu.org/licenses/>.
   17 
   18 #include "pkg.h"
   19 #include <fstream>
   20 #include <algorithm>
   21 #include <dirent.h>
   22 
   23 Package::Package(const std::string &name, const std::string &version, const StringVector &files) {
   24 	_name = name;
   25 	_version = version;
   26 	_files = files;
   27 	_dirs = {};
   28 	_ignore = false;
   29 }
   30 
   31 static Package readPackage(std::istream &in) {
   32 	std::string line;
   33 	size_t fields = 0;
   34 	std::string name;
   35 	std::string version;
   36 	StringVector files;
   37 
   38 	while(getline(in, line) && line != "") {
   39 		switch(++fields) {
   40 			case 1:                  name = line; break;
   41 			case 2:               version = line; break;
   42 			default: files.push_back("/" + line); break;
   43 		}
   44 	}
   45 
   46 	if(fields < 2) {
   47 		return Package("", "", files);
   48 	}
   49 
   50 	return Package(name, version, files);
   51 }
   52 
   53 bool ReadPackages(const std::string &path, PackageVector &pkgs) {
   54 	std::ifstream fin;
   55 
   56 	fin.open(path.c_str());
   57 
   58 	if(!fin.is_open()) {
   59 		return false;
   60 	}
   61 
   62 	while(true) {
   63 		Package pkg = readPackage(fin);
   64 
   65 		if(pkg.Name() == "" && pkg.Version() == "") {
   66 			break;
   67 		}
   68 
   69 		pkgs.push_back(pkg);
   70 	}
   71 
   72 	fin.close();
   73 
   74 	return (pkgs.size() > 0);
   75 }
   76 
   77 void ReadPackageDirs(const std::string &path, PackageVector &pkgs) {
   78 	DIR *dir;
   79 
   80 	dir = opendir(path.c_str());
   81 
   82 	if(dir == NULL) {
   83 		return;
   84 	}
   85 
   86 	struct dirent de, *res;
   87 
   88 	while(readdir_r(dir, &de, &res) == 0 && res != NULL) {
   89 		if(de.d_type != DT_REG) {
   90 			continue;
   91 		}
   92 
   93 		StringVector dirs;
   94 
   95 		ReadRdConf(path + "/" + de.d_name, dirs);
   96 
   97 		if(dirs.size() == 0) {
   98 			continue;
   99 		}
  100 
  101 		PackageVector::iterator pkg = std::find(pkgs.begin(), pkgs.end(), de.d_name);
  102 
  103 		if(pkg == pkgs.end()) {
  104 			continue;
  105 		}
  106 
  107 		pkg->Dirs(dirs);
  108 	}
  109 
  110 	closedir(dir);
  111 }

Generated by cgit