This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort
115 lines (92 loc) · 2.58 KB
/
sort
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
#!/usr/bin/env php
<?php
/**
* Sort script.
*
* Sorts randomized floating numbers from file and save to another file.
*
* @author Adam "Saibamen" Stachowicz <[email protected]>
* @license MIT
*
* @link https://github.com/Saibamen/Generate-Sort-Numbers
*/
namespace Sort;
use Includes\File;
use Includes\Generate;
use Includes\Input;
use Includes\Text;
// Run only in CLI
if (php_sapi_name() !== 'cli') {
exit('This is console app. Please run it in console (CLI)!');
}
// Increase the memory limit
ini_set('memory_limit', '1024M');
require_once 'Includes/Generate.php';
require_once 'Includes/Input.php';
require_once 'Includes/File.php';
// Console arguments
$longOpts = array(
'input:',
'output:',
'ext:',
'delimiter:',
'debug',
);
$args = getopt(null, $longOpts);
// Extension
$fileExtension = '.dat';
if (isset($args['ext'])) {
// preg match for \ and /
if (Input::isFilenameWrong($args['ext']) || preg_match('/[\/\\\\]/', $args['ext'])) {
Text::message('Wrong file extension.');
} else {
$fileExtension = $args['ext'];
}
}
// Delimiter
$delimiter = ' ';
if (isset($args['delimiter'])) {
if (preg_match('/[0-9-.]/', $args['delimiter'])) {
Text::message('Wrong delimiter.');
} else {
$delimiter = $args['delimiter'];
}
}
// Debug mode
if (isset($args['debug'])) {
Text::message('Debug mode. Have fun!');
Text::setDebug(true);
}
// Input
if (isset($args['input'])) {
if (file_exists($args['input'].$fileExtension)) {
$inputFilename = $args['input'];
} else {
Text::message('Input file does not exist.');
}
}
// Output
if (isset($args['output'])) {
if (Input::isFilenameWrong($args['output'])) {
Text::message('Wrong output filename.');
} else {
$outputFilename = $args['output'];
}
}
unset($args);
// Collect User input
if (!isset($inputFilename)) {
$inputFilename = Input::getExistsFilename('Type input filename to read (without extension)', 'output', $fileExtension);
}
if (!isset($outputFilename)) {
$outputFilename = Input::getFilename('Type output filename (without extension)', 'sorted', $fileExtension);
}
File::createMissingDirectory($outputFilename);
File::checkIfFileExistsAndDeleteContent($outputFilename, $fileExtension);
$array = File::getArrayFromFile($inputFilename, $fileExtension, $delimiter);
Text::debug('Array elements: '.count($array));
Text::message('SORTING...');
$sortStart = microtime(true);
sort($array);
Text::printTimeDuration($sortStart);
File::saveArrayToFile($array, $outputFilename, $fileExtension, $delimiter);