otsdaq_utilities  v2_05_02_indev
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__ std::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) std::cout
43 
44 int main(int argc, char** argv)
45 {
46  __COUT__ << "Starting doxygen main.html editor..." << __E__;
47 
48  if(argc < 4)
49  {
50  __COUT__ << "Need 3 arguments: for the full path to main.html AND to "
51  "ARRAY:<html-to-inject>"
52  << __E__;
53  return 0;
54  }
55  std::string mainfn = argv[1];
56  std::string injectfn = argv[2];
57  std::string inject2fn = argv[3];
58  __COUT__ << "main.html destination full path: " << mainfn << __E__;
59  __COUT__ << "main.html source full path: " << mainfn + ".bk" << __E__;
60  __COUT__ << "inject.html source full path: " << injectfn << __E__;
61  __COUT__ << "inject2.html source full path: " << inject2fn << __E__;
62 
63  FILE* mainSrc = fopen((mainfn + ".bk").c_str(), "r");
64  if(!mainSrc)
65  {
66  __COUT__ << "Failed to open... " << mainfn + ".bk" << __E__;
67  return 0;
68  }
69  FILE* injectSrc = fopen((injectfn).c_str(), "r");
70  if(!injectSrc)
71  {
72  __COUT__ << "Failed to open... " << injectfn << __E__;
73  return 0;
74  }
75  FILE* inject2Src = fopen((inject2fn).c_str(), "r");
76  if(!inject2Src)
77  {
78  __COUT__ << "Failed to open... " << inject2fn << __E__;
79  return 0;
80  }
81  FILE* mainDest = fopen((mainfn).c_str(), "w");
82  if(!mainSrc)
83  {
84  __COUT__ << "Failed to open... " << mainfn << __E__;
85  return 0;
86  }
87 
88  char line[1000];
89  unsigned int countdown = -1;
90 
91  unsigned int injectIndex = 0;
92 
93  bool injected = true;
94  while(fgets(line, 1000, mainSrc))
95  {
96  fputs(line, mainDest); // output main line to file
97  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
98 
99  if(injected && !strcmp(line, "<div class=\"contents\">\n"))
100  {
101  injected = false;
102  countdown = 0;
103  injectIndex = 1;
104  // continue;
105  }
106  else if(injected && !strcmp(line, "<head>\n"))
107  {
108  injected = false;
109  countdown = 0;
110  injectIndex = 2;
111  // continue;
112  }
113 
114  if(!injected && countdown == 0) // inject file
115  {
116  injected = true;
117 
118  switch(injectIndex)
119  {
120  case 1:
121  {
122  // get one more line and modify it, ie, clip ugly start and delete close
123  // tag for content div fgets(line,1000,mainSrc);
124  // __COUT__ << "MOD " << line << (line[strlen(line)-1] ==
125  //'\n'?"":"\n"); line[strlen(line)-7] = '\0';
126  // for(countdown=strlen(line)-16;countdown<strlen(line);++countdown)
127  // if(line[countdown]=='v') break;
128  //
129  // //keep version number
130  // fputs("<h3>",mainDest);
131  // __COUT__ << "<h3>" << __E__;
132  // fputs(&line[countdown],mainDest); //output main line to
133  // file
134  // __COUT__ << &line[countdown] << __E__;
135 
136  while(fgets(line, 1000, injectSrc))
137  {
138  fputs(line, mainDest); // output inject line to file
139  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
140  }
141 
142  // add close content div
143  // fputs("</div>",mainDest);
144  // __COUT__ << "</div>" << __E__;
145 
146  break;
147  }
148  case 2:
149  {
150  while(fgets(line, 1000, inject2Src))
151  {
152  fputs(line, mainDest); // output inject line to file
153  __COUT__ << line << (line[strlen(line) - 1] == '\n' ? "" : "\n");
154  }
155  break;
156  }
157  default:
158  __COUT__ << "Unknown injection!" << __E__;
159  }
160  }
161  else if(!injected)
162  {
163  --countdown;
164  }
165  }
166 
167  fclose(mainDest);
168  fclose(injectSrc);
169  fclose(mainSrc);
170 
171  __COUT__ << "Doxygen main.html editor complete!" << __E__;
172 
173  return 0;
174 }