1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.IO;
 8 using System.IO.Compression;
 9 
10 namespace System.Init.Compression
11 {
12     public void CompressDeflate(SharedPtr<ByteStream>& inSharedPtr<ByteStream>& out)
13     {
14         DeflateStream compressStream(outCompressionMode.compress);
15         in->CopyTo(compressStream);
16     }
17 
18     public void ExpandDeflate(SharedPtr<ByteStream>& inSharedPtr<ByteStream>& out)
19     {
20         DeflateStream expandStream(inCompressionMode.decompress);
21         expandStream.CopyTo(*out);
22     }
23 
24     public void CompressBZ2(SharedPtr<ByteStream>& inSharedPtr<ByteStream>& out)
25     {
26          BZip2Stream compressStream(outCompressionMode.compress);
27          in->CopyTo(compressStream);
28     }
29 
30     public void ExpandBZ2(SharedPtr<ByteStream>& inSharedPtr<ByteStream>& out)
31     {
32         BZip2Stream expandStream(inCompressionMode.decompress);
33         expandStream.CopyTo(*out);
34     }
35 
36     public byte GetSystemCompressionMethod()
37     {
38         string compression = "deflate";
39         const char* cmajorCompression = RtGetEnvironmentVariable("CMAJOR_COMPRESSION");
40         if (cmajorCompression != null)
41         {
42             compression = ToLower(cmajorCompression);
43         }
44         if (compression == "bzip2")
45         {
46             return System.IO.bzip2Compression;
47         }
48         else
49         {
50             if (!compression.IsEmpty() && compression != "deflate")
51             {
52                 Console.Error() << "warning: System.Init.Compression: unknown CMAJOR_COMPRESSION value '" << compression << "', using deflate compression." << endl();
53             }
54             return System.IO.deflateCompression;
55         }
56     }
57 
58     public void SetCompressionFunctions()
59     {
60         System.IO.getSystemCompressionMethod = GetSystemCompressionMethod;
61         System.IO.compressFunctions[deflateCompression] = CompressDeflate;
62         System.IO.expandFunctions[deflateCompression] = ExpandDeflate;
63         System.IO.compressFunctions[bzip2Compression] = CompressBZ2;
64         System.IO.expandFunctions[bzip2Compression] = ExpandBZ2;
65     }
66 }