Optimised and corrected updatestatus function

This commit is contained in:
Ashish Kumar Yadav 2020-07-29 22:36:15 +05:30
parent 74621edcab
commit 2d876bcd55

View file

@ -13,8 +13,7 @@
#define LOCKFILE "/tmp/dwmblocks.pid" #define LOCKFILE "/tmp/dwmblocks.pid"
#define NILL INT_MIN #define NILL INT_MIN
#define EMPTYCMDOUT(block) (block->cmdoutcur[0] == '\n' || block->cmdoutcur[0] == '\0') #define EMPTYCMDOUT(block) (*(block->cmdoutcur) == '\n' || *(block->cmdoutcur) == '\0')
#define NOTATCMDOUTEND(block, i) (i < CMDLENGTH && block->cmdoutcur[i] != '\n' && block->cmdoutcur[i] != '\0')
typedef struct { typedef struct {
char *pathu; char *pathu;
@ -209,60 +208,72 @@ termhandler(int signum)
int int
updatestatus() updatestatus()
{ {
int i; char *s = statusstr;
char *str = statusstr; char *c, *p; /* for cmdoutcur and cmdoutprv */
Block *current; const char *d; /* for delimiter */
Block *current = blocks;
for (current = blocks; current->pathu; current++) { /* checking half of the function */
if (EMPTYCMDOUT(current)) { /* skip empty blocks */
if (current->cmdoutprv[0] != current->cmdoutcur[0]) { while (current->pathu && EMPTYCMDOUT(current)) {
current->cmdoutprv[0] = current->cmdoutcur[0]; if (*(current->cmdoutprv) != *(current->cmdoutcur)) {
current++; *(current->cmdoutprv) = *(current->cmdoutcur);
goto update0; current++;
} goto update0;
continue;
} }
i = 0; current++;
}
/* skip delimiter in front of the first non-empty block */
if (current->pathu)
goto skipdelimc;
else
return 0;
/* main loop */
for (; current->pathu; current++) {
s += delimlength;
skipdelimc:
c = current->cmdoutcur; p = current->cmdoutprv;
do { do {
if (current->cmdoutcur[i] == current->cmdoutprv[i]) { if (*c != *p) {
i++; s += (c - current->cmdoutcur);
continue;
} else {
str += i;
goto update1; goto update1;
} }
} while (NOTATCMDOUTEND(current, i)); c++; p++;
str += i; } while (*c != '\n' && *c != '\0');
s += (c - current->cmdoutcur);
if (current->pathc && current->signal) if (current->pathc && current->signal)
str++; s++;
if (*str == '\0')
goto update2;
str += delimlength;
} }
return 0; return 0;
update0: update0:
/* updating half of the function */
/* skip empty blocks */
while (current->pathu && EMPTYCMDOUT(current)) {
*(current->cmdoutprv) = *(current->cmdoutcur);
current++;
}
/* skip delimiter in front of the first non-empty block */
if (current->pathu)
goto skipdelimu;
else
return 1;
/* main loop */
for (; current->pathu; current++) { for (; current->pathu; current++) {
if (EMPTYCMDOUT(current)) { d = delim;
current->cmdoutprv[0] = current->cmdoutcur[0]; while (*d)
continue; *(s++) = *(d++);
} *(s++) = '\n'; /* to mark the end of delimiter */
i = 0; skipdelimu:
c = current->cmdoutcur; p = current->cmdoutprv;
update1: update1:
do { do {
*(str++) = current->cmdoutcur[i]; *(s++) = *c;
current->cmdoutprv[i] = current->cmdoutcur[i]; *p = *c;
i++; p++; c++;
} while (NOTATCMDOUTEND(current, i)); } while (*c != '\n' && *c != '\0');
if (current->pathc && current->signal) if (current->pathc && current->signal)
*(str++) = current->signal; *(s++) = current->signal;
update2:
for (i = 0; delim[i]; i++)
*(str++) = delim[i];
*(str++) = '\n';
} }
/* remove delimiter at the end if not all blocks are empty */
if (str != statusstr)
*(str - delimlength) = '\0';
return 1; return 1;
} }