void layPaths(int numberOfPaths, int levelNum) { assignMemoryForPaths(numberOfPaths); for(int i = 1; i <= numberOfPaths; i++) { readInPath(levelNum, i); } }
int main(int argc, char **argv) { bool printUsageAndExit = true; bool showDebug = false; bool showInfo = false; EPath prog(argv[0]); EStringArray files; const char *lang = "C"; const char *style = "DocBook"; const char *options = NULL; files.AddItem(NULL); const char *tmp_env = getenv("LC_ALL"); if(tmp_env == NULL) tmp_env = getenv("LANG"); if(tmp_env != NULL) lang = tmp_env; do { if(argc < 2) break; for(int n = 1; n < argc; n++) { if(strcmp(argv[n], "-s") == 0) { if(argc - n < 2) break; n++; style = argv[n]; } else if(strcmp(argv[n], "-t") == 0) { if(argc - n < 2) break; n++; options = argv[n]; } else if(strcmp(argv[n], "-o") == 0) { if(argc - n < 2) break; n++; if(files.ReplaceItem(0, argv[n]) == false) break; } else if(strcmp(argv[n], "-l") == 0) { if(argc - n < 2) break; n++; lang = argv[n]; } else if(strcmp(argv[n], "--debug") == 0) { showDebug = true; } else if(strcmp(argv[n], "--showinfo") == 0) { showInfo = true; } else { files.AddItem(argv[n]); } } if(files.CountItems() < 2) break; printUsageAndExit = false; } while(false); if(printUsageAndExit) { print_usage(prog.Leaf()); exit(1); } EString xml_buffer; EString strDocStart = "<document "; EString strDocEnd = "</document>"; for(eint32 i = 1; i < files.CountItems(); i++) { if(files.ItemAt(i) == NULL) continue; EPath readInPath(files.ItemAt(i)->String(), NULL, true); EFile readIn(readInPath.Path(), E_READ_ONLY); if(readIn.InitCheck() != E_OK) { ETK_DEBUG("[%s] --- Unable to read \"%s\".", prog.Leaf(), files.ItemAt(i)->String()); continue; } eint32 old_length = xml_buffer.Length(); char buffer[BUFFER_SIZE]; bool foundDocEnd = true; size_t nLeave = 0; xml_buffer.AppendFormat("<!-- convert from \"%s\" -->\n", readInPath.Leaf()); while(true) { ssize_t len = readIn.Read(buffer + nLeave, BUFFER_SIZE - nLeave); if(len <= 0) break; EString str; str.SetTo(buffer, len + nLeave); str.RemoveAll("\r"); eint32 offset = 0; while(offset >= 0 && offset < str.Length()) { nLeave = 0; if(foundDocEnd) { offset = str.FindFirst("/*", offset); if(offset < 0) { if(str.Length() < 2) break; if(str[str.Length() - 1] == '/' && str[str.Length() - 2] != '*') nLeave = 1; break; } nLeave = str.Length() - offset; offset = str.FindFirst("\n", offset); if(offset < 0) { if(nLeave > 80) nLeave = 0; break; } nLeave = 0; offset++; if(offset >= str.Length()) break; if(strDocStart.Compare(str.String() + offset, strDocStart.Length()) != 0) { eint32 tmp = str.FindLast("<"); if(tmp >= 0 && str.Length() - tmp < strDocStart.Length()) { nLeave = str.Length() - tmp; } else { nLeave = 0; } continue; } foundDocEnd = false; } eint32 endOffset = str.FindFirst(strDocEnd, offset); if(endOffset >= 0) { endOffset += strDocEnd.Length(); foundDocEnd = true; nLeave = 0; } else { eint32 tmp = str.FindLast("<"); if(tmp >= 0 && str.Length() - tmp < strDocEnd.Length()) { nLeave = str.Length() - tmp; } else { nLeave = 0; } } xml_buffer.Append(str.String() + offset, (endOffset >= 0 ? endOffset : str.Length()) - offset - nLeave); if(foundDocEnd) xml_buffer.Append("\n"); offset = endOffset; } if(nLeave > 0) str.CopyInto(buffer, BUFFER_SIZE, str.Length() - nLeave, nLeave); } if(foundDocEnd == false) { xml_buffer.Remove(old_length, -1); ETK_DEBUG("[%s] --- Invalid document \"%s\".", prog.Leaf(), readInPath.Path()); } } EString output_buffer; if(strcmp(style, "None") == 0) { output_buffer.Adopt(xml_buffer); } else if(strcmp(style, "DocBook") == 0) { xml_buffer.ReplaceAll("&", "&"); xml_buffer.ReplaceAll("&lt;", "<"); xml_buffer.ReplaceAll("&gt;", ">"); xml_buffer.ReplaceAll("&nbsp;", " "); xml_buffer.ReplaceAll("©", "©"); xml_buffer.ReplaceAll("®", "®"); xml_buffer.ReplaceAll("\n", "&br;"); eint32 offset = 0; while(offset >= 0 && offset < xml_buffer.Length()) { if((offset = xml_buffer.FindFirst(">", offset)) < 0) break; eint32 tmp = xml_buffer.FindFirst("<", offset); if(tmp < 0 || tmp - offset <= 1) {offset = tmp; continue;} EString str; xml_buffer.MoveInto(str, offset + 1, tmp - offset); str.ReplaceAll(" ", " "); xml_buffer.Insert(str, offset + 1); offset += str.Length() + 1; } ESimpleXmlNode node(NULL, NULL); if(etk_parse_simple_xml(xml_buffer.String(), &node) != E_OK) { ETK_OUTPUT("[%s] --- Unable to parse.\n", prog.Leaf()); exit(1); } ESimpleXmlNode *aNode = NULL; offset = 0; while(offset >= 0 && offset < node.CountNodes()) { if((offset = node.FindNode("document", offset)) < 0) break; if((aNode = node.NodeAt(offset)) == NULL) break; eint32 index = aNode->FindAttribute("lang"); const char *tmp = NULL; if(index < 0 || aNode->AttributeAt(index, &tmp) == NULL || tmp == NULL || strcmp(tmp, lang) != 0) { aNode->RemoveSelf(); delete aNode; continue; } offset++; } aNode = find_xml_node_deep(&node, "documentinfo"); if(aNode != NULL) { if(!showInfo) { ESimpleXmlNode *cNode = aNode->NodeAt(aNode->FindNode("title")); if(cNode) cNode->RemoveSelf(); ESimpleXmlNode *nNode; while((nNode = aNode->NodeAt(0)) != NULL) {nNode->RemoveSelf(); delete nNode;} if(cNode) aNode->AddNode(cNode); } aNode->RemoveSelf(); if(node.AddNode(aNode) == false) delete aNode; } foreach_xml_node(&node, NULL, docbook_foreach, NULL); if(showDebug) node.PrintToStream(); if(convert_to_docbook(&node, &output_buffer, options, lang) != E_OK) { ETK_OUTPUT("[%s] --- Unable to convert to \"DocBook\" style.\n", prog.Leaf()); exit(1); } } else { ETK_OUTPUT("[%s] --- style \"%s\" unsupport yet.\n", prog.Leaf(), style); exit(1); } if(files.ItemAt(0) == NULL || files.ItemAt(0)->String() == NULL) { for(eint32 offset = 0; offset < output_buffer.Length(); offset += BUFFER_SIZE) { EString str(output_buffer.String() + offset, BUFFER_SIZE); fprintf(stdout, "%s", str.String()); } } else { EFile writeOut(files.ItemAt(0)->String(), E_WRITE_ONLY | E_CREATE_FILE | E_ERASE_FILE); if(writeOut.InitCheck() != E_OK) { ETK_OUTPUT("[%s] --- Unable to write \"%s\".\n", prog.Leaf(), files.ItemAt(0)->String()); exit(1); } else { for(eint32 offset = 0; offset < output_buffer.Length(); offset += BUFFER_SIZE) writeOut.Write(output_buffer.String() + offset, min_c(BUFFER_SIZE, output_buffer.Length() - offset)); } } return 0; }