1 // =================================
 2 // Copyright (c) 2020 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Collections;
 8 using System.IO;
 9 
10 namespace System.Lex
11 {
12     public SharedPtr<ByteStream> MakeClassMapData(const List<int>& classMapVecbool compressData)
13     {
14         SharedPtr<ByteStream> in(new MemoryByteStream());
15         SharedPtr<ByteStream> out = in;
16         BinaryWriter binaryWriter(in);
17         binaryWriter.Write(cast<int>(classMapVec.Count()));
18         for (int i : classMapVec)
19         {
20             binaryWriter.Write(i);
21         }
22         if (compressData)
23         {
24             if (getSystemCompressionMethod != GetSystemCompressionMethodFunction())
25             {
26                 byte compressionMethod = getSystemCompressionMethod();
27                 if (compressionMethod >= numSystemCompressions)
28                 {
29                     throw Exception("error: system compression method value too big (>= " + ToString(numSystemCompressions) + ") '" + ToString(compressionMethod) + "'");
30                 }
31                 DataCompressionFunction compressionFunction = compressFunctions[compressionMethod];
32                 if (compressionFunction != DataCompressionFunction())
33                 {
34                     out.Reset(new MemoryByteStream());
35                     out->Write(compressionMethod);
36                     compressionFunction(inout);
37                 }
38                 else
39                 {
40                     throw Exception("error: could not make class map data because System.IO.compressFunction[" + ToString(compressionMethod) + "] function not set");
41                 }
42             }
43             else
44             {
45                 throw Exception("error: could not make class map data because System.IO.getSystemCompressionMethod function not set");
46             }
47         }
48         return out;
49     }
50         
51     public int* CreateClassMap(byte* classMapDatalong classMapDataLengthbool classMapDataCompressed)
52     {
53         SharedPtr<ByteStream> in(new MemoryByteStream());
54         SharedPtr<ByteStream> out = in;
55         BinaryWriter binaryWriter(in);
56         byte* e = classMapData + classMapDataLength;
57         for (byte* p = classMapData; p != e; ++p;)
58         {
59             binaryWriter.Write(*p);
60         }
61         if (classMapDataCompressed)
62         {
63             int compressionMethod = in->ReadByte();
64             if (compressionMethod == -1)
65             {
66                 throw Exception("error: could not expand class map because class map data is empty");
67             }
68             byte method = cast<byte>(compressionMethod);
69             if (method >= numSystemCompressions)
70             {
71                 throw Exception("error: could not expand class map, invalid class map data compression method byte '" + ToString(method) + "', not 0=deflate or 1=bzip2");
72             }
73             DataCompressionFunction expandFunction = expandFunctions[method];
74             if (expandFunction != DataCompressionFunction())
75             {
76                 out.Reset(new MemoryByteStream());
77                 expandFunction(inout);
78             }
79             else
80             {
81                 throw Exception("error: could not expand class map because System.IO.expandFunction[" + ToString(method) + "] function not set");
82             }
83         }
84         BinaryReader binaryReader(out);
85         int n = binaryReader.ReadInt();
86         int* classMap = cast<int*>(MemAlloc(n * sizeof(int)));
87         for (int i = 0; i < n; ++i;)
88         {
89             classMap[i] = binaryReader.ReadInt();
90         }
91         return classMap;
92     }
93 }
94