-
Notifications
You must be signed in to change notification settings - Fork 4
/
Common.cs
90 lines (85 loc) · 2.03 KB
/
Common.cs
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
using System;
using System.IO;
namespace AlbLib
{
/// <summary>
/// Contains various common functions and magic contants.
/// </summary>
public static class Common
{
/// <summary>
/// 63×ColorConversion = 255
/// </summary>
public const double ColorConversion = 255.0/63.0;
/// <summary>
/// Converts nullable index to file index and subfile index
/// </summary>
/// <param name="index">
/// Nullable index. (0 = null, 1 - 99 => 0 - 99, 100+ => 100+)
/// </param>
/// <param name="fileIndex">
/// File index.
/// </param>
/// <param name="subfileIndex">
/// Subfile index.
/// </param>
/// <returns>
/// True if <paramref name="index"/> is non-zero.
/// </returns>
public static bool E(int index, out int fileIndex, out int subfileIndex)
{
if(index == 0)
{
fileIndex = 0; subfileIndex = 0;
return false;
}
fileIndex = index/100;
subfileIndex = index<100?index-1:index%100;
return true;
}
/// <summary>
/// Converts file and subfile index to nullable index.
/// </summary>
/// <param name="fileIndex">
/// File index.
/// </param>
/// <param name="subfileIndex">
/// Subfile index.
/// </param>
/// <returns>
/// Nullable index. (0 = null, 1 - 99 => 0 - 99, 100+ => 100+)
/// </returns>
public static int E(int fileIndex, int subfileIndex)
{
return fileIndex==0?subfileIndex+1:fileIndex*100+subfileIndex;
}
private static readonly byte[] skipBuffer = new byte[4096];
/// <summary>
/// Skips <paramref name="bytes"/> from stream.
/// </summary>
/// <param name="input">
/// Input stream.
/// </param>
/// <param name="bytes">
/// Number of bytes to skip.
/// </param>
/// <returns>
/// Number of skipped bytes.
/// </returns>
public static int Skip(this Stream input, int bytes)
{
int read = 0;
do{
if(bytes > 4096)
{
read += input.Read(skipBuffer, 0, 4096);
bytes -= 4096;
}else{
read += input.Read(skipBuffer, 0, bytes);
bytes = 0;
}
}while(bytes != 0);
return read;
}
}
}