config: Adoption to the project

This commit is contained in:
Michael Schupikov 2017-01-09 18:45:00 +01:00
parent e039c98fc4
commit e38d4b3a06
3 changed files with 30 additions and 63 deletions

View file

@ -7,33 +7,16 @@
#include "readline.h"
#include "stringop.h"
/***************************************************************************//**
*
* \brief Read one line of the file. Lines beginning with '#' are skipped.
* Whitespace is stripped.
*
* \param[in] file Pointer to the already open configuration file. If NULL is
* passed, no operation is performed.
*
* \return String containing one single line from the file.
*
******************************************************************************/
inline char* read_line(FILE* restrict const file) {
char* line = NULL;
if (file) {
size_t line_length = 0u;
const ssize_t bytes_read = getline(&line, &line_length, file);
if (line) {
assert(line_length);
if (line && line_length) {
if (0 > bytes_read) {
free(line);
line = NULL;
} else {
strip_whitespace(line);
if (line[0] == '#') {
free(line);
line = NULL;
}
}
}
}

View file

@ -20,20 +20,19 @@ const char whitespace[] = " \f\n\r\t\v";
* is modified. If NULL is passed, no operation is performed.
*
******************************************************************************/
void strip_whitespace(char * restrict const str)
{
if (NULL != str) {
unsigned int count = 0u;
for (unsigned int index = 0u; str[index]; ++index) {
void strip_whitespace(char * restrict const str) {
if (str) {
size_t count = 0;
for (size_t index = 0; str[index]; ++index) {
if (' ' != str[index] && '\t' != str[index]) {
str[count++] = str[index];
} else if(0u < count && ' ' != str[count-1u] && '\t' != str[count-1u]) {
} else if(0 < count && ' ' != str[count - 1] && '\t' != str[count - 1]) {
str[count++] = ' ';
}
}
str[count] = '\0';
if (0u < count && (' ' == str[count-1u] || '\t' == str[count-1u])) {
str[count-1u] = '\0';
if (0 < count && (' ' == str[count - 1] || '\t' == str[count - 1])) {
str[count - 1] = '\0';
}
}
}
@ -298,17 +297,16 @@ static bool has_whitespace(const char *str) {
* Returns true if the string contains only whitespace and false otherwise.
*/
bool is_empty(const char *str) {
bool ret = true;
if (str) {
while (*str != '\0') {
if (!isspace(*(const unsigned char*)str)) {
ret = false;
break;
}
str++;
}
if(!str) {
return true;
}
return ret;
while (*str != '\0') {
if (!isspace(*(unsigned char*)str)) {
return false;
}
str++;
}
return true;
}
/**

View file

@ -621,19 +621,7 @@ bool load_include_configs(const char *path, struct sway_config *config) {
return true;
}
/***************************************************************************//**
*
* \brief This function corrects the given 'line' if the opening parentheses
* happens to be on the next line.
*
* \param[in] file The pointer to the already open file.
* \param[in/out] line The current line read from the configuration file.
* \param[in] line_number Counter for the line number. Optional.
*
* \return A string to the corrected 'line' is returned.
*
*******************************************************************************/
static inline char* handle_parentheses(FILE* const file, char* line,
static inline char* handle_braces(FILE* const file, char* line,
unsigned int* const line_number) {
if (file && line) {
const size_t line_length = strlen(line);
@ -645,6 +633,11 @@ static inline char* handle_parentheses(FILE* const file, char* line,
/* Read the file. Skip empty lines. Comments are skipped in read_line()
* already. */
next_line = read_line(file);
strip_whitespace(next_line);
if (next_line[0] == '#') {
free(next_line);
next_line = NULL;
}
if (line_number) {
++(*line_number);
}
@ -691,23 +684,16 @@ static inline char* handle_parentheses(FILE* const file, char* line,
return line;
}
/***************************************************************************//**
*
* \brief Read one line from the configuration file.
*
* \param[in] file The pointer to the already open file.
* \param[in] block Indication if we are in some special block currently.
* \param[out] line_number The counter for the line number. Optional.
*
* \return String that contains the whitespace-stripped line from the
* configuration file.
*
*******************************************************************************/
static inline char* get_config_line(FILE* const file, const enum cmd_status block,
unsigned int* const line_number) {
char* line = NULL;
if (file) {
line = read_line(file);
strip_whitespace(line);
if (line[0] == '#') {
free(line);
line = NULL;
}
if (line_number) {
++(*line_number);
}
@ -717,13 +703,13 @@ static inline char* get_config_line(FILE* const file, const enum cmd_status bloc
0 == strncmp(line, "bar", 3u)) {
/* If we are in no block, we check the opening parentheses for modes
* and the bar configuration. */
line = handle_parentheses(file, line, line_number);
line = handle_braces(file, line, line_number);
}
}else if (block == CMD_BLOCK_BAR) {
if (0 == strncmp(line, "colors", 5u)) {
/* If we are in the 'bar' block, we check the opening parentheses for
* the 'colors' block. */
line = handle_parentheses(file, line, line_number);
line = handle_braces(file, line, line_number);
}
}
}