Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

130
LINES

< > BotCompany Repo | #1027374 // CleanFluidTextFile

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5188L/34K).

1  
sclass CleanFluidTextFile implements Runnable {
2  
  srecord Entry(S text, SS params) {
3  
    long targetIndex;
4  
    S actualText() { ret unquote(text); }
5  
  }
6  
7  
  // set by user
8  
  int maxRefLength = 8;
9  
  bool verbose;
10  
  Reader reader;
11  
  Writer writer;
12  
  
13  
  // set by class
14  
  new L<Entry> entries;
15  
  new Map<S, Entry> relocationMap;
16  
17  
  long maxFileSize() {
18  
    ret rangeOfNHexNumberWithNDigits(maxRefLength);
19  
  }
20  
  
21  
  run {
22  
    read();
23  
    write();
24  
  }
25  
  
26  
  void read ctex {
27  
    Producer<S> tokenizer = javaTok_onReader(reader);
28  
    FluidTextFileEntry entry;
29  
    while ((entry = readFluidTextFileEntry(tokenizer)) != null) {
30  
      if (!containsKey(entry.params, "_")) // not a deletable entry
31  
        entries.add(new Entry(entry.quotedContent, entry.params));
32  
    }
33  
    if (verbose) pnlStruct(entries);
34  
  }
35  
  
36  
  void write ctex {
37  
    // make a map and pessimistically assign byte indices to all entries
38  
39  
    for (Entry e : entries)
40  
      mapPut(relocationMap, e.params.get("i"), e);
41  
    makeIndices(entries);
42  
43  
    // rewrite references
44  
45  
    for (Entry e : entries)
46  
      if (hasReferences(e))
47  
        e.text = replaceRefs(e, r -> lookupInRelocationMap(r));
48  
49  
    // write out
50  
51  
    long idx = 0; // index into file
52  
    for (Entry e : entries) {
53  
      assertEquals(e.targetIndex, idx);
54  
      e.params.put("i", renderReference(e.targetIndex));
55  
      S line = entryToString(e, idx);
56  
      if (verbose)
57  
        print(quote(line));
58  
      writer.write(line);
59  
      int l = lUtf8(line);
60  
      if (verbose)
61  
        print("Actual entry length: " + l);
62  
      idx += l;
63  
    }
64  
  }
65  
66  
  S lookupInRelocationMap(S ref) {
67  
    Entry e = relocationMap.get(ref);
68  
    //print("lookupInRelocationMap: " + ref + " => " + e);
69  
    ret e == null ? null : renderReference(e.targetIndex);
70  
  }
71  
72  
  S renderReference(long idx) {
73  
    ret takeLast(maxRefLength, longToHex(idx));
74  
  }
75  
76  
  void makeIndices(L<Entry> entries) {
77  
    long idx = 0; // index into file
78  
    for (Entry e : entries) {
79  
      e.targetIndex = idx;
80  
      int l = pessimisticEntryLength(e);
81  
      idx += l;
82  
      if (verbose)
83  
        print("Pessimistic entry length: " + l);
84  
    }
85  
  }
86  
87  
  S entryToString(Entry e, long idx) {
88  
    ret entryToString(e, renderReference(idx));
89  
  }
90  
  
91  
  S entryToString(Entry e, S idx) {
92  
    SS params = litorderedmap(
93  
      i := idx,
94  
      l := intToHex_flexLength(lUtf8(e.text)));
95  
    mapPutAll_noOverwrite(params, e.params);
96  
    ret renderEqualsCommaProperties(params) + " " + e.text + "\n";
97  
  }  
98  
99  
  S dummyRef() {
100  
    ret rep('0', maxRefLength);
101  
  }
102  
103  
  bool hasReferences(Entry e) {
104  
    ret eq(e.params.get("refs"), quote("at *"));
105  
  }
106  
107  
  int pessimisticEntryLength(Entry e) {
108  
    Entry e2 = e;
109  
    if (hasReferences(e)) {
110  
      e2 = cloneObject(e);
111  
      e2.text = replaceRefs(e, r -> dummyRef());
112  
      if (verbose)
113  
        print("Pessimistic: rewrote text to " + quote(e2.text));
114  
    }
115  
    S s = entryToString(e2, dummyRef());
116  
    if (verbose)
117  
      print(quote(s));
118  
    ret lUtf8(s);
119  
  }
120  
121  
  S replaceRefs(Entry e, IF1<S> f) {
122  
    LS tok = javaTokWithUnifiedNumbersAndIdentifiers(e.actualText());
123  
    for (int i : jfindAll(tok, "at *")) {
124  
      S x = f.get(tok.get(i+2));
125  
      if (nempty(x))
126  
        tok.set(i+2, x);
127  
    }
128  
    ret multiLineQuote(join(tok));
129  
  }
130  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027374
Snippet name: CleanFluidTextFile
Eternal ID of this version: #1027374/19
Text MD5: ffa4dfe811c003225ea49771acaee792
Transpilation MD5: b13db51370e86a00e50eebeda216e009
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-15 17:11:10
Source code size: 3353 bytes / 130 lines
Pitched / IR pitched: No / No
Views / Downloads: 265 / 670
Version history: 18 change(s)
Referenced in: [show references]