C++ um die Klammern zu ergänzen:
Code:
// author: daniel capelle
// 'program' texFileBrackets (quick and dirty):
// puts additional curly brackets around the latex subscript-environment
// specified by the string "_\\text{" (hardcoded)
// call accepts a directory for which all included *.tex files will be modified.
// no warranty (make a copy of the directory before usage)
#include stdafx.h
#include windows.h
#include stdio.h
#include limits.h
#include fstream
#include iostream
#include string
using namespace std;
int *envBrackets(string);
int iterReplace(string&);
int main(int argc, char *argv[]){
string FileExtension = "tex";
string fDir, line, filename, Extension;
WIN32_FIND_DATA FileInformation;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
// If the directory is not specified as a command-line argument,
// print usage.
if(argc != 2) {
cout << "\nUsage: " + string(argv[0]) + " " << endl;
return (-1);
}
// Check that the input path plus 3 is not longer than MAX_PATH.
// Three characters are for the "\*" plus NULL appended below.
if (strlen(argv[1]) > (MAX_PATH - 3)){
cout << "Directory path is too long" << endl;
return (-1);
}
cout << "\nTarget directory is " + string(argv[1]) << endl;
// Find the first file in the directory.
if (argv[1][strlen(argv[1] - 1)] != '\\'){
fDir = string(argv[1]) + '\\';
}
hFind = FindFirstFile((fDir + '*').c_str(), &FileInformation);
if(hFind != INVALID_HANDLE_VALUE){
// List all the files in the directory with specified extension.
do{
if (!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
filename = FileInformation.cFileName;
Extension = filename.substr(filename.rfind(".") + 1);
if (Extension == FileExtension){
char *infName = new char[fDir.length() + filename.length()];
strcpy(infName, (fDir + filename).c_str());
cout << infName << endl;
fstream inFile(infName, ios::in);
if(!inFile){
cout << "Error opening file" << endl;
return 1;
}
char *outfName = new char[fDir.length() + filename.length() + 1];
strcpy(outfName, (fDir + "tmp.txt").c_str());
fstream tmpFile(outfName, ios::out);
if(!tmpFile){
cout << "Error generating file" << endl;
return 2;
}
while(getline(inFile, line)){
while(iterReplace(line));
tmpFile << line << endl;
}
inFile.close();
tmpFile.close();
if(remove(infName) != 0 )
cout << "Error deleting file" << endl;
else
rename(outfName, infName);
}
}
}
while (FindNextFile(hFind, &FileInformation) != 0);
}
FindClose(hFind);
return dwError;
}
int iterReplace(string &inStr){
int pos_1 = inStr.find("_\\text{");
if (pos_1 >= 0){
int pos_2 = envBrackets(inStr.substr(pos_1))[1];
cout << inStr.substr(pos_1 + 1, pos_2) << " => " << '{' + inStr.substr(pos_1 + 1, pos_2) + '}' << endl;
inStr.replace(pos_1 + 1, pos_2, '{' + inStr.substr(pos_1 + 1, pos_2) + '}');
return 1;
}
return 0;
}
int *envBrackets(string inStr){
int tmpOpen;
int open = inStr.find("{");
int close = 0;
int env[2];
do{
tmpOpen = inStr.substr(open + 1).find("{");
open += tmpOpen + 1;
close += inStr.substr(close + 1).find("}") + 1;
}
while(tmpOpen != string::npos && open < close);
env[0] = inStr.find("{");
env[1] = close;
return env;
}
Da hier das nächste Problem mit Klammern auftritt, habe ich in den #include Anweisungen die spitzen Klamnmern ausgelassen.
Diese wurden scheinbar als html interpretiert und deshalb wurde der eingefasste Text nicht dargestellt.
Übrigens hatte ich das Problem natürlich genauso mit superscripten. Dafür im code einfach '_\\text{' durch '^\\text{' zu esetzen.
Lesezeichen