-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
executable file
·67 lines (53 loc) · 1.7 KB
/
io.js
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
const fs = require('fs');
const Photo = require('./photo.class');
function parseFile(selectedInput) {
let inputFile;
fs.readdirSync(__dirname + '/../input/').forEach(filename => {
if(filename.indexOf(selectedInput) === 0) {
inputFile = filename.replace('.txt', '');
}
});
if(!inputFile) {
console.log('No input file');
process.exit(0);
}
let content = fs.readFileSync(__dirname + '/../input/' + inputFile + '.txt', {encoding: 'ascii'});
let lines = content.split('\n');
let currentRow = 0;
params = lines[currentRow].split(' ');
photosNumber = parseInt(params[0]);
currentRow++;
const photos = [];
let id = 0;
for(; currentRow <= photosNumber; currentRow++) {
params = lines[currentRow].split(' ');
let tags = [];
for (let i = 0; i < params[1]; i++) {
let currentTag = params[i + 2];
let numericTag = "";
for (let x = 0; x < currentTag.length; x++) {
numericTag += (currentTag.charCodeAt(x) - 30).toString();
}
numericTag = parseInt(numericTag);
tags.push(numericTag);
}
photos.push(new Photo(id, params[0], +params[1], tags));
id++;
}
return { filename: inputFile, selectedInput: selectedInput, photos: photos };
}
function exportSlideshow(slideshow, filename) {
let content = "";
content += slideshow.slides.length + "\n";
slideshow.slides.forEach(slide => {
let line = "";
slide.photos.forEach(photo => {
line += photo.id + " ";
})
line = line.trim();
content += line + "\n";
});
fs.writeFileSync(__dirname + '/../output/' + filename + '.txt', content, {encoding: 'ascii'});
}
module.exports.parseFile = parseFile;
module.exports.exportSlideshow = exportSlideshow;