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
// This is an ILLUSTRATION (not tested code) from office hours, discussing HW10.
//
// This is not a “solution”.   These are just very generic helper functions.
//
// You don't have to do it this way.  There are many other, perfectly reasonable
// ways to think about HW10.
//
// OK to copy/adapt anything in this file

typedef unsigned char uchar;

//static const size_t BLOCK_SIZE = 1024;
static const size_t BLOCK_SIZE = 32;  // maybe good for test

uchar* _get_last_line(const uchar* buf, size_t len_chars) {  
    // Given a buffer (already in memory), return a newly malloc'd string containing
    // the last line (terminated by '\n' or end of buffer) in buf.
    //
    // - buf is a string (i.e., a section of the file)
    // - len_chars is the length of the buffer.  You could also call len_chars stop_idx.
    //   len_chars does not include the '\0'.
}
// Suppose:
// _get_last_line("abc\nxyz\n", 8)  →  "xyz\n"
// _get_last_line("abc\nxyz\n", 4)  →  "abc\n"
// _get_last_line("abc\nxyz",   7)  →  "xyz"
// _get_last_line("abc\nxyz",   4)  →  "abc\n"

uchar* _get_last_block(FILE* fp, size_t stop_idx) {
    // Return a newly malloc'd buffer containing the last block of uchars in the file,
    // up to (but not including) stop_idx.
    // - stop_idx might be the file size.
    // - Sometimes (e.g., on latter invocations of read_line(…)), stop_idx might be
    //   less than the file size, but we get the “last” block, as if it were the file size.

    assert(stop_idx <= _get_file_size(fp));
}

uchar* _concat_string(const uchar* s1, const uchar* s2) {
    // Return a newly malloc'd string containing s1 + s1.
}

// The first time read_line(…) is called, stop_idx will be the same as the file size.

© Copyright 2019 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.