summaryrefslogtreecommitdiffhomepage
path: root/dlang/src/ocda/load_pod.d
blob: 8f59fe317c3b31324fcf4e3ac6407632f6848e44 (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
module ocda.load_pod;

/+ ↓ the abstraction out of a pod archive.
     .
     A .sisupod is one zstd frame wrapping a stored zip; a .zip is the same
     archive unwrapped, which is what spine published before it wrapped them.
     Either way, a pod built with --pod2 carries the abstraction inside it,
     at media/abstraction/<uid>.<lang>.ssp, and that is what is read.
     .
     So this is a third input form and *not* a third model: the .ssp inside
     is parsed by ocda.load_ssp and comes back as the same value. A writer
     over the library gains .sisupod without being touched, which is the
     whole reason the dispatch is in one place.
     .
     A pod built without --pod2 carries markup and no abstraction. That is
     refused, and the refusal says which flag makes a pod this reader can
     read, because "no abstraction in it" is a fact about the pod rather
     than about the reader.
+/

import std.algorithm : endsWith, sort;
import std.array : array;
import std.file : read;
import std.string : indexOf;
import ocda.model;
import ocda.load_ssp;
import ocda.zip;
import ocda.zstd;

/+ ↓ a limit on what a frame may expand to.
     A reader of an artefact it did not make declines to be told how much
     memory to allocate. 512 MiB is far above the largest pod in the
     reference collection (4 MiB) and far below anything that matters.
+/
enum size_t max_pod_size = 512 * 1024 * 1024;

class OcdaPodError : Exception {
  this(string msg) { super(msg); }
}

/+ ↓ which languages of the abstraction the pod carries, in sorted order +/
string[] podLanguages(string path) {
  auto bytes = archiveBytes(path);
  string[] out_;
  foreach (e; zipFindSuffix(zipEntries(bytes), ".ssp")) {
    string lang = langOfSspName(e.name);
    if (lang.length > 0) { out_ ~= lang; }
  }
  return out_.sort.array;
}

/+ ↓ load one language of the abstraction a pod carries.
     lang == "" takes the only one there is, and where there is a choice it
     says so rather than choosing, as the .ocda.db loader does.
+/
OcdaDocument loadPodFile(string path, string lang = "") {
  auto bytes = archiveBytes(path);
  auto entries = zipEntries(bytes);
  auto ssps = zipFindSuffix(entries, ".ssp");
  if (ssps.length == 0) {
    throw new OcdaPodError(path ~ ": carries no .ssp."
      ~ " A pod carries the abstraction only when spine was asked for it:"
      ~ " spine --pod2 puts it in, spine --source does not.");
  }
  ZipEntry chosen = ssps[0];
  if (lang.length > 0) {
    bool found = false;
    foreach (e; ssps) {
      if (langOfSspName(e.name) == lang) { chosen = e; found = true; break; }
    }
    if (!found) {
      string have;
      foreach (i, e; ssps) {
        have ~= (i > 0 ? " " : "") ~ langOfSspName(e.name);
      }
      throw new OcdaPodError(path ~ ": no language '" ~ lang
        ~ "'; it holds: " ~ have);
    }
  } else if (ssps.length > 1) {
    string have;
    foreach (i, e; ssps) {
      have ~= (i > 0 ? " " : "") ~ langOfSspName(e.name);
    }
    throw new OcdaPodError(path ~ ": holds more than one language ("
      ~ have ~ "); name the one wanted");
  }
  auto ssp = zipRead(bytes, chosen);
  return parseSsp(cast(string) ssp.idup);
}

/+ ↓ the archive, unwrapped if it was wrapped.
     The bytes decide, not the name: a .sisupod is a zstd frame and a .zip
     is not, and a file misnamed either way still reads.
+/
package ubyte[] archiveBytes(string path) {
  auto bytes = cast(ubyte[]) read(path);
  return zstdIsFrame(bytes)
    ? zstdDecompress(bytes, max_pod_size)
    : bytes;
}

/+ ↓ <uid>.<lang>.ssp -> <lang> +/
package string langOfSspName(string name) {
  if (!name.endsWith(".ssp")) { return ""; }
  string stem = name[0 .. $ - 4];
  auto dot = stem.lastIndexOf('.');
  return (dot > 0) ? stem[dot + 1 .. $] : "";
}

private ptrdiff_t lastIndexOf(string s, char c) {
  for (ptrdiff_t i = s.length - 1; i >= 0; i--) {
    if (s[i] == c) { return i; }
  }
  return -1;
}