File size: 4,096 Bytes
6851d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*******************************************************************************
*  Copyright by the contributors to the Dafny Project
*  SPDX-License-Identifier: MIT
*******************************************************************************/

namespace DafnyLibraries
{
    using System;
    using System.IO;

    using Dafny;

    public class FileIO
    {
        /// <summary>
        /// Attempts to read all bytes from the file at the given path, and outputs the following values:
        /// <list>
        ///   <item>
        ///     <term>isError</term>
        ///     <description>
        ///       true iff an exception was thrown during path string conversion or when reading the file
        ///     </description>
        ///   </item>
        ///   <item>
        ///     <term>bytesRead</term>
        ///     <description>
        ///       the sequence of bytes read from the file, or an empty sequence if <c>isError</c> is true
        ///     </description>
        ///   </item>
        ///   <item>
        ///     <term>errorMsg</term>
        ///     <description>
        ///       the error message of the thrown exception if <c>isError</c> is true, or an empty sequence otherwise
        ///     </description>
        ///   </item>
        /// </list>
        ///
        /// We output these values individually because Result is not defined in the runtime but instead in library code.
        /// It is the responsibility of library code to construct an equivalent Result value.
        /// </summary>
        public static void INTERNAL_ReadBytesFromFile(ISequence<char> path, out bool isError, out ISequence<byte> bytesRead,
          out ISequence<char> errorMsg)
        {
            isError = true;
            bytesRead = Sequence<byte>.Empty;
            errorMsg = Sequence<char>.Empty;
            try
            {
                bytesRead = Helpers.SeqFromArray(File.ReadAllBytes(path?.ToString()));
                isError = false;
            }
            catch (Exception e)
            {
                errorMsg = Helpers.SeqFromArray(e.ToString().ToCharArray());
            }
        }

        /// <summary>
        /// Attempts to write all given bytes to the file at the given path, creating nonexistent parent directories as necessary,
        /// and outputs the following values:
        /// <list>
        ///   <item>
        ///     <term>isError</term>
        ///     <description>
        ///       true iff an exception was thrown during path string conversion or when writing to the file
        ///     </description>
        ///   </item>
        ///   <item>
        ///     <term>errorMsg</term>
        ///     <description>
        ///       the error message of the thrown exception if <c>isError</c> is true, or an empty sequence otherwise
        ///     </description>
        ///   </item>
        /// </list>
        ///
        /// We output these values individually because Result is not defined in the runtime but instead in library code.
        /// It is the responsibility of library code to construct an equivalent Result value.
        /// </summary>
        public static void INTERNAL_WriteBytesToFile(ISequence<char> path, ISequence<byte> bytes, out bool isError, out ISequence<char> errorMsg)
        {
            isError = true;
            errorMsg = Sequence<char>.Empty;
            try
            {
                string pathStr = path?.ToString();
                CreateParentDirs(pathStr);
                File.WriteAllBytes(pathStr, bytes.CloneAsArray());
                isError = false;
            }
            catch (Exception e)
            {
                errorMsg = Helpers.SeqFromArray(e.ToString().ToCharArray());
            }
        }

        /// <summary>
        /// Creates the nonexistent parent directory(-ies) of the given path.
        /// </summary>
        private static void CreateParentDirs(string path)
        {
            string parentDir = Path.GetDirectoryName(Path.GetFullPath(path));
            Directory.CreateDirectory(parentDir);
        }
    }
}