summaryrefslogtreecommitdiff
path: root/functions.c
blob: f381dc8d0e4ff7f1fa2e31fe4e376dc860259134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include "7ed.h"
#include <time.h>

void CONFIRM() {
    struct termios old,new;

    tcgetattr(fileno(stdin),&old); // gets something?
    tcgetattr(fileno(stdin),&new); // gets something else?
    cfmakeraw(&new); // makes new terminal settings
    tcsetattr(fileno(stdin),TCSANOW,&new); // sets those settings immediately (TCSANOW) to &new
    fputs("Press any key to continue...",stdout);
    fflush(NULL);
    fgetc(stdin); 
    tcsetattr(fileno(stdin),TCSANOW,&old); // goes back to old settings
    puts(""); // newline
}

int CHOICE() {
    int choice;

    do {

    fputs("[Y / N] ? ", stdout);

    choice = getchar();
    if (choice == '\n') { continue; }

    while ('\n' != getchar());

    } while ( (choice != 'Y') && (choice != 'y') && (choice != 'N') && (choice != 'n') );

    if ( (choice == 'Y') || (choice == 'y') ) 
    {
        return 0;
    }

    if ((choice == 'N') || (choice == 'n') )
    {
        return 1;
    }

    return EXIT_FAILURE;
}

int COUNT_LINES_IN_FILE (char filename[], size_t *lines) {

    // Does not follow posix because this function accounts for if the last line does not end with a newline.

    size_t line_count = 0; // Counter starting at 0
    FILE *file;
    file = fopen(filename,"rb"); // Open file

    if (file == NULL) { // Check if you can open file
        fprintf(stderr, "Cannot open file.\n");
        return 1;
    }
    fseek(file, -1, SEEK_END);
    int last = fgetc(file);
    if (last != '\n') {         // Checks if the file ends with a newline or not. If not then it adds 1 to the count to account for the last line
        line_count++;
    } 
    fseek(file, 0, SEEK_SET);

    char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4
    while (1) {
        size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes.
        for (size_t i = 0 ; i < bytes_read; i++) { 
            if (buffer[i] == '\n') { // Searches through the buffer for newlines.
                line_count++;
            } 
        }
        if (feof(file)) { // If end of file is encountered then break
            break; 
        }
    }
    fclose(file);
    *lines = line_count;
    return 0;
}

int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines) {

    // Same function as before but posix

    size_t line_count = 0; // Counter starting at 0
    FILE *file;
    file = fopen(filename,"rb"); // Open file

    if (file == NULL) { // Check if you can open file
        fprintf(stderr, "Cannot open file.\n");
        return 1;
    }

    char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4
    while (1) {
        size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes.
        for (size_t i = 0 ; i < bytes_read; i++) { 
            if (buffer[i] == '\n') { // Searches through the buffer for newlines.
                line_count++;
            } 
        }
        if (feof(file)) { // If end of file is encountered then break
            break; 
        }
    }
    fclose(file);
    *lines = line_count;
    return 0;
}

void shuffle(char arr[], int n) {
    for (int i = n -1 ; i > 0 ; i--) { 
        int j = rand() % (i + 1); 
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

int print_7ed() {

    printf("print_7ed()\n");

    return 0;

}