/
IncreaseDecreaseNumbersProcessor.cs
64 lines (55 loc) · 1.6 KB
/
IncreaseDecreaseNumbersProcessor.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Daramkun.DaramRenamer.Processors.Number
{
[Serializable]
public class IncreaseDecreaseNumbersProcessor : IProcessor
{
public string Name => "process_increase_decrease_numbers";
public bool CannotMultithreadProcess => false;
[Localized ( "incdec_count", 0 )]
public int Count { get; set; } = 1;
[Localized ( "incdec_pos", 1 )]
public OnePointPosition Position { get; set; } = OnePointPosition.EndPoint;
public bool Process ( FileInfo file )
{
if ( Count == 0 ) return true;
if ( file.ChangedFilename.Length == 0 ) return false;
string fn = Path.GetFileNameWithoutExtension ( file.ChangedFilename );
bool meetTheNumber = false;
int offset = 0, count = 0, size = 0;
foreach ( char ch in Position == OnePointPosition.StartPoint ? fn : fn.Reverse () )
{
if ( ( ch >= '0' && ch <= '9' ) )
{
if ( !meetTheNumber )
{
offset = count;
meetTheNumber = true;
}
++size;
}
else
{
if ( meetTheNumber )
break;
}
++count;
}
if ( !meetTheNumber ) return false;
if ( Position == OnePointPosition.EndPoint )
offset = fn.Length - ( offset + size );
string origin = fn.Substring ( offset, size );
int number = int.Parse ( origin ) + Count;
StringBuilder sb = new StringBuilder ();
sb.Append ( number );
fn = fn.Remove ( offset, size ).Insert ( offset, sb.ToString () );
file.ChangedFilename = fn + Path.GetExtension ( file.ChangedFilename );
return true;
}
}
}