Improve flags using bitwise operations

This commit is contained in:
John Barns 2024-02-25 21:09:38 -06:00
parent f8398f24a9
commit 5021e5a86b
4 changed files with 36 additions and 28 deletions

BIN
bin/fex

Binary file not shown.

BIN
obj/fex.o

Binary file not shown.

Binary file not shown.

View File

@ -11,13 +11,23 @@
#include "config.h"
#include "debug.h"
enum Flags {
FLAG_INSTALL = 1 << 0,
FLAG_UPDATE = 1 << 1,
FLAG_SOURCE = 1 << 2,
FLAG_UNINSTALL = 1 << 3,
FLAG_LIST = 1 << 4,
FLAG_SEARCH = 1 << 5,
FLAG_FORCE = 1 << 6,
FLAG_UPGRADE = 1 << 7,
};
void help() {
nprint("E", "Usage: fex (OPT) (PACKAGES)");
}
int main(int argc, char** argv) {
int fIns = 0, fUpd = 0, fSrc = 0, fUin = 0, fLis = 0, fSer = 0, fVer = 0, fFrc = 0, fUpg = 0;
unsigned short flags = 0;
char* packages[argc - 1];
int pkgCount = 0;
if (argc < 2) {
@ -25,38 +35,33 @@ int main(int argc, char** argv) {
return 1;
}
for(int i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
for(int j = 1; argv[i][j] != '\0'; j++) {
switch(argv[i][j]) {
case 'i': // install
fIns = 1;
case 'i': // install, 0
flags |= FLAG_INSTALL;
break;
case 'u': // update
fUpd = 1;
case 'u': // update, 1
flags |= FLAG_UPDATE;
break;
case 'S': // source
fSrc = 1;
case 'S': // source, 2
flags |= FLAG_SOURCE;
break;
case 'r': // remove
fUin = 1;
case 'r': // remove, 3
flags |= FLAG_UNINSTALL;
break;
case 'l': // list
fLis = 1;
case 'l': // list, 4
flags |= FLAG_LIST;
break;
case 's': // search
fSer = 1;
case 's': // search, 5
flags |= FLAG_SEARCH;
break;
case 'v': // verbose
fVer = 1;
case 'f': // force, 6
flags |= FLAG_FORCE;
break;
case 'f': // force
fFrc = 1;
break;
case 'c': // upgrade; don't ask why I'm using 'c'
fUpg = 1;
case 'c': // upgrade; don't ask why I'm using 'c', 7
flags |= FLAG_UPGRADE;
break;
default:
nprint("E", "Flag '%c' is unknown.", argv[i][j]);
@ -72,12 +77,15 @@ int main(int argc, char** argv) {
#ifdef debugEnabled
DEBUG_PRINT("sysSanity: %d", sysSanity);
const char* flagNames[] = {"Install", "Update", "Source", "Remove", "List", "Search","Verbose", "Force", "Upgrade"};
int* flagValues[] = {&fIns, &fUpd, &fSrc, &fUin, &fLis, &fSer, &fVer, &fFrc, &fUpg};
int flagCount = sizeof(flagNames) / sizeof(flagNames[0]); // This should match the number of flags
for(int i = 0; i < flagCount; i++) {
nprint("D", "Flag %s: %d", flagNames[i], *flagValues[i]);
unsigned int flag_values[] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000};
char* flag_names[] = {"Install", "Update", "Source", "Uninstall", "List", "Search", "Force", "Upgrade"};
int num_flags = sizeof(flag_values) / sizeof(flag_values[0]);
for(int i = 0; i < num_flags; i++) {
if (flags & flag_values[i]) {
nprint("D", "%s flag is set.", flag_names[i]);
} else {
nprint("D", "%s flag is not set.", flag_names[i]);
}
}
for(int i = 0; i < pkgCount; i++) {
nprint("D", "Package %d: %s", i, packages[i]);