summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2017-11-08 13:35:11 -0500
committerGitHub <noreply@github.com>2017-11-08 13:35:11 -0500
commit9a26ba87e79d4ec16bdee49bb05345849227cc39 (patch)
treecffe8ed0c6d2a884c5e7ff00035ae956ce917e56
parentd4aaa6cda39b338e77d861eb321dc1a677287b75 (diff)
parentda58c702d8be302f8d4ccc9ba27252759398d4d6 (diff)
Merge pull request #267 from csyonghe/work
Don't update generated .h file if its not changed.
-rw-r--r--tools/slang-generate/main.cpp51
1 files changed, 50 insertions, 1 deletions
diff --git a/tools/slang-generate/main.cpp b/tools/slang-generate/main.cpp
index 8208354e4..475849993 100644
--- a/tools/slang-generate/main.cpp
+++ b/tools/slang-generate/main.cpp
@@ -370,6 +370,42 @@ void usage(char const* appName)
fprintf(stderr, "usage: %s <input>\n", appName);
}
+char* readAllText(char const * fileName)
+{
+ FILE * f;
+ fopen_s(&f, fileName, "rb");
+ if (!f)
+ {
+ return "";
+ }
+ else
+ {
+ fseek(f, 0, SEEK_END);
+ auto size = ftell(f);
+ char * buffer = new char[size + 1];
+ memset(buffer, 0, size + 1);
+ fseek(f, 0, SEEK_SET);
+ fread(buffer, sizeof(char), size, f);
+ fclose(f);
+ return buffer;
+ }
+}
+
+void writeAllText(char const *srcFileName, char const* fileName, char* content)
+{
+ FILE * f = nullptr;
+ fopen_s(&f, fileName, "wb");
+ if (!f)
+ {
+ printf("%s(0): error G0001: cannot write file %s\n", srcFileName, fileName);
+ }
+ else
+ {
+ fwrite(content, 1, strlen(content), f);
+ fclose(f);
+ }
+}
+
int main(
int argc,
char** argv)
@@ -416,8 +452,9 @@ int main(
Node* node = readInput(input, inputEnd);
+ // write output to a temporary file first
char outputPath[1024];
- sprintf_s(outputPath, "%s.h", inputPath);
+ sprintf_s(outputPath, "%s.temp.h", inputPath);
FILE* outputStream;
fopen_s(&outputStream, outputPath, "w");
@@ -426,5 +463,17 @@ int main(
fclose(outputStream);
+ // update final output only when content has changed
+ char outputPathFinal[1024];
+ sprintf_s(outputPathFinal, "%s.h", inputPath);
+
+ char * allTextOld = readAllText(outputPathFinal);
+ char * allTextNew = readAllText(outputPath);
+ if (strcmp(allTextNew, allTextOld) != 0)
+ {
+ writeAllText(inputPath, outputPathFinal, allTextNew);
+ }
+ remove(outputPath);
+
return 0;
}