Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- * *
- * Working with utf8 only file encoding. *
- * This program reads file, and prints same file without words, *
- * that not consist of numbers and numbers ending with "-й" *
- * *
- *******************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #define BUF_SIZE 128
- void process_output_buf(unsigned char* output_buf, int buf_size);
- void clear_output_buf(unsigned char* output_buf, int buf_size);
- unsigned char* realloc_output_buf(unsigned char* output_buf, int prev_size, int new_size);
- int main(int argc, char *argv[]) {
- FILE *fp = NULL;
- unsigned char* output_buf = NULL;
- unsigned char file_buf[BUF_SIZE] = { 0 };
- int ob_i = 0;
- int ob_size = BUF_SIZE;
- output_buf = calloc(BUF_SIZE, sizeof(unsigned char));
- if(output_buf == NULL) {
- printf("Couldn't allocate memory.\n");
- exit(4);
- }
- if(argc != 2) {
- printf("Incorrect number of arguments.\n");
- exit(1);
- }
- if((fp = fopen(argv[1], "rb") ) == NULL) {
- printf("Cannot open file.\n");
- exit(2);
- }
- while(1) {
- if(fgets(file_buf, BUF_SIZE, fp)) {
- for(int i = 0; i < BUF_SIZE, file_buf[i] != '\0'; i++) {
- if(file_buf[i] == 32 || file_buf[i] == 10) {
- process_output_buf(output_buf, ob_size);
- clear_output_buf(output_buf, ob_size);
- ob_i = 0;
- printf("%c", file_buf[i]);
- } else {
- output_buf[ob_i] = file_buf[i];
- ob_i++;
- if(ob_i >= ob_size) {
- output_buf = realloc_output_buf(output_buf, ob_size, ob_size + BUF_SIZE);
- ob_size += BUF_SIZE;
- }
- }
- }
- } else {
- if(ferror(fp)) {
- printf("Error while reading file.\n");
- exit(3);
- }
- break;
- }
- }
- process_output_buf(output_buf, ob_size);
- fclose(fp);
- free(output_buf);
- return 0;
- }
- void process_output_buf(unsigned char* output_buf, int buf_size) {
- if(buf_size < 1) return;
- if(48 <= output_buf[0] && output_buf[0] < 58) {
- for(int i = 1; i < buf_size, output_buf[i] != 0; i++) {
- if(48 <= output_buf[i] && output_buf[i] < 58) {
- continue;
- } else if(output_buf[i] == 45) { // -й
- if((i + 2) < buf_size) {
- if((output_buf[i + 1] == 208) && (output_buf[i + 2] == 185)) {
- printf("%s", output_buf);
- }
- }
- }
- return;
- }
- printf("%s", output_buf);
- }
- }
- void clear_output_buf(unsigned char* output_buf, int buf_size) {
- for(int i = 0; i < buf_size; i++) {
- output_buf[i] = 0;
- }
- }
- unsigned char* realloc_output_buf(unsigned char* output_buf, int prev_size, int new_size) {
- output_buf = realloc(output_buf, new_size * sizeof(unsigned char));
- if(output_buf == NULL) {
- printf("Couldn't allocate memory.\n");
- exit(4);
- }
- for(int i = prev_size; i < new_size; i++) {
- output_buf[i] = 0;
- }
- return output_buf;
- }
Add Comment
Please, Sign In to add comment