Skip to content

Commit

Permalink
string operations are job safe
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 30, 2024
1 parent 89473bd commit 16dffb6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
36 changes: 18 additions & 18 deletions src/ccode/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

#define strdup(A) strcpy(calloc(strlen(A) + 1, sizeof(char)), A)

static size_t i;
static size_t start, skip, removed;
static char* tmp;
static char* tmp2;

static int string_compare(const void* a, const void* b){
return strcmp(*(const char**)a, *(const char**)b);
}
Expand All @@ -26,6 +21,7 @@ array *array_new() {
arr->size = 0;
arr->capacity = 1024;
arr->removed = 0;
size_t start;
for(start=0;start<arr->capacity;start++){
arr->data[start] = NULL;
}
Expand All @@ -43,6 +39,7 @@ void array_add(array *arr, char *value) {
if (arr->size >= arr->capacity-1) {
arr->capacity += 1024;
arr->data = (char **)realloc(arr->data, arr->capacity * sizeof(char *));
size_t start;
for(start=arr->capacity-1024;start<arr->capacity;start++){
arr->data[start] = NULL;
}
Expand All @@ -55,6 +52,7 @@ void array_set(array *arr, char** new_data, size_t len){
arr->data = calloc(arr->capacity, sizeof(char*));
arr->size = 0;
arr->removed = 0;
size_t start;
for(start=0;start<arr->capacity;start++){
arr->data[start] = NULL;
}
Expand All @@ -63,7 +61,7 @@ void array_set(array *arr, char** new_data, size_t len){

char* array_get_string(array *arr){
size_t tot_len = 0;
start = 0;
size_t start = 0;
while(start < arr->capacity){
if(arr->data[start] != NULL){
tot_len += strlen(arr->data[start]);
Expand All @@ -82,13 +80,14 @@ char* array_get_string(array *arr){
}

void array_adds(array *arr, char **value, size_t len) {
size_t i;
for(i=0;i<len;i++){
array_add(arr, value[i]);
}
}

void array_remove(array* arr, char* item){
start = 0;
size_t start = 0;
while(start < arr->capacity){
if(arr->data[start] != NULL && strcmp(arr->data[start],item)==0){
arr->data[start] = NULL;
Expand All @@ -100,7 +99,7 @@ void array_remove(array* arr, char* item){
}

bool array_has(array* arr, char* name){
start = 0;
size_t start = 0;
while(start < arr->size + arr->removed){
if(arr->data[start]){
if (strcmp(arr->data[start], name) == 0){
Expand All @@ -113,9 +112,9 @@ bool array_has(array* arr, char* name){
}

void array_uniq(array* arr){
start = 1;
i = 0;
removed = 0;
size_t start = 1;
size_t i = 0;
size_t removed = 0;
while(start < arr->capacity){
if(arr->data[start] == NULL){
start++;
Expand Down Expand Up @@ -149,7 +148,8 @@ void array_insert(array* arr, char* value, size_t index){
arr->size++;
return;
}
tmp = strdup(arr->data[index]);
char* tmp = strdup(arr->data[index]);
char* tmp2;
arr->data[index] = strdup(value);
size_t start = index+1;
while(start < arr->capacity){
Expand All @@ -168,9 +168,9 @@ void array_insert(array* arr, char* value, size_t index){

void array_sort(array* arr){
char** new_data = (char**)calloc(arr->capacity,sizeof(char*));
start = 0;
skip = 0;
while(start < arr->size+arr->removed+1){
size_t start = 0;
size_t skip = 0;
while(start < arr->size+arr->removed+1){
if(arr->data[start] == NULL){
start++;
skip++;
Expand All @@ -186,8 +186,8 @@ void array_sort(array* arr){
char **array_get(array *arr, int* len) {
*len = arr->size;
char** ret = calloc(arr->size+1, sizeof(char*));
start = 0;
skip = 0;
size_t start = 0;
size_t skip = 0;
while(start < arr->size+arr->removed+1 && start < arr->capacity){
if(arr->data[start] == NULL){
start++;
Expand All @@ -210,7 +210,7 @@ void array_reverse(array *arr) {
return; /* No need to reverse if size is 0 or 1 */
}

start = 0;
size_t start = 0;
size_t tot = arr->size + arr->removed;
while (start < tot/2) {
/* Swap elements at start and end indices */
Expand Down
9 changes: 7 additions & 2 deletions src/ccode/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <pthread.h>
#include <string.h>
#include <stdarg.h>

#include <sys/sysinfo.h>
#include <jobs.h>
#include <logger.h>

Expand All @@ -13,6 +13,8 @@ typedef struct _worker_job {
int id;
} worker_job;

extern char* get_value(char* name);

static void* worker_thread(void* arg) {
worker_job* jb= (worker_job*)arg;
jobs *j = jb->j;
Expand Down Expand Up @@ -67,7 +69,10 @@ jobs* jobs_new() {
j->current = 0;
j->finished = 0;
j->total = 0;
j->parallel = JOB_PARALLEL;
j->parallel = atoi(get_value("jobs"));
if(j->parallel < 1){
j->parallel = get_nprocs_conf();
}
j->jobs = (job*)malloc(j->max * sizeof(job));
pthread_cond_init(&j->cond, NULL);
fdebug("New jobs");
Expand Down
40 changes: 18 additions & 22 deletions src/ccode/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
#include <stddef.h>
#include <string.h>

/*generic counters*/
long i;
long j;
long cnt = 0;
long len = 0;

int iseq(char* str1, char* str2){
return strcmp(str1,str2) == 0;
}
Expand All @@ -28,7 +22,7 @@ size_t sstrlen(const char* str){


long count_tab(char* data){
cnt = 0;
int cnt = 0;
while (*data == ' ') {
cnt++;
data++;
Expand All @@ -37,8 +31,8 @@ long count_tab(char* data){
}

char* join(char* f, char** array){
i = 0;
len = 0;
int i = 0;
int len = 0;
/* find output size */
while(array[i]){
len += sstrlen(array[i]) + sstrlen(f);
Expand All @@ -48,7 +42,7 @@ char* join(char* f, char** array){
char* ret = calloc(len+1, sizeof(char));
strcpy(ret,"");
/* copy item len and reset value */
cnt = i;
int cnt = i;
i = 0;
/* copy items */
while(array[i]){
Expand All @@ -69,10 +63,10 @@ char* str_add(char* str1, char* str2){
}

char* trim(char* data) {
i=0;
j=0;
cnt=0;
len = sstrlen(data);
int i=0;
int j=0;
int cnt=0;
int len = sstrlen(data);
char* str = calloc(len+1, sizeof(char));
strcpy(str,data);
cnt = count_tab (data);
Expand Down Expand Up @@ -113,7 +107,8 @@ char* int_to_string(int num){

/* Function to perform URL decoding */
char* url_decode(const char *input) {
cnt = 0;
int cnt = 0;
int i;
for (i = 0; input[i] != '\0'; i++) {
if (input[i] == '%'){
if (isHexDigit(input[i + 1])){
Expand All @@ -136,7 +131,7 @@ char* url_decode(const char *input) {
return (char*) input;
}

j = 0;
int j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (input[i] == '%') {
if (isHexDigit(input[i + 1])){
Expand All @@ -159,7 +154,8 @@ char* url_decode(const char *input) {

/* Function to perform URL encoding */
char* url_encode(const char *input) {
cnt = 0;
int cnt = 0;
int i;
for (i = 0; input[i] != '\0'; i++) {
if (!isalnum_c(input[i])) {
/* Two characters for % and the hexadecimal digit */
Expand All @@ -177,7 +173,7 @@ char* url_encode(const char *input) {
return (char*) input;
}

j = 0;
int j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (isalnum_c(input[i])) {
output[j++] = input[i];
Expand All @@ -195,8 +191,8 @@ char* url_encode(const char *input) {


int endswith(const char* data, const char* f) {
i = sstrlen(data);
j = sstrlen(f);
int i = sstrlen(data);
int j = sstrlen(f);

if (i < j) {
return 0;
Expand All @@ -206,8 +202,8 @@ int endswith(const char* data, const char* f) {
}

int startswith(const char* data, const char* f) {
i = sstrlen(data);
j = sstrlen(f);
int i = sstrlen(data);
int j = sstrlen(f);

if (i < j) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/include/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct _jobs {
} jobs;

#define MAX_JOB 1024*1024
#define JOB_PARALLEL 8

void jobs_unref(jobs *j);
void jobs_add(jobs* j, void (*callback)(void*,...), void* ctx, void* args, ...);
void jobs_run(jobs* j);
Expand Down

0 comments on commit 16dffb6

Please sign in to comment.