Commit fe77a268 authored by guinux's avatar guinux

code optimization

parent aaa977b7
......@@ -361,50 +361,50 @@ internal class AlpmConfig {
// DataInputStream, so we can read line by line
var dis = new DataInputStream (file.read ());
string? line;
string[] data = {};
var data = new StringBuilder ();
// Read lines until end of file (null) is reached
while ((line = dis.read_line ()) != null) {
if (line.length == 0) {
data += "\n";
data.append ("\n");
continue;
}
if (line.contains ("IgnorePkg")) {
if (new_conf.contains ("IgnorePkg")) {
string val = new_conf.get ("IgnorePkg").get_string ();
if (val == "") {
data += "#IgnorePkg =\n";
data.append ("#IgnorePkg =\n");
} else {
data += "IgnorePkg = %s\n".printf (val);
data.append ("IgnorePkg = %s\n".printf (val));
}
// simply comment other IgnorePkg lines
new_conf.replace ("IgnorePkg", "");
} else {
data += line + "\n";
data.append (line);
data.append ("\n");
}
} else if (line.contains ("CheckSpace")) {
if (new_conf.contains ("CheckSpace")) {
bool val = new_conf.get ("CheckSpace").get_boolean ();
if (val) {
data += "CheckSpace\n";
data.append ("CheckSpace\n");
} else {
data += "#CheckSpace\n";
data.append ("#CheckSpace\n");
}
new_conf.remove ("CheckSpace");
} else {
data += line + "\n";
data.append (line);
data.append ("\n");
}
} else {
data += line + "\n";
data.append (line);
data.append ("\n");
}
}
// delete the file before rewrite it
file.delete ();
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
foreach (unowned string new_line in data) {
// writing a short string to the stream
dos.put_string (new_line);
}
dos.put_string (data.str);
reload ();
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
......
......@@ -94,14 +94,14 @@ namespace Pamac {
internal bool force_refresh;
internal bool enable_downgrade;
internal int flags;
string[] to_syncfirst;
GenericSet<string?> to_syncfirst;
internal string[] to_install;
internal string[] to_remove;
internal string[] to_load;
internal string[] to_build;
internal bool sysupgrade;
AURPackageStruct[] to_build_pkgs;
GLib.List<string> aur_pkgbases_to_build;
GenericSet<string?> aur_pkgbases_to_build;
HashTable<string, string> to_install_as_dep;
internal string[] temporary_ignorepkgs;
internal string[] overwrite_files;
......@@ -130,7 +130,8 @@ namespace Pamac {
this.config = config;
alpm_config = new AlpmConfig ("/etc/pacman.conf");
tmp_path = "/tmp/pamac";
aur_pkgbases_to_build = new GLib.List<string> ();
to_syncfirst = new GenericSet<string?> (str_hash, str_equal);
aur_pkgbases_to_build = new GenericSet<string?> (str_hash, str_equal);
to_install_as_dep = new HashTable<string, string> (str_hash, str_equal);
timer = new Timer ();
current_error = ErrorInfos ();
......@@ -413,13 +414,13 @@ namespace Pamac {
return false;
}
// check syncfirsts
to_syncfirst = {};
to_syncfirst.remove_all ();
foreach (unowned string name in alpm_config.get_syncfirsts ()) {
unowned Alpm.Package? pkg = Alpm.find_satisfier (alpm_handle.localdb.pkgcache, name);
if (pkg != null) {
unowned Alpm.Package? candidate = pkg.sync_newversion (alpm_handle.syncdbs);
if (candidate != null) {
to_syncfirst += candidate.name;
to_syncfirst.add (candidate.name);
}
}
}
......@@ -676,7 +677,8 @@ namespace Pamac {
internal void trans_prepare () {
to_build_pkgs = {};
aur_pkgbases_to_build = new GLib.List<string> ();
aur_conflicts_to_remove = {};
aur_pkgbases_to_build.remove_all ();
to_install_as_dep.remove_all ();
launch_trans_prepare_real ();
}
......@@ -740,7 +742,7 @@ namespace Pamac {
internal void build_prepare () {
to_build_pkgs = {};
aur_pkgbases_to_build = new GLib.List<string> ();
aur_pkgbases_to_build.remove_all ();
to_install_as_dep.remove_all ();
// get an handle with fake aur db and without emit signal callbacks
alpm_handle = alpm_config.get_handle ();
......@@ -847,9 +849,7 @@ namespace Pamac {
if (db != null) {
if (db.name == "aur") {
// it is a aur pkg to build
if (aur_pkgbases_to_build.find_custom (trans_pkg.pkgbase, strcmp) == null) {
aur_pkgbases_to_build.append (trans_pkg.pkgbase);
}
aur_pkgbases_to_build.add (trans_pkg.pkgbase);
to_build_pkgs += AURPackageStruct () {
name = trans_pkg.name,
version = trans_pkg.version,
......@@ -946,11 +946,6 @@ namespace Pamac {
to_remove += initialise_pkg_struct (trans_pkg);
pkgs_to_remove.next ();
}
PackageStruct[] conflicts_to_remove = {};
foreach (unowned PackageStruct pkg in aur_conflicts_to_remove){
conflicts_to_remove += pkg;
}
aur_conflicts_to_remove = {};
string[] pkgbases_to_build = {};
foreach (unowned string name in aur_pkgbases_to_build) {
pkgbases_to_build += name;
......@@ -962,7 +957,7 @@ namespace Pamac {
to_reinstall = (owned) to_reinstall,
to_remove = (owned) to_remove,
to_build = to_build_pkgs,
aur_conflicts_to_remove = (owned) conflicts_to_remove,
aur_conflicts_to_remove = aur_conflicts_to_remove,
aur_pkgbases_to_build = (owned) pkgbases_to_build
};
return summary;
......
......@@ -56,12 +56,16 @@ namespace Pamac {
if (needles.length == 0) {
return new Json.Array ();
} else {
Json.Array[] found_array = {};
var all_found = new SList<Json.Array> ();
foreach (unowned string needle in needles) {
found_array += yield rpc_query (rpc_url + rpc_search + Uri.escape_string (needle));
var builder = new StringBuilder ();
builder.append (rpc_url);
builder.append (rpc_search);
builder.append (Uri.escape_string (needle));
all_found.append (yield rpc_query (builder.str));
}
var result = new Json.Array ();
foreach (unowned Json.Array found in found_array) {
foreach (unowned Json.Array found in all_found) {
if (found.get_length () == 0) {
continue;
}
......
......@@ -1797,6 +1797,7 @@ namespace Pamac {
}
// get user input
while (true) {
stdout.printf ("\n");
stdout.printf ("%s: ", dgettext (null, "Enter a selection (default=%s)").printf (dgettext (null, "all")));
string ans = stdin.read_line ();
uint64 nb;
......@@ -1836,7 +1837,6 @@ namespace Pamac {
}
}
}
stdout.printf ("\n");
if (numbers.length > 0) {
foreach (uint64 number in numbers) {
to_install += pkgs.nth_data ((uint) number -1).name;
......
......@@ -252,14 +252,14 @@ namespace Pamac {
return screenshot;
}
As.App[] get_pkgname_matching_apps (string pkgname) {
As.App[] matching_apps = {};
SList<As.App> get_pkgname_matching_apps (string pkgname) {
var matching_apps = new SList<As.App> ();
app_store.get_apps ().foreach ((app) => {
if (app.get_pkgname_default () == pkgname) {
matching_apps += app;
matching_apps.append (app);
}
});
return matching_apps;
return (owned) matching_apps;
}
PackageStruct initialise_pkg_struct (Alpm.Package? alpm_pkg) {
......@@ -294,9 +294,9 @@ namespace Pamac {
}
if (repo_name != "" && repo_name != dgettext (null, "AUR")) {
// find if pkgname provides only one app
As.App[] matching_apps = get_pkgname_matching_apps (alpm_pkg.name);
if (matching_apps.length == 1) {
As.App app = matching_apps[0];
var matching_apps = get_pkgname_matching_apps (alpm_pkg.name);
if (matching_apps.length () == 1) {
As.App app = matching_apps.nth_data (0);
app_name = get_app_name (app);
desc = get_app_summary (app);
icon = get_app_icon (app, repo_name);
......@@ -355,8 +355,8 @@ namespace Pamac {
repo_name = alpm_pkg.db.name;
}
if (repo_name != "" && repo_name != dgettext (null, "AUR")) {
As.App[] apps = get_pkgname_matching_apps (alpm_pkg.name);
if (apps.length > 0) {
var apps = get_pkgname_matching_apps (alpm_pkg.name);
if (apps.length () > 0) {
// alpm_pkg provide some apps
foreach (unowned As.App app in apps) {
pkgs.append (new Package.from_struct (PackageStruct () {
......@@ -975,9 +975,9 @@ namespace Pamac {
});
} else {
// find if pkgname provides only one app
As.App[] matching_apps = get_pkgname_matching_apps (pkgname);
if (matching_apps.length == 1) {
As.App app = matching_apps[0];
var matching_apps = get_pkgname_matching_apps (pkgname);
if (matching_apps.length () == 1) {
As.App app = matching_apps.nth_data (0);
app_name = get_app_name (app);
desc = get_app_summary (app);
try {
......@@ -1668,7 +1668,7 @@ namespace Pamac {
string line;
string current_section = "";
bool current_section_is_pkgbase = true;
var version = new StringBuilder ();
var version = new StringBuilder ("");
string pkgbase = "";
string desc = "";
var pkgnames_found = new SList<string> ();
......
......@@ -174,7 +174,7 @@ namespace Pamac {
public void write (HashTable<string,Variant> new_conf) {
var file = GLib.File.new_for_path (conf_path);
var data = new GLib.List<string> ();
var data = new StringBuilder();
if (file.query_exists ()) {
try {
// Open file for reading and wrap returned FileInputStream into a
......@@ -197,21 +197,24 @@ namespace Pamac {
}
new_conf.remove ("RemoveUnrequiredDeps");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("RefreshPeriod")) {
if (new_conf.lookup_extended ("RefreshPeriod", null, out variant)) {
data.append ("RefreshPeriod = %llu\n".printf (variant.get_uint64 ()));
new_conf.remove ("RefreshPeriod");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("KeepNumPackages")) {
if (new_conf.lookup_extended ("KeepNumPackages", null, out variant)) {
data.append ("KeepNumPackages = %llu\n".printf (variant.get_uint64 ()));
new_conf.remove ("KeepNumPackages");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("OnlyRmUninstalled")) {
if (new_conf.lookup_extended ("OnlyRmUninstalled", null, out variant)) {
......@@ -222,7 +225,8 @@ namespace Pamac {
}
new_conf.remove ("OnlyRmUninstalled");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("NoUpdateHideIcon")) {
if (new_conf.lookup_extended ("NoUpdateHideIcon", null, out variant)) {
......@@ -233,7 +237,8 @@ namespace Pamac {
}
new_conf.remove ("NoUpdateHideIcon");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("EnableAUR")) {
if (new_conf.lookup_extended ("EnableAUR", null, out variant)) {
......@@ -244,14 +249,16 @@ namespace Pamac {
}
new_conf.remove ("EnableAUR");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("BuildDirectory")) {
if (new_conf.lookup_extended ("BuildDirectory", null, out variant)) {
data.append ("BuildDirectory = %s\n".printf (variant.get_string ()));
new_conf.remove ("BuildDirectory");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("CheckAURUpdates")) {
if (new_conf.lookup_extended ("CheckAURUpdates", null, out variant)) {
......@@ -262,7 +269,8 @@ namespace Pamac {
}
new_conf.remove ("CheckAURUpdates");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("CheckAURVCSUpdates")) {
if (new_conf.lookup_extended ("CheckAURVCSUpdates", null, out variant)) {
......@@ -273,7 +281,8 @@ namespace Pamac {
}
new_conf.remove ("CheckAURVCSUpdates");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("DownloadUpdates")) {
if (new_conf.lookup_extended ("DownloadUpdates", null, out variant)) {
......@@ -284,17 +293,20 @@ namespace Pamac {
}
new_conf.remove ("DownloadUpdates");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else if (line.contains ("MaxParallelDownloads")) {
if (new_conf.lookup_extended ("MaxParallelDownloads", null, out variant)) {
data.append ("MaxParallelDownloads = %llu\n".printf (variant.get_uint64 ()));
new_conf.remove ("MaxParallelDownloads");
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
} else {
data.append (line + "\n");
data.append (line);
data.append ("\n");
}
}
// delete the file before rewrite it
......@@ -369,10 +381,7 @@ namespace Pamac {
try {
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
foreach (unowned string new_line in data) {
// writing a short string to the stream
dos.put_string (new_line);
}
dos.put_string (data.str);
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
......
......@@ -179,10 +179,10 @@ namespace Pamac {
}
}
protected override List<string> choose_optdeps (string pkgname, List<string> optdeps) {
protected override List<string> choose_optdeps (string pkgname, string[] optdeps) {
var optdeps_to_install = new List<string> ();
// print pkgs
int num_length = optdeps.length ().to_string ().length + 1;
int num_length = optdeps.length.to_string ().length + 1;
stdout.printf ("%s:\n".printf (dgettext (null, "Choose optional dependencies for %s").printf (pkgname)));
int num = 1;
foreach (unowned string name in optdeps) {
......@@ -193,6 +193,7 @@ namespace Pamac {
}
// get user input
while (true) {
stdout.printf ("\n");
stdout.printf ("%s: ", dgettext (null, "Enter a selection (default=%s)").printf (dgettext (null, "none")));
string ans = stdin.read_line ();
uint64 nb;
......@@ -215,7 +216,7 @@ namespace Pamac {
if (int64.try_parse (splitted2[1], out end_num)) {
nb = beg_num;
while (nb <= end_num) {
if (nb >= 1 && nb < optdeps.length ()) {
if (nb >= 1 && nb <= optdeps.length) {
numbers += nb;
}
nb++;
......@@ -223,16 +224,15 @@ namespace Pamac {
}
}
} else if (uint64.try_parse (part, out nb)) {
if (nb >= 1 && nb < optdeps.length ()) {
if (nb >= 1 && nb <= optdeps.length) {
numbers += nb;
}
}
}
}
stdout.printf ("\n");
if (numbers.length > 0) {
foreach (uint64 number in numbers) {
optdeps_to_install.append (optdeps.nth_data ((uint) number -1));
optdeps_to_install.append (optdeps[number -1]);
}
break;
}
......@@ -242,11 +242,11 @@ namespace Pamac {
}
protected override int choose_provider (string depend, string[] providers) {
Package[] pkgs = {};
var pkgs = new SList<Package> ();
foreach (unowned string pkgname in providers) {
var pkg = database.get_sync_pkg (pkgname);
if (pkg.name != "") {
pkgs += pkg;
pkgs.append (pkg);
}
}
// print pkgs
......@@ -273,6 +273,7 @@ namespace Pamac {
}
// get user input
while (true) {
stdout.printf ("\n");
stdout.printf ("%s: ", dgettext (null, "Enter a number (default=%d)").printf (1));
string ans = stdin.read_line ();
int64 nb;
......@@ -284,7 +285,6 @@ namespace Pamac {
} else if (!int64.try_parse (ans, out nb)) {
nb = 0;
}
stdout.printf ("\n");
if (nb >= 1 && nb <= providers.length) {
int index = (int) nb - 1;
stdout.printf ("\n");
......
......@@ -178,7 +178,7 @@ namespace Pamac {
}
}
protected override List<string> choose_optdeps (string pkgname, List<string> optdeps) {
protected override List<string> choose_optdeps (string pkgname, string[] optdeps) {
var optdeps_to_install = new List<string> ();
choose_pkgs_dialog.title = dgettext (null, "Choose optional dependencies for %s").printf (pkgname);
choose_pkgs_dialog.pkgs_list.clear ();
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment