forked from nikhilk/scriptsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptPreprocessor.cs
More file actions
74 lines (64 loc) · 2.62 KB
/
Copy pathScriptPreprocessor.cs
File metadata and controls
74 lines (64 loc) · 2.62 KB
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
// ScriptSharpPreprocessor.cs
// Script#/Tools/Preprocessor
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using ScriptSharp.Preprocessor;
namespace ScriptSharp {
internal sealed class ScriptPreprocessor {
private IStreamResolver _includeResolver;
private IErrorHandler _errorHandler;
public ScriptPreprocessor(IStreamResolver includeResolver, IErrorHandler errorHandler) {
_includeResolver = includeResolver;
_errorHandler = errorHandler;
}
public void Preprocess(PreprocessorOptions options) {
Stream outputStream = options.TargetFile.GetStream();
StreamWriter outputWriter = null;
PreprocessorTextReader preprocessor = null;
try {
if (outputStream != null) {
outputWriter = new StreamWriter(outputStream);
preprocessor = new PreprocessorTextReader(options.SourceFile,
options.PreprocessorVariables,
_includeResolver);
TextReader contentReader = preprocessor;
if (options.Minimize) {
contentReader = new CondenserTextReader(preprocessor, options.StripCommentsOnly);
}
if (preprocessor.Initialize(outputWriter)) {
int ch;
while ((ch = contentReader.Read()) != -1) {
if (options.UseWindowsLineBreaks && (ch == '\n')) {
outputWriter.Write('\r');
}
outputWriter.Write((char)ch);
}
}
}
}
catch (PreprocessorException pe) {
if (_errorHandler != null) {
_errorHandler.ReportError(pe.Message, pe.SourceFile + " (" + pe.Line + ", 1)");
}
if (pe.InnerException != null) {
throw pe.InnerException;
}
}
finally {
if (preprocessor != null) {
preprocessor.Close();
}
if (outputWriter != null) {
outputWriter.Flush();
}
if (outputStream != null) {
options.TargetFile.CloseStream(outputStream);
}
}
}
}
}