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