Skip to content

Commit

Permalink
Moved get_two_ids() from dev.c to utility.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola02nb committed Dec 10, 2024
1 parent 35483dc commit 3ddc516
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
39 changes: 0 additions & 39 deletions src/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,6 @@ int check_range(int number, int low, int high)
return 0;
}

/**
* @brief Accept an string input like 123:456 and splits them into two ids
*
* @param input input string
* @param id1 the first (left) id
* @param id2 the secound it
* @return int 0 if successfull, or 1 if not two ids provided
*/
int get_two_ids(char* input, int* id1, int* id2)
{
const char* delim = " :.,";

size_t sz = strlen(input);
char* str = (char*)malloc(sz + 1);
strcpy(str, input);

char* token = strtok(input, delim);
int i = 0;
while (token) {
char* endptr;
long int val = strtol(token, &endptr, 0);

if (i == 0)
*id1 = val;
else if (i == 1)
*id2 = val;

i++;
token = strtok(NULL, delim);
}

free(str);

if (i != 2) // not exactly supplied two ids
return 1;

return 0;
}

static void print_help()
{
printf("HeadsetControl Developer menu. Take caution.\n\n");
Expand Down
2 changes: 0 additions & 2 deletions src/dev.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

int get_two_ids(char* input, int* id1, int* id2);

int dev_main(int argc, char* argv[]);
31 changes: 31 additions & 0 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ int get_float_data_from_parameter(char* input, float* dest, size_t len)
return i;
}

int get_two_ids(char* input, int* id1, int* id2)
{
const char* delim = " :.,";

size_t sz = strlen(input);
char* str = (char*)malloc(sz + 1);
strcpy(str, input);

char* token = strtok(input, delim);
int i = 0;
while (token) {
char* endptr;
long int val = strtol(token, &endptr, 0);

if (i == 0)
*id1 = val;
else if (i == 1)
*id2 = val;

i++;
token = strtok(NULL, delim);
}

free(str);

if (i != 2) // not exactly supplied two ids
return 1;

return 0;
}

// ----------------- asprintf / vasprintf -----------------
/*
* Copyright (c) 2004 Darren Tucker.
Expand Down
10 changes: 10 additions & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ int get_byte_data_from_parameter(char* input, unsigned char* dest, size_t len);
*/
int get_float_data_from_parameter(char* input, float* dest, size_t len);

/**
* @brief Accept an string input like 123:456 and splits them into two ids
*
* @param input input string
* @param id1 the first (left) id
* @param id2 the secound it
* @return int 0 if successfull, or 1 if not two ids provided
*/
int get_two_ids(char* input, int* id1, int* id2);

int vasprintf(char** str, const char* fmt, va_list ap);

int _asprintf(char** str, const char* fmt, ...);

0 comments on commit 3ddc516

Please sign in to comment.