]> git.sesse.net Git - plocate/blob - serializer.cpp
Bump version number.
[plocate] / serializer.cpp
1 #include "serializer.h"
2
3 #include "dprintf.h"
4
5 #include <chrono>
6 #include <inttypes.h>
7 #include <memory>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <utility>
11
12 using namespace std;
13 using namespace std::chrono;
14
15 extern steady_clock::time_point start;
16
17 void apply_limit()
18 {
19         if (--limit_left > 0) {
20                 return;
21         }
22         dprintf("Done in %.1f ms, found %" PRId64 " matches.\n",
23                 1e3 * duration<float>(steady_clock::now() - start).count(), limit_matches);
24         if (only_count) {
25                 printf("%" PRId64 "\n", limit_matches);
26         }
27         exit(0);
28 }
29
30 void Serializer::print(uint64_t seq, uint64_t skip, const string msg)
31 {
32         if (only_count) {
33                 if (!msg.empty()) {
34                         apply_limit();
35                 }
36                 return;
37         }
38
39         if (next_seq != seq) {
40                 pending.push(Element{ seq, skip, move(msg) });
41                 return;
42         }
43
44         if (!msg.empty()) {
45                 if (print_nul) {
46                         printf("%s%c", msg.c_str(), 0);
47                 } else {
48                         printf("%s\n", msg.c_str());
49                 }
50                 apply_limit();
51         }
52         next_seq += skip;
53
54         // See if any delayed prints can now be dealt with.
55         while (!pending.empty() && pending.top().seq == next_seq) {
56                 if (!pending.top().msg.empty()) {
57                         if (print_nul) {
58                                 printf("%s%c", pending.top().msg.c_str(), 0);
59                         } else {
60                                 printf("%s\n", pending.top().msg.c_str());
61                         }
62                         apply_limit();
63                 }
64                 next_seq += pending.top().skip;
65                 pending.pop();
66         }
67 }