otsdaq_utilities  v2_04_01
doxygen_main_editor.cpp
1 // doxygen_main_editor.cpp
2 // by rrivera at fnal dot gov
3 // created May 2018
4 //
5 // This is a simple html code injector to improve the main.html generated by doxygen.
6 //
7 // Planned to be used in conjunction with OnlineDocPushUpdate.sh
8 //
9 // compile with:
10 // g++ doxygen_main_editor.cpp -o hw.o
11 //
12 // if developing, consider appending -D_GLIBCXX_DEBUG to get more
13 // descriptive error messages
14 //
15 // run with:
16 //./doxygen_main_editor.o <full main.html path> <full inject main html file path>
17 //
18 
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <netdb.h>
22 #include <netinet/in.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <iomanip>
30 #include <iostream>
31 
32 // take only file name
33 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
34 
35 // use this for normal printouts
36 #define __PRINTF__ printf
37 #define __COUT__ cout << __FILENAME__ << std::dec << " [" << __LINE__ << "]\t"
38 #define __E__ std::endl
39 
40 // and use this to suppress
41 //#define __PRINTF__ if(0) printf
42 //#define __COUT__ if(0) cout
43 
44 using namespace std;
45 
46 int main(int argc, char** argv)
47 {
48  __COUT__ << "Starting doxygen main.html editor..." << __E__;
49 
50  if(argc < 4)
51  {
52  __COUT__ << "Need 3 arguments: for the full path to main.html AND to "
53  "ARRAY:<html-to-inject>"
54  << __E__;
55  return 0;
56  }
57  string mainfn = argv[1];
58  string injectfn = argv[2];
59  string inject2fn = argv[3];
60  __COUT__ << "main.html destination full path: " << mainfn << __E__;
61  __COUT__ << "main.html source full path: " << mainfn + ".bk" << __E__;
62  __COUT__ << "inject.html source full path: " << injectfn << __E__;
63  __COUT__ << "inject2.html source full path: " << inject2fn << __E__;
64 
65  FILE* mainSrc = fopen((mainfn + ".bk").c_str(), "r");
66  if(!mainSrc)
67  {
68  __COUT__ << "Failed to open... " << mainfn + ".bk" << __E__;
69  return 0;
70  }
71  FILE* injectSrc = fopen((injectfn).c_str(), "r");
72  if(!injectSrc)
73  {
74  __COUT__ << "Failed to open... " << injectfn << __E__;
75  return 0;
76  }
77  FILE* inject2Src = fopen((inject2fn).c_str(), "r");
78  if(!inject2Src)
79  {
80  __COUT__ << "Failed to open... " << inject2fn << __E__;
81  return 0;
82  }
83  FILE* mainDest = fopen((mainfn).c_str(), "w");
84  if(!mainSrc)
85  {
86  __COUT__ << "Failed to open... " << mainfn << __E__;
87  return 0;
88  }
89 
90  char line[1000];
91  unsigned int countdown = -1;
92 
93  unsigned int injectIndex = 0;
94 
95  bool injected = true;
96  while(fgets(line, 1000, mainSrc))
97  {
98  fputs(line, mainDest); // output main line to file
99  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
100 
101  if(injected && !strcmp(line, "<div class=\"contents\">\n"))
102  {
103  injected = false;
104  countdown = 0;
105  injectIndex = 1;
106  // continue;
107  }
108  else if(injected && !strcmp(line, "<head>\n"))
109  {
110  injected = false;
111  countdown = 0;
112  injectIndex = 2;
113  // continue;
114  }
115 
116  if(!injected && countdown == 0) // inject file
117  {
118  injected = true;
119 
120  switch(injectIndex)
121  {
122  case 1:
123  {
124  // get one more line and modify it, ie, clip ugly start and delete close
125  // tag for content div fgets(line,1000,mainSrc);
126  // __COUT__ << "MOD " << line << (line[strlen(line)-1] ==
127  //'\n'?"":"\n"); line[strlen(line)-7] = '\0';
128  // for(countdown=strlen(line)-16;countdown<strlen(line);++countdown)
129  // if(line[countdown]=='v') break;
130  //
131  // //keep version number
132  // fputs("<h3>",mainDest);
133  // __COUT__ << "<h3>" << __E__;
134  // fputs(&line[countdown],mainDest); //output main line to
135  // file
136  // __COUT__ << &line[countdown] << __E__;
137 
138  while(fgets(line, 1000, injectSrc))
139  {
140  fputs(line, mainDest); // output inject line to file
141  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
142  }
143 
144  // add close content div
145  // fputs("</div>",mainDest);
146  // __COUT__ << "</div>" << __E__;
147 
148  break;
149  }
150  case 2:
151  {
152  while(fgets(line, 1000, inject2Src))
153  {
154  fputs(line, mainDest); // output inject line to file
155  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
156  }
157  break;
158  }
159  default:
160  __COUT__ << "Unknown injection!" << __E__;
161  }
162  }
163  else if(!injected)
164  {
165  --countdown;
166  }
167  }
168 
169  fclose(mainDest);
170  fclose(injectSrc);
171  fclose(mainSrc);
172 
173  __COUT__ << "Doxygen main.html editor complete!" << __E__;
174 
175  return 0;
176 }