carnold 2005/02/03 21:35:42
Modified: tests/src/util transformer.cpp
Log:
Switch sed to use temporary files for regexs
Revision Changes Path
1.10 +95 -66 logging-log4cxx/tests/src/util/transformer.cpp
Index: transformer.cpp
===================================================================
RCS file: /home/cvs/logging-log4cxx/tests/src/util/transformer.cpp,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- transformer.cpp 29 Jan 2005 23:44:53 -0000 1.9
+++ transformer.cpp 4 Feb 2005 05:35:41 -0000 1.10
@@ -20,17 +20,18 @@
#include <apr_pools.h>
#include <apr_file_io.h>
#include <assert.h>
+#include <iostream>
using namespace log4cxx;
-using namespace log4cxx::helpers;
-
-#if !defined(APR_FOPEN_READ)
-#define APR_FOPEN_READ APR_READ
-#define APR_FOPEN_CREATE APR_CREATE
-#define APR_FOPEN_WRITE APR_WRITE
-#define APR_FOPEN_TRUNCATE APR_TRUNCATE
-#define APR_FOPEN_APPEND APR_APPEND
-#endif
+using namespace log4cxx::helpers;
+
+#if !defined(APR_FOPEN_READ)
+#define APR_FOPEN_READ APR_READ
+#define APR_FOPEN_CREATE APR_CREATE
+#define APR_FOPEN_WRITE APR_WRITE
+#define APR_FOPEN_TRUNCATE APR_TRUNCATE
+#define APR_FOPEN_APPEND APR_APPEND
+#endif
void Transformer::transform(const File& in, const File& out,
const std::vector<Filter *>& filters)
@@ -63,18 +64,18 @@
apr_pool_t* pool;
apr_status_t stat = apr_pool_create(&pool, NULL);
-
+
if (patterns.size() == 0) {
//
// fairly naive file copy code
//
//
- apr_file_t* child_out;
- apr_int32_t flags = APR_FOPEN_WRITE | APR_FOPEN_CREATE |
APR_FOPEN_TRUNCATE;
- stat = apr_file_open(&child_out, out.getOSName().c_str(),
- flags, APR_OS_DEFAULT, pool);
- assert(stat == 0);
-
+ apr_file_t* child_out;
+ apr_int32_t flags = APR_FOPEN_WRITE | APR_FOPEN_CREATE |
APR_FOPEN_TRUNCATE;
+ stat = apr_file_open(&child_out, out.getOSName().c_str(),
+ flags, APR_OS_DEFAULT, pool);
+ assert(stat == 0);
+
apr_file_t* in_file;
stat = apr_file_open(&in_file, in.getOSName().c_str(),
APR_FOPEN_READ, APR_OS_DEFAULT, pool);
@@ -89,8 +90,8 @@
stat = apr_file_write(child_out, buf, &bytesRead);
assert(stat == 0);
}
- }
- } else {
+ }
+ } else {
//
// if there are patterns, invoke sed to execute the replacements
//
@@ -98,67 +99,95 @@
apr_procattr_t* attr = NULL;
stat = apr_procattr_create(&attr, pool);
assert(stat == 0);
-
- //
- // find the program on the path
- //
+
+ //
+ // find the program on the path
+ //
stat = apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
- assert(stat == 0);
-
- //
- // build the argument list
- // using Q as regex separator on s command
+ assert(stat == 0);
+
+ //
+ // build the argument list
+ // using Q as regex separator on s command
//
const char** args = (const char**)
- apr_palloc(pool, (patterns.size()*2 + 3) * sizeof(*args));
+ apr_palloc(pool, 6 * sizeof(*args));
int i = 0;
- args[i++] = "-r";
+
+ //
+ // write the regex's to a temporary file since they
+ // may get mangled if passed as parameters
+ //
+ apr_file_t* regexFile;
+ char regexName[400];
+
+ const char* tmpDir;
+ stat = apr_temp_dir_get(&tmpDir, pool);
+
+ strcpy(regexName, tmpDir);
+ strcat(regexName, "/regexpXXXXXX");
+ stat = apr_file_mktemp(®exFile, regexName, APR_WRITE |
APR_CREATE, pool);
+ assert(stat == 0);
+
+ args[i++] = "-f";
+ const char* tmpName;
+ stat = apr_file_name_get(&tmpName, regexFile);
+ assert(stat == 0);
+ args[i++] = tmpName;
+
+ //
+ // specify the input file
+ args[i++] = in.getOSName().c_str();
+ args[i] = NULL;
+
+
std::string tmp;
for (log4cxx::Filter::PatternList::const_iterator iter =
patterns.begin();
iter != patterns.end();
iter++) {
- args[i++] = "-e";
tmp = "sQ";
tmp.append(iter->first);
tmp.append(1, 'Q');
tmp.append(iter->second);
- tmp.append("Qg");
- char* arg = (char*) apr_palloc(pool, (tmp.length() + 1) *
sizeof(char));
- strcpy(arg, tmp.c_str());
- args[i++] = arg;
+ tmp.append("Qg\n");
+ apr_file_puts(tmp.c_str(), regexFile);
}
-
- //
- // specify the input file
- args[i++] = in.getOSName().c_str();
- args[i] = NULL;
+ apr_file_close(regexFile);
+
+
+
+ //
+ // set the output stream to go to the out file
+ //
+ apr_file_t* child_out;
+ stat = apr_file_open(&child_out,
+ out.getOSName().c_str(),
+ APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE,
+ APR_OS_DEFAULT,
+ pool);
+ assert(stat == 0);
+
+ stat = apr_procattr_child_out_set(attr, child_out, NULL);
+ assert(stat == 0);
+
+ //
+ // redirect the child's error stream this processes
+ //
+ apr_file_t* child_err;
+ stat = apr_file_open_stderr(&child_err, pool);
+ assert(stat == 0);
+ stat = apr_procattr_child_err_set(attr, child_err, NULL);
+ assert(stat == 0);
+
-
- //
- // set the output stream to go to the out file
- //
- apr_file_t* child_out;
- stat = apr_file_open(&child_out,
- out.getOSName().c_str(),
- APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE,
- APR_OS_DEFAULT,
- pool);
- assert(stat == 0);
-
- stat = apr_procattr_child_out_set(attr, child_out, NULL);
- assert(stat == 0);
-
- apr_file_t* child_err;
- stat = apr_file_open(&child_err,
- "sed.err",
- APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_APPEND,
- APR_OS_DEFAULT,
- pool);
- assert(stat == 0);
-
- stat = apr_procattr_child_err_set(attr, child_err, NULL);
- assert(stat == 0);
-
+ //
+ // echo command to stdout
+ //
+ std::cout << "Running sed";
+ for(const char** arg = args; *arg != NULL; arg++) {
+ std::cout << ' ' << *arg;
+ }
+ std::cout << " > " << out.getOSName();
apr_proc_t pid;
stat = apr_proc_create(&pid,"sed", args, NULL, attr, pool);
@@ -166,7 +195,7 @@
apr_proc_wait(&pid, NULL, NULL, APR_WAIT);
}
- apr_pool_destroy(pool);
+ apr_pool_destroy(pool);
}