aboutsummaryrefslogtreecommitdiff
path: root/challenge-103/abigail/awk/ch-2.gawk
blob: 301730b203524debe8b487244c9ac2c9e0debb51 (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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: gawk -f ch-2.gawk < input-file
#

#
# We're reading a start time, the current time, and a file name
# from STDIN, separated by white space. The file contains the
# tracks played.
#
{
    start_time   = $1
    current_time = $2
    file_name    = $3

    time_diff = (current_time - start_time) * 1000

    nr_of_tracks = 1
    total_time = 0
    while ((getline track < file_name > 0)) {
        #
        # AWK doesn't have the ability to limit the number of
        # fields a string is split into, so we use index() and
        # substring() instead of split() to get the info.
        #
        j        = index  (track, ",")
        run_time = substr (track, 1, j - 1)
        title    = substr (track, j + 1)
        tracks [nr_of_tracks, 0] = run_time
        tracks [nr_of_tracks, 1] = title
        nr_of_tracks ++

        total_time += run_time
    }

    #
    # We're not interested in playing full loops.
    #
    time_diff %= total_time

    #
    # Find the current track playing. If time_diff is less than the
    # play time of the track, this is the track currently playing. If
    # so, print the track name (with quotes, as given in the example --
    # although they don't belong there, as they're from the CSV encoding;
    # for that reason, we won't deescape anything else either). Otherwise
    # (track length is shorter than time_diff), we subtrack the length
    # of the track from time_diff, and continue checking the next track.
    #
    for (i = 1; i <= nr_of_tracks; i ++) {
        if (time_diff - tracks [i, 0] < 0) {
            time_diff = int  (time_diff / 1000)
            hours     = int  (time_diff / 3600)
            minutes   = int ((time_diff % 3600) / 60)
            seconds   =       time_diff         % 60
            print tracks [i, 1]
            if (hours > 0) {
                printf ("%02d:%02d:%02d\n", hours, minutes, seconds)
            }
            else {
                printf (     "%02d:%02d\n",        minutes, seconds)
            }

            break
        }
        else {
            time_diff -= tracks [i, 0]
        }
    }
}