Skip to content

Commit

Permalink
clang-format: Align structures and add braces
Browse files Browse the repository at this point in the history
Makes code style more readable.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Aug 17, 2024
1 parent edbfb26 commit aeff45d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
17 changes: 13 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
---
Language: Cpp
BasedOnStyle: WebKit
ColumnLimit: 80

AlignAfterOpenBracket: Align
AlignEscapedNewlines: Left
AlignArrayOfStructures: Left
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
AlignTrailingComments: true

AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortLambdasOnASingleLine: Empty

ForEachMacros: [list_for_each]
SpaceBeforeParens: ControlStatementsExceptForEachMacros

BreakBeforeBinaryOperators: None
ColumnLimit: 80
IndentCaseLabels: true
InsertBraces: true

SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^".*\.h"'
Expand All @@ -21,5 +32,3 @@ IncludeCategories:
Priority: 3
- Regex: '.*'
Priority: 4
IndentCaseLabels: true
SortIncludes: true
8 changes: 4 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct config_context {
static struct config_context ctx;

static const struct location config_locations[] = {
{ "XDG_CONFIG_HOME", "/swayimg/config" },
{ "HOME", "/.config/swayimg/config" },
{ "XDG_CONFIG_DIRS", "/swayimg/config" },
{ NULL, "/etc/xdg/swayimg/config" }
{ "XDG_CONFIG_HOME", "/swayimg/config" },
{ "HOME", "/.config/swayimg/config" },
{ "XDG_CONFIG_DIRS", "/swayimg/config" },
{ NULL, "/etc/xdg/swayimg/config" }
};

/**
Expand Down
18 changes: 12 additions & 6 deletions src/formats/bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,24 @@ static inline size_t right_zeros(uint32_t val)
{
size_t count = sizeof(uint32_t) * BITS_PER_BYTE;
val &= -(int32_t)val;
if (val)
if (val) {
--count;
if (val & 0x0000ffff)
}
if (val & 0x0000ffff) {
count -= 16;
if (val & 0x00ff00ff)
}
if (val & 0x00ff00ff) {
count -= 8;
if (val & 0x0f0f0f0f)
}
if (val & 0x0f0f0f0f) {
count -= 4;
if (val & 0x33333333)
}
if (val & 0x33333333) {
count -= 2;
if (val & 0x55555555)
}
if (val & 0x55555555) {
count -= 1;
}
return count;
}

Expand Down
63 changes: 42 additions & 21 deletions src/formats/pnm.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,36 @@ struct pnm_iter {
*/
static int pnm_readint(struct pnm_iter* it, size_t digits)
{
if (!digits)
if (!digits) {
digits = INT_MAX_DIGITS;
}
for (; it->pos != it->end; ++it->pos) {
const char c = *it->pos;
if (c == '#') {
while (it->pos != it->end && *it->pos != '\n' && *it->pos != '\r')
while (it->pos != it->end && *it->pos != '\n' && *it->pos != '\r') {
++it->pos;
}
} else if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
break;
}
}
if (it->pos == it->end)
if (it->pos == it->end) {
return PNM_EEOF;
if (*it->pos < '0' || *it->pos > '9')
}
if (*it->pos < '0' || *it->pos > '9') {
return PNM_EFMT;
}
int val = 0;
size_t i = 0;
do {
const uint8_t d = *it->pos - '0';
if (val > INT_MAX / 10)
if (val > INT_MAX / 10) {
return PNM_ERNG;
}
val *= 10;
if (val > INT_MAX - d)
if (val > INT_MAX - d) {
return PNM_ERNG;
}
val += d;
++it->pos;
++i;
Expand All @@ -94,36 +100,47 @@ static int decode_plain(struct pixmap* pm, struct pnm_iter* it,
argb_t pix = ARGB_SET_A(0xff);
if (type == pnm_pbm) {
const int bit = pnm_readint(it, 1);
if (bit < 0)
if (bit < 0) {
return bit;
if (bit > maxval)
}
if (bit > maxval) {
return PNM_EOVF;
}
pix |= bit - 1;
} else if (type == pnm_pgm) {
int v = pnm_readint(it, 0);
if (v < 0)
if (v < 0) {
return v;
if (v > maxval)
}
if (v > maxval) {
return PNM_EOVF;
if (maxval != UINT8_MAX)
}
if (maxval != UINT8_MAX) {
v = div_near(v * UINT8_MAX, maxval);
}
pix |= ARGB_SET_R(v) | ARGB_SET_G(v) | ARGB_SET_B(v);
} else {
int r = pnm_readint(it, 0);
if (r < 0)
if (r < 0) {
return r;
if (r > maxval)
}
if (r > maxval) {
return PNM_EOVF;
}
int g = pnm_readint(it, 0);
if (g < 0)
if (g < 0) {
return g;
if (g > maxval)
}
if (g > maxval) {
return PNM_EOVF;
}
int b = pnm_readint(it, 0);
if (b < 0)
if (b < 0) {
return b;
if (b > maxval)
}
if (b > maxval) {
return PNM_EOVF;
}
if (maxval != UINT8_MAX) {
r = div_near(r * UINT8_MAX, maxval);
g = div_near(g * UINT8_MAX, maxval);
Expand Down Expand Up @@ -155,8 +172,9 @@ static int decode_raw(struct pixmap* pm, struct pnm_iter* it,
size_t rowsz = type == pnm_pbm
? div_ceil(pm->width, 8)
: pm->width * bpc * (type == pnm_pgm ? 1 : 3);
if (it->end < it->pos + pm->height * rowsz)
if (it->end < it->pos + pm->height * rowsz) {
return PNM_EEOF;
}
for (size_t y = 0; y < pm->height; ++y) {
argb_t* dst = pm->data + y * pm->width;
const uint8_t* src = it->pos + y * rowsz;
Expand All @@ -167,10 +185,12 @@ static int decode_raw(struct pixmap* pm, struct pnm_iter* it,
pix |= bit - 1;
} else if (type == pnm_pgm) {
int v = bpc == 1 ? src[x] : src[x] << 8 | src[x + 1];
if (v > maxval)
if (v > maxval) {
return PNM_EOVF;
if (maxval != UINT8_MAX)
}
if (maxval != UINT8_MAX) {
v = div_near(v * UINT8_MAX, maxval);
}
pix |= ARGB_SET_R(v) | ARGB_SET_G(v) | ARGB_SET_B(v);
} else {
int r, g, b;
Expand All @@ -183,8 +203,9 @@ static int decode_raw(struct pixmap* pm, struct pnm_iter* it,
g = src[x * 3 + 2] << 8 | src[x * 3 + 3];
b = src[x * 3 + 4] << 8 | src[x * 3 + 5];
}
if (r > maxval || g > maxval || b > maxval)
if (r > maxval || g > maxval || b > maxval) {
return PNM_EOVF;
}
if (maxval != UINT8_MAX) {
r = div_near(r * UINT8_MAX, maxval);
g = div_near(g * UINT8_MAX, maxval);
Expand Down
6 changes: 2 additions & 4 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <sys/timerfd.h>
#include <unistd.h>

// clang-format off

/** Display modes. */
enum info_mode {
mode_viewer,
Expand All @@ -35,6 +33,7 @@ static const char* mode_names[] = {
};
#define MODES_NUM 2

// clang-format off
/** Field names. */
static const char* field_names[] = {
[info_file_name] = "name",
Expand All @@ -49,6 +48,7 @@ static const char* field_names[] = {
[info_status] = "status",
};
#define INFO_FIELDS_NUM ARRAY_SIZE(field_names)
// clang-format on

/** Positions of text info block. */
enum block_position {
Expand Down Expand Up @@ -100,8 +100,6 @@ static const struct field_scheme default_gallery_br[] = {
{ .type = info_status, .title = false },
};

// clang-format on

// Space between text layout and window edge
#define TEXT_PADDING 10

Expand Down
6 changes: 3 additions & 3 deletions src/keybind.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ struct virtual_keys {
const char* name;
};
static const struct virtual_keys virtual_keys[] = {
{ VKEY_SCROLL_UP, "ScrollUp" },
{ VKEY_SCROLL_DOWN, "ScrollDown" },
{ VKEY_SCROLL_LEFT, "ScrollLeft" },
{ VKEY_SCROLL_UP, "ScrollUp" },
{ VKEY_SCROLL_DOWN, "ScrollDown" },
{ VKEY_SCROLL_LEFT, "ScrollLeft" },
{ VKEY_SCROLL_RIGHT, "ScrollRight" },
};

Expand Down

0 comments on commit aeff45d

Please sign in to comment.