summaryrefslogtreecommitdiff
path: root/SparkleLib/SparkleExtensions.cs
blob: 0b28480c5d5f6dd75c432b7024645b5a256955df (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as 
//   published by the Free Software Foundation, either version 3 of the 
//   License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, see <http://www.gnu.org/licenses/>.


using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SparkleLib {

    public static class Extensions {

        public static string Combine (this string [] parts)
        {
            string new_path = "";

            foreach (string part in parts)
                new_path = Path.Combine (new_path, part);

            return new_path;
        }

        
        public static string SHA1 (this string s)
        {
            SHA1 sha1          = new SHA1CryptoServiceProvider ();
            byte [] bytes      = ASCIIEncoding.Default.GetBytes (s);
            byte [] sha1_bytes = sha1.ComputeHash (bytes);

            return BitConverter.ToString (sha1_bytes).ToLower ().Replace ("-", "");
        }


        public static string SHA256 (this string s)
        {
            SHA256 sha256        = new SHA256CryptoServiceProvider ();
            byte [] bytes        = ASCIIEncoding.Default.GetBytes (s);
            byte [] sha256_bytes = sha256.ComputeHash (bytes);

            return BitConverter.ToString (sha256_bytes).ToLower ().Replace ("-", "");
        }


        public static string SHA256 (this string s, string salt)
        {
            SHA256 sha256        = new SHA256CryptoServiceProvider ();
            byte [] bytes        = ASCIIEncoding.Default.GetBytes (s + salt);
            byte [] sha256_bytes = sha256.ComputeHash (bytes);

            return BitConverter.ToString (sha256_bytes).ToLower ().Replace ("-", "");
        }


        public static string MD5 (this string s)
        {
            MD5 md5           = new MD5CryptoServiceProvider ();
            byte [] bytes     = ASCIIEncoding.Default.GetBytes (s);
            byte [] md5_bytes = md5.ComputeHash (bytes);

            return BitConverter.ToString (md5_bytes).ToLower ().Replace ("-", "");
        }


        // Format a file size nicely with small caps.
        // Example: 1048576 becomes "1 ᴍʙ"
        public static string ToSize (this double byte_count)
        {
            if (byte_count >= 1099511627776)
                return String.Format ("{0:##.##} ᴛʙ", Math.Round (byte_count / 1099511627776, 2));
            else if (byte_count >= 1073741824)
                return String.Format ("{0:##.##} ɢʙ", Math.Round (byte_count / 1073741824, 1));
            else if (byte_count >= 1048576)
                return String.Format ("{0:##.##} ᴍʙ", Math.Round (byte_count / 1048576, 1));
            else if (byte_count >= 1024)
                return String.Format ("{0:##.##} ᴋʙ", Math.Round (byte_count / 1024, 0));
            else
                return byte_count.ToString () + " ʙ";
        }


        public static bool IsSymlink (this FileSystemInfo file)
        {
            return ((file.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
        }


        public static string ToPrettyDate (this DateTime timestamp)
        {
            TimeSpan time_diff = DateTime.Now.Subtract (timestamp);
            int day_diff       = (int) time_diff.TotalDays;                
            DateTime yesterday = DateTime.Today.AddDays (-1);

            if (timestamp >= yesterday && timestamp < DateTime.Today) {
                return "yesterday at " + timestamp.ToString ("HH:mm");

            } else if (day_diff == 0) {
                return "today at " + timestamp.ToString ("HH:mm");
            
            } else if (day_diff < 7) {
                return timestamp.ToString ("dddd");
            
            } else if (day_diff < 31) {
                if (day_diff < 14)
                    return "last week";
                else
                    return string.Format ("{0} weeks ago", Math.Ceiling ((double) day_diff / 7));

            } else if (day_diff < 62) {
                return "last month";

            } else { 
                return string.Format ("{0} months ago", Math.Ceiling ((double) day_diff / 31));
            }
        }


        public static string ReplaceUnderscoreWithSpace (this string s)
        {
            int len = s.Length, lead = 0, trail = 0;
            for (int i = 0; i < len && s[i] == '_'; i++, lead++)
                ; // nop
            for (int i = len - 1; i >= lead && s[i] == '_'; i--, trail++)
                ; // nop
            if (lead == 0 && trail == 0)
                return s.Replace("_", " "); // fast code path
            else
                return s.Substring (0, lead) +
                       s.Substring (lead, len - lead - trail).Replace ("_", " ") +
                       s.Substring (len - trail, trail);
        }
    }
}