Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Strcasecomp #439

Closed
wants to merge 10 commits into from
18 changes: 3 additions & 15 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,27 @@ enum stumpless_facility
stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) {
size_t facility_bound;
size_t i;
char *facility_name;
const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_"
size_t buf_length;

facility_bound = sizeof( facility_enum_to_string ) /
sizeof( facility_enum_to_string[0] );

facility_name = copy_cstring_with_length(facility_buffer, &buf_length);
if( !facility_name ) {
return -1;
}

to_upper_case(facility_name);
for( i = 0; i < facility_bound; i++ ) {
if( strcmp( facility_name, facility_enum_to_string[i] + str_offset ) == 0 ) {
free_mem( facility_name );
if( strncasecmp( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) {
return i << 3;
}
}

// exeption, for 'security' return 'auth' enum value
if( strcmp( facility_name, "SECURITY" ) == 0 ) {
free_mem( facility_name );
if( strncasecmp( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) {
return STUMPLESS_FACILITY_AUTH_VALUE;
}

// exeption, for 'authpriv' not presented in enum list
if( strcmp( facility_name, "AUTHPRIV" ) == 0 ) {
free_mem( facility_name );
if( strncasecmp( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) {
return STUMPLESS_FACILITY_AUTH2_VALUE;
}

free_mem( facility_name );
return -1;
}

Expand Down
25 changes: 4 additions & 21 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
#include <stumpless/prival.h>
#include <stumpless/severity.h>
#include <stumpless/facility.h>
#include <stumpless/error.h>
#include "private/config.h"
#include "private/config/wrapper/locale.h"
#include "private/error.h"
#include "private/facility.h"
#include "private/memory.h"
#include "private/prival.h"
#include "private/severity.h"
#include "private/strhelper.h"
#include "private/validate.h"

const char *
Expand Down Expand Up @@ -64,7 +64,6 @@ stumpless_prival_from_string( const char *string ) {
int prival;
int severity;
int facility;
const char *param;
const char *period;
const char *sec_period;
size_t len;
Expand Down Expand Up @@ -103,15 +102,7 @@ stumpless_prival_from_string( const char *string ) {
// Calculate the facility length, up to the first period character
len = period - string;

// Copy the facility substring to the param buffer
param = copy_cstring_length( string, len );
if( !param ) {
return -1;
}

facility = stumpless_get_facility_enum( param );

free_mem( param );
facility = stumpless_get_facility_enum_from_buffer( string, len );

if( facility < 0 ) {
raise_invalid_param( );
Expand All @@ -122,17 +113,9 @@ stumpless_prival_from_string( const char *string ) {
len++;
len = slen - len;

// Copy the severity substring to the param buffer
param = copy_cstring_length( period + 1, len );
if( !param ) {
return -1;
}

severity = stumpless_get_severity_enum( param );

free_mem( param );
severity = stumpless_get_severity_enum_from_buffer( period + 1, len );

if( severity < 0 ) {
if( severity < 0 && !stumpless_has_error()) {
raise_invalid_param( );
return -1;
}
Expand Down
21 changes: 4 additions & 17 deletions src/severity.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,29 @@ enum stumpless_severity stumpless_get_severity_enum(const char *severity_string)
enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *severity_buffer, size_t severity_buffer_length) {
size_t severity_bound;
size_t i;
char *severity_name;
const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_"
size_t buf_length;

severity_bound = sizeof( severity_enum_to_string ) /
sizeof( severity_enum_to_string[0] );

severity_name = copy_cstring_with_length( severity_buffer, &buf_length );
if( !severity_name ) {
return -1;
}

to_upper_case( severity_name );
for( i = 0; i < severity_bound; i++ ) {
if( strcmp( severity_name, severity_enum_to_string[i] + str_offset ) == 0 ) {
free_mem( severity_name );
if( strncasecmp( severity_buffer, severity_enum_to_string[i] + str_offset, severity_buffer_length ) == 0 ) {
return i;
}
}

if( strcmp( severity_name, "PANIC" ) == 0 ) {
free_mem( severity_name );
if( strncasecmp( severity_buffer, "PANIC", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_EMERG_VALUE;
}

if( strcmp( severity_name, "ERROR" ) == 0 ) {
free_mem( severity_name );
if( strncasecmp( severity_buffer, "ERROR", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_ERR_VALUE;
}

if( strcmp( severity_name, "WARN" ) == 0 ) {
free_mem( severity_name );
if( strncasecmp( severity_buffer, "WARN", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_WARNING_VALUE;
}

free_mem( severity_name );
return -1;
}

Expand Down
29 changes: 0 additions & 29 deletions test/function/prival.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"

namespace {

Expand Down Expand Up @@ -112,34 +111,6 @@ namespace {
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING );
}

TEST( GetPrivalFromString, InvalidMemFacilityPriority ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_prival_from_string( "user.err" );
EXPECT_EQ( result, -1 );

set_malloc_result = stumpless_set_malloc( malloc );
EXPECT_TRUE( set_malloc_result == malloc );
}

TEST( GetPrivalFromString, InvalidMemSeverityPriority ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL_ON_SIZE( 4 ) );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_prival_from_string( "syslog.err" );
EXPECT_EQ( result, -1 );

EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE );

set_malloc_result = stumpless_set_malloc( malloc );
EXPECT_TRUE( set_malloc_result == malloc );
}

TEST(GetPrivalString, ValidPrival) {
int prival;
const char *result;
Expand Down