/* ---------- */ /* naji_str.c */ /* ---------- */ /* naji string functions */ /* this .c file is a part */ /* of libnaji version 0.6.1 */ /* libnaji is based on */ /* the original najitool */ /* both najitool and libnaji */ /* are public domain and are */ /* made by the same author */ /* please read license.txt */ /* made by NECDET COKYAZICI */ /* chstr */ /* chstr_line */ /* skipstr */ /* skipstr_line */ /* touppersn */ /* touppers */ /* str_move_left */ /* made by YEHRCL */ /* addtolinebuf, isequal */ /* made by SACHIN MANE */ #include "libnaji.h" int swrdcoun(const char *string) { int i=0; int x=1; int c=0; for (i=0; string[i] != '\0'; ++i) if ( (string[i] > 32) && (string[i] < 127) ) { if (x) { ++c; x=0; } } else x=1; return c; } void sreverse(char *str) { int i; int len=strlen(str); char *strbackup=newchar(len); exitnull(strbackup); strcpy(strbackup, str); for (i=0; i strlen(str)) { /* need more memory */ oldsize += (strlen(newstr) - strlen(str)) * sizeof(char) +1; aux = (char*) realloc(aux, oldsize); } for (j=0; newstr[j] != '\0'; j++) aux[x++] = newstr[j]; } } aux[x] = '\0'; return aux; } void chstr(char *namein, char *nameout, char *str, char *newstr) { long pos; int i; int c; char *aux; char *line; najin(namein); najout(nameout); pos = ftell(naji_input); c = fgetc(naji_input); if (c == '\n') fprintf(naji_output, "\n"); /* line by line */ while (c != EOF) { for (i=0; c != EOF && c != '\n' && c != '\0'; i++) c = fgetc(naji_input); if (i > 0) { line = (char*) malloc(sizeof(char)*i+1); /* go back and save the chars now */ fseek(naji_input, pos, SEEK_SET); fgets(line, i+1, naji_input); aux = chstr_line(line, str, newstr); fprintf(naji_output, "%s", aux); free(aux); free(line); } c = fgetc(naji_input); if (c =='\n') fprintf(naji_output, "\n"); ungetc(c, naji_input); pos = ftell(naji_input); c = fgetc(naji_input); } najinclose(); najoutclose(); } /* returns a line without "str" naji_input it */ char * skipstr_line(char *line, char *str) { int i; int j; int x; int n; char * aux; n = strlen(line); aux = (char*) malloc(sizeof(char)*n+1); x = 0; for (i=0; i 0) { line = (char*) malloc(sizeof(char)*i+1); /* go back and save the chars now */ fseek(naji_input, pos, SEEK_SET); fgets(line, i+1, naji_input); aux = skipstr_line(line, str); fprintf(naji_output, "%s", aux); free(aux); free(line); } c = fgetc(naji_input); if (c == '\n') fprintf(naji_output, "\n"); ungetc(c, naji_input); pos = ftell(naji_input); c = fgetc(naji_input); } najinclose(); najoutclose(); } /* This function adds a character 'c' to a buffer line_buf in the end. Before adding a character it checks for the length of the buffer and if required allocates more memory for the buffer. This functions returns the new buffer pointer to the calling fuction. */ char *addtolinebuf(char c, char *line_buf, int cur_pos, int *cur_size, int block_size) { char *swap_buf = NULL; if (cur_pos == *cur_size -1) { /* Buffer exceeded its limits. Reallocate */ /* Save the current content of the buffer in the swap buffer first */ swap_buf = (char *) calloc (*cur_size, sizeof (char)); if (swap_buf == NULL) { fprintf(stderr, "\n\nError, cannot allocate memory"); perror(" "); fprintf(stderr, "\n\n"); return NULL; } memcpy(swap_buf, line_buf, *cur_size * sizeof (char)); /* Free the existing allocated memory for the line buffer */ free(line_buf); /* Allocate fresh memory and increase it by BLOCK_SIZE this time */ line_buf = (char *) calloc ((*cur_size + block_size), sizeof (char)); if (line_buf == NULL) { fprintf(stderr, "\n\nError, cannot allocate memory"); perror(" "); fprintf(stderr, "\n\n"); return NULL; } /* Copy old buffer from swap buffer to the line buffer */ memcpy(line_buf, swap_buf, *cur_size * sizeof(char)); /* Free the swap buffer */ free(swap_buf); /* Set the current size. */ *cur_size += block_size; } /* Add the new character at the end of the buffer */ line_buf[cur_pos] = c; /* printf("\n added [%c] at [%d]", line_buf[cur_pos], cur_pos); */ return line_buf; } int isequal(char *str, char *tempbuf, int len, int start_pos) { int i; for (i=start_pos; i < (start_pos + len); i++) { if (str[i % len] != tempbuf[i % len]) break; } if (i == (start_pos + len)) return 1; return 0; }