updated sed docs
This commit is contained in:
parent
686f63f665
commit
4581bf1e4e
@ -17,13 +17,13 @@ over its corresponding core layer name.
|
||||
|
||||
Embedding QSEAWK involves the following steps in the simplest form:
|
||||
|
||||
- open a new awk object
|
||||
- create a new awk object
|
||||
- parse in a source script
|
||||
- open a new runtime context
|
||||
- create a new runtime context
|
||||
- execute pattern-action blocks or call a function
|
||||
- decrement the reference count of the return value
|
||||
- close the runtime context
|
||||
- close the awk object
|
||||
- destroy the runtime context
|
||||
- destroy the awk object
|
||||
|
||||
The sample below follows these steps using as many standard layer functions as
|
||||
possible for convenience sake. It simply prints *hello, world* to the console.
|
||||
|
@ -24,30 +24,30 @@ forms:
|
||||
- address - specify a single address
|
||||
- address,address - specify an address range
|
||||
- start~step - specify a starting line and a step.
|
||||
#QSE_SED_STARTSTEP enables this form.
|
||||
#QSE_SED_EXTENDEDADR enables this form.
|
||||
|
||||
An @b address is a line number, a regular expression, or a dollar sign ($)
|
||||
while a @b start and a @b step is a line number.
|
||||
An *address* is a line number, a regular expression, or a dollar sign ($)
|
||||
while a *start* and a *step* is a line number.
|
||||
|
||||
A regular expression for an address has the following form:
|
||||
- /rex/ - a regular expression @b rex is enclosed in slashes
|
||||
- \\CrexC - a regular expression @b rex is enclosed in @b \\C and @b C
|
||||
where @b C can be any character.
|
||||
- /rex/ - a regular expression *rex* is enclosed in slashes
|
||||
- \\CrexC - a regular expression *rex* is enclosed in \\C and *C*
|
||||
where *C* can be any character.
|
||||
|
||||
It treats the @b \\n sequence specially to match a newline character.
|
||||
It treats the \\n sequence specially to match a newline character.
|
||||
|
||||
Here are examples of line selectors:
|
||||
- 10 - match the 10th line
|
||||
- 10,20 - match lines from the 10th to the 20th.
|
||||
- /^[[:space:]]*$/ - match an empty line
|
||||
- /^abc$/,/^def$/ - match all lines between @b abc and @b def inclusive
|
||||
- /^abc$/,/^def$/ - match all lines between *abc* and *def* inclusive
|
||||
- 10,$ - match the 10th line down to the last line.
|
||||
- 3~4 - match every 4th line from the 3rd line.
|
||||
|
||||
Note that an address range always selects the line matching the first address
|
||||
regardless of the second address; For example, 8,6 selects the 8th line.
|
||||
|
||||
The exclamation mark @b !, when used after the line selector and before
|
||||
The exclamation mark(!), when used after the line selector and before
|
||||
the command code, negates the line selection; For example, 1! selects all
|
||||
lines except the first line.
|
||||
|
||||
@ -55,172 +55,176 @@ A command without a line selector is applied to all input lines;
|
||||
A command with a single address is applied to an input line that matches
|
||||
the address; A command with an address range is applied to all input
|
||||
lines within the range, inclusive; A command with a start and a step is
|
||||
applied to every <b>step</b>'th line starting from the line @b start.
|
||||
applied to every <b>step</b>'th line starting from the line start.
|
||||
|
||||
Here is the summary of the commands.
|
||||
|
||||
- <b># comment</b>
|
||||
### # comment ###
|
||||
The text beginning from # to the line end is ignored; # in a line following
|
||||
<b>a \\</b>, <b>i \\</b>, and <b>c \\</b> is treated literally and does not
|
||||
introduce a comment.
|
||||
|
||||
- <b>: label</b>
|
||||
### : label ###
|
||||
A label can be composed of letters, digits, periods, hyphens, and underlines.
|
||||
It remembers a target label for @b b and @b t commands and prohibits a line
|
||||
It remembers a target label for *b* and *t* commands and prohibits a line
|
||||
selector.
|
||||
|
||||
- <b>{</b>
|
||||
### { ###
|
||||
The left curly bracket forms a command group where you can nest other
|
||||
commands. It should be paired with an ending @b }.
|
||||
commands. It should be paired with an ending }.
|
||||
|
||||
- <b>q</b>
|
||||
### q ###
|
||||
Terminates the exection of commands. Upon termination, it prints the pattern
|
||||
space if #QSE_SED_QUIET is not set.
|
||||
|
||||
- <b>Q</b>
|
||||
### Q ###
|
||||
Terminates the exection of commands quietly.
|
||||
|
||||
- <b>a \\ \n text</b>
|
||||
Stores @b text into the append buffer which is printed after the pattern
|
||||
### a \\ \n text ###
|
||||
Stores *text* into the append buffer which is printed after the pattern
|
||||
space for each input line. If #QSE_SED_STRICT is on, an address range is not
|
||||
allowed for a line selector. If #QSE_SED_SAMELINE is on, the backslash and the
|
||||
text can be located on the same line without a line break.
|
||||
|
||||
- <b>i \\ \n text</b>
|
||||
Inserts @b text into an insert buffer which is printed before the pattern
|
||||
### i \\ \n text ###
|
||||
Inserts *text* into an insert buffer which is printed before the pattern
|
||||
space for each input line. If #QSE_SED_STRICT is on, an address range is not
|
||||
allowed for a line selector. If #QSE_SED_SAMELINE is on, the backslash and the
|
||||
text can be located on the same line without a line break.
|
||||
|
||||
- <b>c \\ \n text</b>
|
||||
### c \\ \n text ###
|
||||
If a single line is selected for the command (i.e. no line selector, a single
|
||||
address line selector, or a start~step line selector is specified), it changes
|
||||
the pattern space to @b text and branches to the end of commands for the line.
|
||||
the pattern space to *text* and branches to the end of commands for the line.
|
||||
If an address range is specified, it deletes the pattern space and branches
|
||||
to the end of commands for all input lines but the last, and changes pattern
|
||||
space to @b text and branches to the end of commands. If #QSE_SED_SAMELINE is
|
||||
space to *text* and branches to the end of commands. If #QSE_SED_SAMELINE is
|
||||
on, the backlash and the text can be located on the same line without a line
|
||||
break.
|
||||
|
||||
- <b>d</b>
|
||||
### d ###
|
||||
Deletes the pattern space and branches to the end of commands.
|
||||
|
||||
- <b>D</b>
|
||||
### D ###
|
||||
Deletes the first line of the pattern space. If the pattern space is emptied,
|
||||
it branches to the end of script. Otherwise, the commands from the first are
|
||||
reapplied to the current pattern space.
|
||||
|
||||
- <b>=</b>
|
||||
### = ###
|
||||
Prints the current line number. If #QSE_SED_STRICT is on, an address range is
|
||||
not allowed as a line selector.
|
||||
|
||||
- <b>p</b>
|
||||
### p ###
|
||||
Prints the pattern space.
|
||||
|
||||
- <b>P</b>
|
||||
### P ###
|
||||
Prints the first line of the pattern space.
|
||||
|
||||
- <b>l</b>
|
||||
### l ###
|
||||
Prints the pattern space in a visually unambiguous form.
|
||||
|
||||
- <b>h</b>
|
||||
### h ###
|
||||
Copies the pattern space to the hold space
|
||||
|
||||
- <b>H</b>
|
||||
### H ###
|
||||
Appends the pattern space to the hold space
|
||||
|
||||
- <b>g</b>
|
||||
### g ###
|
||||
Copies the hold space to the pattern space
|
||||
|
||||
- <b>G</b>
|
||||
### G ###
|
||||
Appends the hold space to the pattern space
|
||||
|
||||
- <b>x</b>
|
||||
### x ###
|
||||
Exchanges the pattern space and the hold space
|
||||
|
||||
- <b>n</b>
|
||||
### n ###
|
||||
Prints the pattern space and read the next line from the input stream to fill
|
||||
the pattern space.
|
||||
|
||||
- <b>N</b>
|
||||
### N ###
|
||||
Prints the pattern space and read the next line from the input stream
|
||||
to append it to the pattern space with a newline inserted.
|
||||
|
||||
- <b>b</b>
|
||||
### b ###
|
||||
Branches to the end of commands.
|
||||
|
||||
- <b>b label</b>
|
||||
Branches to @b label
|
||||
### b label ###
|
||||
Branches to *label*
|
||||
|
||||
- <b>t</b>
|
||||
### t ###
|
||||
Branches to the end of commands if substitution(s//) has been made
|
||||
successfully since the last reading of an input line or the last @b t command.
|
||||
successfully since the last reading of an input line or the last *t* command.
|
||||
|
||||
- <b>t label</b>
|
||||
Branches to @b label if substitution(s//) has been made successfully
|
||||
since the last reading of an input line or the last @b t command.
|
||||
### t label ###
|
||||
Branches to *label* if substitution(s//) has been made successfully
|
||||
since the last reading of an input line or the last *t* command.
|
||||
|
||||
- <b>r file</b>
|
||||
Reads text from @b file and prints it after printing the pattern space but
|
||||
before printing the append buffer. Failure to read @b file does not cause an
|
||||
### r file ###
|
||||
Reads text from *file* and prints it after printing the pattern space but
|
||||
before printing the append buffer. Failure to read *file* does not cause an
|
||||
error.
|
||||
|
||||
- <b>R file</b>
|
||||
Reads a line of text from @b file and prints it after printing pattern space
|
||||
but before printing the append buffer. Failure to read @b file does not cause
|
||||
### R file ###
|
||||
Reads a line of text from *file* and prints it after printing pattern space
|
||||
but before printing the append buffer. Failure to read *file* does not cause
|
||||
an error.
|
||||
|
||||
- <b>w file</b>
|
||||
Writes the pattern space to @b file
|
||||
### w file ###
|
||||
Writes the pattern space to *file*
|
||||
|
||||
- <b>W file</b>
|
||||
Writes the first line of the pattern space to @b file
|
||||
### W file ####
|
||||
Writes the first line of the pattern space to *file*
|
||||
|
||||
- <b>s/rex/repl/opts</b>
|
||||
Finds a matching substring with @b rex in pattern space and replaces it
|
||||
with @b repl. @b & in @b repl refers to the matching substring. @b opts may
|
||||
be empty; You can combine the following options into @b opts:
|
||||
- @b g replaces all occurrences of a matching substring with @b rex
|
||||
- @b number replaces the <b>number</b>'th occurrence of a matching substring
|
||||
with @b rex
|
||||
- @b p prints pattern space if a successful replacement was made
|
||||
- @b w file writes pattern space to @b file if a successful replacement
|
||||
### s/rex/repl/opts ###
|
||||
Finds a matching substring with *rex* in pattern space and replaces it
|
||||
with *repl*. An ampersand(&) in *repl* refers to the matching substring.
|
||||
*opts* may be empty; You can combine the following options into *opts*:
|
||||
|
||||
- *g* replaces all occurrences of a matching substring with *rex*
|
||||
- *number* replaces the <b>number</b>'th occurrence of a matching substring
|
||||
with *rex*
|
||||
- *p* prints pattern space if a successful replacement was made
|
||||
- *w* file writes pattern space to *file* if a successful replacement
|
||||
was made. It, if specified, should be the last option.
|
||||
|
||||
- <b>y/src/dst/</b>
|
||||
Replaces all occurrences of characters in @b src with characters in @b dst.
|
||||
@b src and @b dst must contain equal number of characters.
|
||||
### y/src/dst/ ###
|
||||
Replaces all occurrences of characters in *src* with characters in *dst*.
|
||||
*src* and *dst* must contain equal number of characters.
|
||||
|
||||
- <b>c/selector/opts</b>
|
||||
### C/selector/opts ###
|
||||
Selects characters or fields from the pattern space as specified by the
|
||||
@b selector and update the pattern space with the selected text. A selector
|
||||
*selector* and update the pattern space with the selected text. A selector
|
||||
is a comma-separated list of specifiers. A specifier is one of the followings:
|
||||
<ul>
|
||||
<li>@b d specifies the input field delimiter with the next character. e.g) d:
|
||||
<li>@b D sepcifies the output field delimiter with the next character. e.g) D;
|
||||
<li>@b c specifies a position or a range of characters to select. e.g) c23-25
|
||||
<li>@b f specifies a position or a range of fields to select. e.g) f1,f4-3
|
||||
</ul>
|
||||
@b opts may be empty; You can combine the following options into @b opts:
|
||||
<ul>
|
||||
<li>@b f folds consecutive delimiters into one.
|
||||
<li>@b w uses white spaces for a field delimiter regardless of the input
|
||||
delimiter specified in the selector.
|
||||
<li>@b d deletes the pattern space if the line is not delimited by
|
||||
the input field delimiter
|
||||
</ul>
|
||||
|
||||
In principle, this can replace the @b cut utility.
|
||||
+ *d* specifies the input field delimiter with the next character. e.g) d:
|
||||
+ *D* sepcifies the output field delimiter with the next character. e.g) D;
|
||||
+ *c* specifies a position or a range of characters to select. e.g) c23-25
|
||||
+ *f* specifies a position or a range of fields to select. e.g) f1,f4-3
|
||||
|
||||
Let's see actual examples:
|
||||
- <b>G;G;G</b>
|
||||
*opts* may be empty; You can combine the following options into *opts*:
|
||||
|
||||
+ *f* folds consecutive delimiters into one.
|
||||
+ *w* uses white spaces for a field delimiter regardless of the input
|
||||
delimiter specified in the selector.
|
||||
+ *d* deletes the pattern space if the line is not delimited by
|
||||
the input field delimiter
|
||||
|
||||
In principle, this can replace the *cut* utility with the *C* command.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Here are some samples.
|
||||
|
||||
### G;G;G ###
|
||||
Triple spaces input lines. If #QSE_SED_QUIET is on, <b>G;G;G;p</b>.
|
||||
It works because the hold space is empty unless something is copied to it.
|
||||
|
||||
- <b>$!d</b>
|
||||
### $!d ###
|
||||
Prints the last line. If #QSE_SED_QUIET is on, try <b>$p</b>.
|
||||
|
||||
- <b>1!G;h;$!d</b>
|
||||
### 1!G;h;$!d ###
|
||||
Prints input lines in the reverse order. That is, it prints the last line
|
||||
first and the first line last.
|
||||
|
||||
@ -229,10 +233,10 @@ first and the first line last.
|
||||
b
|
||||
a
|
||||
|
||||
- <b>s/[[:space:]]{2,}/ /g</b>
|
||||
Compacts whitespaces if #QSE_SED_REXBOUND is on.
|
||||
### s/[[:space:]]{2,}/ /g ###
|
||||
Compacts whitespaces if #QSE_SED_EXTENDEDREX is on.
|
||||
|
||||
- <b>C/d:,f3,1/</b>
|
||||
### C/d:,f3,1/ ###
|
||||
Prints the third field and the first field from a colon separated text.
|
||||
|
||||
$ head -5 /etc/passwd
|
||||
|
@ -4,6 +4,30 @@ QSESED Embedding Guide {#sed-embed}
|
||||
Overview
|
||||
--------
|
||||
|
||||
The QSESED library is divided into the core layer and the standard layer.
|
||||
The core layer is a skeleton implmenetation that requires various callbacks
|
||||
to be useful. The standard layer provides these callbacks in a general respect.
|
||||
|
||||
You can find core layer routines in <qse/sed/sed.h> while you can find standard
|
||||
layer routines in <qse/sed/std.h>.
|
||||
|
||||
Embedding QSESED involves the following steps in the simplest form:
|
||||
|
||||
- create a new sed object
|
||||
- compile commands
|
||||
- execute commands
|
||||
- destroy the sed object
|
||||
|
||||
The sample here shows a simple stream editor than can accepts a command string,
|
||||
and optionally an input file name and an output file name.
|
||||
|
||||
\includelineno sed01.c
|
||||
|
||||
Accessing Pattern Space
|
||||
-----------------------
|
||||
|
||||
Accessing Hold Space
|
||||
--------------------
|
||||
|
||||
Embedding In C++
|
||||
----------------
|
||||
|
@ -413,21 +413,57 @@ public:
|
||||
public:
|
||||
friend class Awk;
|
||||
|
||||
/// Console mode enumerators
|
||||
enum Mode
|
||||
{
|
||||
READ = QSE_AWK_RIO_CONSOLE_READ,
|
||||
WRITE = QSE_AWK_RIO_CONSOLE_WRITE
|
||||
READ = QSE_AWK_RIO_CONSOLE_READ, ///< open for input
|
||||
WRITE = QSE_AWK_RIO_CONSOLE_WRITE ///< open for output
|
||||
};
|
||||
|
||||
///
|
||||
/// The Handler class is an abstract class that can be
|
||||
/// implemented for customized I/O handling.
|
||||
class QSE_EXPORT Handler
|
||||
{
|
||||
public:
|
||||
virtual int open (Console& io) = 0;
|
||||
virtual int close (Console& io) = 0;
|
||||
/// The open() function is called before the initial
|
||||
/// access to the console for input and output.
|
||||
/// It must return 0 for success and -1 for failure.
|
||||
/// Upon successful opening, it can store information
|
||||
/// required using setHandle() and setUflags().
|
||||
/// The information set here is available in subsequent
|
||||
/// calls to other methods and are accessible with
|
||||
/// getHandle() and getUflags().
|
||||
virtual int open (Console& io) = 0;
|
||||
|
||||
/// The close() function is called when the console
|
||||
/// is not needed any more. It must return 0 for success
|
||||
/// and -1 for failure.
|
||||
virtual int close (Console& io) = 0;
|
||||
|
||||
/// The read() function is called when the console
|
||||
/// is read for input. It must fill the buffer \a buf with
|
||||
/// data not more than \a len characters and return the
|
||||
/// number of characters filled into the buufer. It can
|
||||
/// return 0 to indicate EOF and -1 for failure.
|
||||
virtual ssize_t read (Console& io, char_t* buf, size_t len) = 0;
|
||||
|
||||
/// The write() function is called when the console
|
||||
/// is written for output. It can write upto \a len characters
|
||||
/// available in the buffer \a buf and return the number of
|
||||
/// characters written. It can return 0 to indicate EOF and -1
|
||||
/// for failure.
|
||||
virtual ssize_t write (Console& io, const char_t* buf, size_t len) = 0;
|
||||
virtual int flush (Console& io) = 0;
|
||||
virtual int next (Console& io) = 0;
|
||||
|
||||
/// You may choose to buffer the data passed to the write()
|
||||
/// function and perform actual writing when flush() is called.
|
||||
/// It must return 0 for success and -1 for failure.
|
||||
virtual int flush (Console& io) = 0;
|
||||
|
||||
/// The next() function is called when \b nextfile or
|
||||
/// \b nextofile is executed. It must return 0 for success
|
||||
/// and -1 for failure.
|
||||
virtual int next (Console& io) = 0;
|
||||
};
|
||||
|
||||
protected:
|
||||
@ -435,7 +471,10 @@ public:
|
||||
~Console ();
|
||||
|
||||
public:
|
||||
/// The getMode() function returns if the console is
|
||||
/// opened for reading or writing.
|
||||
Mode getMode () const;
|
||||
|
||||
int setFileName (const char_t* name);
|
||||
int setFNR (long_t fnr);
|
||||
|
||||
|
@ -1,35 +1,71 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2012 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
|
||||
QSE is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
QSE is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <qse/awk/StdAwk.hpp>
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
||||
#include <string>
|
||||
|
||||
#include <locale.h>
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
typedef std::wstring String;
|
||||
#else
|
||||
typedef std::string String;
|
||||
#endif
|
||||
|
||||
typedef QSE::StdAwk StdAwk;
|
||||
typedef QSE::StdAwk::Run Run;
|
||||
typedef QSE::StdAwk::Value Value;
|
||||
|
||||
class MyAwk: public StdAwk
|
||||
{
|
||||
public:
|
||||
//
|
||||
// this class overrides console methods to use
|
||||
// string buffers for console input and output.
|
||||
//
|
||||
MyAwk () { }
|
||||
~MyAwk () { close (); }
|
||||
|
||||
void setInput (const char_t* instr)
|
||||
{
|
||||
this->input = instr;
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
|
||||
void clearOutput () { this->output.clear (); }
|
||||
const char_t* getOutput () { return this->output.c_str(); }
|
||||
|
||||
protected:
|
||||
String input; // console input buffer
|
||||
const char_t* inptr;
|
||||
const char_t* inend;
|
||||
|
||||
String output; // console output buffer
|
||||
|
||||
int openConsole (Console& io) { return 0; }
|
||||
int closeConsole (Console& io) { return 0; }
|
||||
int flushConsole (Console& io) { return 0; }
|
||||
int nextConsole (Console& io) { return 0; }
|
||||
|
||||
ssize_t readConsole (Console& io, char_t* data, size_t size)
|
||||
{
|
||||
if (this->inptr >= this->inend) return 0; // EOF
|
||||
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
|
||||
this->inptr += x;
|
||||
return x;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
|
||||
{
|
||||
try { this->output.append (data, size); }
|
||||
catch (...)
|
||||
{
|
||||
((Run*)io)->setError (QSE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static void print_error (
|
||||
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
||||
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
|
||||
{
|
||||
if (loc.line > 0 || loc.colm > 0)
|
||||
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
|
||||
@ -38,41 +74,55 @@ static void print_error (
|
||||
|
||||
}
|
||||
|
||||
static int run_awk (QSE::StdAwk& awk)
|
||||
static int run_awk (MyAwk& awk)
|
||||
{
|
||||
// sample input string
|
||||
const qse_char_t* instr = QSE_T(
|
||||
"aardvark 555-5553 1200/300 B\n"
|
||||
"alpo-net 555-3412 2400/1200/300 A\n"
|
||||
"barfly 555-7685 1200/300 A\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 C\n"
|
||||
"fooey 555-1234 2400/1200/300 B\n"
|
||||
"foot 555-6699 1200/300 B\n"
|
||||
"macfoo 555-6480 1200/300 A\n"
|
||||
"sdace 555-3430 2400/1200/300 A\n"
|
||||
"sabafoo 555-2127 1200/300 C\n");
|
||||
|
||||
// ARGV[0]
|
||||
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
|
||||
if (awk.addArgument (QSE_T("awk22")) <= -1) return -1;
|
||||
|
||||
// ARGV[1] and/or the first console input file
|
||||
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
|
||||
// prepare a script to print the second and the first column
|
||||
MyAwk::SourceString in (QSE_T("{ print $2, $1; }"));
|
||||
|
||||
const qse_char_t* script = QSE_T(
|
||||
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
|
||||
"length($0) > 0 { print $0; count++; }\n"
|
||||
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
|
||||
);
|
||||
// parse the script.
|
||||
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
|
||||
MyAwk::Value r;
|
||||
|
||||
QSE::StdAwk::SourceString in (script);
|
||||
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
|
||||
awk.setInput (instr); // locate the input string
|
||||
awk.clearOutput (); // clear the output string
|
||||
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
|
||||
|
||||
// parse the script string and deparse it to awk05.out.
|
||||
if (awk.parse (in, out) == QSE_NULL) return -1;
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
}
|
||||
|
||||
QSE::StdAwk::Value r;
|
||||
// execute the BEGIN, pattern-action, END blocks.
|
||||
return awk.loop (&r);
|
||||
return x;
|
||||
}
|
||||
|
||||
static int awk_main (int argc, qse_char_t* argv[])
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
QSE::StdAwk awk;
|
||||
MyAwk awk;
|
||||
|
||||
int ret = awk.open ();
|
||||
if (ret >= 0) ret = run_awk (awk);
|
||||
|
||||
if (ret <= -1)
|
||||
{
|
||||
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
||||
MyAwk::loc_t loc = awk.getErrorLocation();
|
||||
print_error (loc, awk.getErrorMessage());
|
||||
}
|
||||
|
||||
@ -80,26 +130,3 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
return ret;
|
||||
}
|
||||
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
char locale[100];
|
||||
UINT codepage = GetConsoleOutputCP();
|
||||
if (codepage == CP_UTF8)
|
||||
{
|
||||
/*SetConsoleOUtputCP (CP_UTF8);*/
|
||||
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf (locale, ".%u", (unsigned int)codepage);
|
||||
setlocale (LC_ALL, locale);
|
||||
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
|
||||
}
|
||||
#else
|
||||
setlocale (LC_ALL, "");
|
||||
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
|
||||
#endif
|
||||
|
||||
return qse_runmain (argc,argv,awk_main);
|
||||
}
|
||||
|
@ -1,35 +1,98 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2012 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
|
||||
QSE is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
QSE is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <qse/awk/StdAwk.hpp>
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
||||
#include <string>
|
||||
|
||||
#include <locale.h>
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
typedef std::wstring String;
|
||||
#else
|
||||
typedef std::string String;
|
||||
#endif
|
||||
|
||||
typedef QSE::StdAwk StdAwk;
|
||||
typedef QSE::StdAwk::Run Run;
|
||||
typedef QSE::StdAwk::Value Value;
|
||||
|
||||
class MyAwk: public StdAwk
|
||||
{
|
||||
public:
|
||||
//
|
||||
// this class overrides console methods to use
|
||||
// string buffers for console input and output.
|
||||
//
|
||||
MyAwk () { }
|
||||
~MyAwk () { close (); }
|
||||
|
||||
void setInput (const char_t* instr)
|
||||
{
|
||||
this->input = instr;
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
|
||||
const char_t* getOutput () { return this->output.c_str(); }
|
||||
|
||||
protected:
|
||||
String input; // console input buffer
|
||||
const char_t* inptr;
|
||||
const char_t* inend;
|
||||
|
||||
String output; // console output buffer
|
||||
|
||||
int openConsole (Console& io)
|
||||
{
|
||||
if (io.getMode() == Console::READ)
|
||||
{
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->output.clear ();
|
||||
}
|
||||
|
||||
return 1; // return open-success
|
||||
}
|
||||
|
||||
int closeConsole (Console& io)
|
||||
{
|
||||
return 0; // return success
|
||||
}
|
||||
|
||||
int flushConsole (Console& io)
|
||||
{
|
||||
// there is nothing to flush since a string buffer
|
||||
// is used for a console output. just return success.
|
||||
return 0;
|
||||
}
|
||||
int nextConsole (Console& io)
|
||||
{
|
||||
// this stripped-down awk doesn't honor the nextfile statement
|
||||
// or the nextofile statement. just return failure.
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t readConsole (Console& io, char_t* data, size_t size)
|
||||
{
|
||||
if (this->inptr >= this->inend) return 0; // EOF
|
||||
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
|
||||
this->inptr += x;
|
||||
return x;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
|
||||
{
|
||||
try { this->output.append (data, size); }
|
||||
catch (...)
|
||||
{
|
||||
((Run*)io)->setError (QSE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static void print_error (
|
||||
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
||||
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
|
||||
{
|
||||
if (loc.line > 0 || loc.colm > 0)
|
||||
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
|
||||
@ -38,94 +101,85 @@ static void print_error (
|
||||
|
||||
}
|
||||
|
||||
static int run_awk (QSE::StdAwk& awk)
|
||||
static int run_awk (MyAwk& awk)
|
||||
{
|
||||
QSE::StdAwk::Run* run;
|
||||
// sample input string
|
||||
const qse_char_t* instr = QSE_T(
|
||||
"aardvark 555-5553 1200/300 B\n"
|
||||
"alpo-net 555-3412 2400/1200/300 A\n"
|
||||
"barfly 555-7685 1200/300 A\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 C\n"
|
||||
"fooey 555-1234 2400/1200/300 B\n"
|
||||
"foot 555-6699 1200/300 B\n"
|
||||
"macfoo 555-6480 1200/300 A\n"
|
||||
"sdace 555-3430 2400/1200/300 A\n"
|
||||
"sabafoo 555-2127 1200/300 C\n");
|
||||
|
||||
const qse_char_t* script = QSE_T(
|
||||
"function add (a, b) { return a + b }\n"
|
||||
"function mul (a, b) { return a * b }\n"
|
||||
"function div (a, b) { return a / b }\n"
|
||||
"function sine (a) { return sin(a) }\n"
|
||||
);
|
||||
const qse_char_t* instr2 = QSE_T(
|
||||
"aardvark 555-5553 1200/300 A\n"
|
||||
"alpo-net 555-3412 2400/1200/300 B\n"
|
||||
"barfly 555-7685 1200/300 C\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 B\n"
|
||||
"fooey 555-1234 2400/1200/300 A\n"
|
||||
"foot 555-6699 1200/300 A\n"
|
||||
"macfoo 555-6480 1200/300 B\n"
|
||||
"sdace 555-3430 2400/1200/300 B\n"
|
||||
"sabafoo 555-2127 1200/300 A\n");
|
||||
|
||||
QSE::StdAwk::SourceString in (script);
|
||||
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
|
||||
// ARGV[0]
|
||||
if (awk.addArgument (QSE_T("awk23")) <= -1) return -1;
|
||||
|
||||
// parse the script and deparse it to awk06.out
|
||||
run = awk.parse (in, out);
|
||||
if (run == QSE_NULL) return -1;
|
||||
// prepare a string to print lines with A in the fourth column
|
||||
MyAwk::SourceString in (QSE_T("$4 == \"A\" { print $2, $1, $3; }"));
|
||||
|
||||
QSE::StdAwk::Value arg[2];
|
||||
if (arg[0].setInt (run, -20) <= -1) return -1;
|
||||
if (arg[1].setStr (run, QSE_T("51")) <= -1) return -1;
|
||||
// parse the script.
|
||||
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
|
||||
MyAwk::Value r;
|
||||
|
||||
// ret = add (-20, 51)
|
||||
QSE::StdAwk::Value ret;
|
||||
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
|
||||
awk.setInput (instr); // locate the input string
|
||||
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
|
||||
|
||||
// ret = mul (ret, 51);
|
||||
arg[0] = ret;
|
||||
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
|
||||
// ret = div (ret, 2);
|
||||
arg[0] = ret;
|
||||
if (arg[1].setFlt (run, 2) <= -1) return -1;
|
||||
if (awk.call (QSE_T("div"), &ret, arg, 2) <= -1) return -1;
|
||||
awk.setInput (instr2);
|
||||
|
||||
// output the result in various types
|
||||
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
||||
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
|
||||
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
||||
// reset the runtime context so that the next loop() method
|
||||
// is performed over a new console stream.
|
||||
if (awk.resetRunContext() == QSE_NULL) return -1;
|
||||
|
||||
// ret = sine (ret);
|
||||
arg[0] = ret;
|
||||
if (awk.call (QSE_T("sine"), &ret, arg, 1) <= -1) return -1;
|
||||
int x = awk.loop (&r);
|
||||
|
||||
// output the result in various types
|
||||
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
||||
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
|
||||
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput());
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return x;
|
||||
}
|
||||
|
||||
static int awk_main (int argc, qse_char_t* argv[])
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
QSE::StdAwk awk;
|
||||
|
||||
int ret = awk.open();
|
||||
MyAwk awk;
|
||||
|
||||
int ret = awk.open ();
|
||||
if (ret >= 0) ret = run_awk (awk);
|
||||
|
||||
if (ret <= -1)
|
||||
{
|
||||
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
||||
MyAwk::loc_t loc = awk.getErrorLocation();
|
||||
print_error (loc, awk.getErrorMessage());
|
||||
}
|
||||
|
||||
awk.close ();
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
char locale[100];
|
||||
UINT codepage = GetConsoleOutputCP();
|
||||
if (codepage == CP_UTF8)
|
||||
{
|
||||
/*SetConsoleOUtputCP (CP_UTF8);*/
|
||||
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf (locale, ".%u", (unsigned int)codepage);
|
||||
setlocale (LC_ALL, locale);
|
||||
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
|
||||
}
|
||||
#else
|
||||
setlocale (LC_ALL, "");
|
||||
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
|
||||
#endif
|
||||
return qse_runmain (argc,argv,awk_main);
|
||||
}
|
||||
|
@ -22,96 +22,14 @@
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
||||
#include <qse/cmn/str.h>
|
||||
|
||||
#include <locale.h>
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
typedef std::wstring String;
|
||||
#else
|
||||
typedef std::string String;
|
||||
#endif
|
||||
|
||||
typedef QSE::StdAwk StdAwk;
|
||||
typedef QSE::StdAwk::Run Run;
|
||||
typedef QSE::StdAwk::Value Value;
|
||||
|
||||
class MyAwk: public StdAwk
|
||||
{
|
||||
public:
|
||||
//
|
||||
// this class overrides console methods to use
|
||||
// string buffers for console input and output.
|
||||
//
|
||||
MyAwk () { }
|
||||
~MyAwk () { close (); }
|
||||
|
||||
void setInput (const char_t* instr)
|
||||
{
|
||||
this->input = instr;
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
|
||||
void clearOutput () { this->output.clear (); }
|
||||
const char_t* getOutput () { return this->output.c_str(); }
|
||||
|
||||
protected:
|
||||
String input; // console input buffer
|
||||
const char_t* inptr;
|
||||
const char_t* inend;
|
||||
|
||||
String output; // console output buffer
|
||||
|
||||
int openConsole (Console& io)
|
||||
{
|
||||
return 1; // return open-success
|
||||
}
|
||||
|
||||
int closeConsole (Console& io)
|
||||
{
|
||||
return 0; // return success
|
||||
}
|
||||
|
||||
int flushConsole (Console& io)
|
||||
{
|
||||
// there is nothing to flush since a string buffer
|
||||
// is used for a console output. just return success.
|
||||
return 0;
|
||||
}
|
||||
int nextConsole (Console& io)
|
||||
{
|
||||
// this stripped-down awk doesn't honor the nextfile statement
|
||||
// or the nextofile statement. just return success.
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t readConsole (Console& io, char_t* data, size_t size)
|
||||
{
|
||||
if (this->inptr >= this->inend) return 0; // EOF
|
||||
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
|
||||
this->inptr += x;
|
||||
return x;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
|
||||
{
|
||||
try { this->output.append (data, size); }
|
||||
catch (...)
|
||||
{
|
||||
((Run*)io)->setError (QSE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static void print_error (
|
||||
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
|
||||
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
||||
{
|
||||
if (loc.line > 0 || loc.colm > 0)
|
||||
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
|
||||
@ -120,70 +38,41 @@ static void print_error (
|
||||
|
||||
}
|
||||
|
||||
static int run_awk (MyAwk& awk)
|
||||
static int run_awk (QSE::StdAwk& awk)
|
||||
{
|
||||
// sample input string
|
||||
const qse_char_t* instr = QSE_T(
|
||||
"aardvark 555-5553 1200/300 B\n"
|
||||
"alpo-net 555-3412 2400/1200/300 A\n"
|
||||
"barfly 555-7685 1200/300 A\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 C\n"
|
||||
"fooey 555-1234 2400/1200/300 B\n"
|
||||
"foot 555-6699 1200/300 B\n"
|
||||
"macfoo 555-6480 1200/300 A\n"
|
||||
"sdace 555-3430 2400/1200/300 A\n"
|
||||
"sabafoo 555-2127 1200/300 C\n");
|
||||
|
||||
// ARGV[0]
|
||||
if (awk.addArgument (QSE_T("awk12")) <= -1) return -1;
|
||||
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
|
||||
|
||||
// prepare a script to print the second and the first column
|
||||
MyAwk::SourceString in (QSE_T("{ print $2, $1; }"));
|
||||
// ARGV[1] and/or the first console input file
|
||||
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
|
||||
|
||||
// parse the script.
|
||||
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
|
||||
MyAwk::Value r;
|
||||
const qse_char_t* script = QSE_T(
|
||||
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
|
||||
"length($0) > 0 { print $0; count++; }\n"
|
||||
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
|
||||
);
|
||||
|
||||
awk.setInput (instr); // locate the input string
|
||||
awk.clearOutput (); // clear the output string
|
||||
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
|
||||
QSE::StdAwk::SourceString in (script);
|
||||
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
|
||||
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
// parse the script string and deparse it to awk05.out.
|
||||
if (awk.parse (in, out) == QSE_NULL) return -1;
|
||||
|
||||
// prepare a string to print lines with A in the fourth column
|
||||
MyAwk::SourceString in2 (QSE_T("$4 == \"A\" { print $1; }"));
|
||||
if (awk.parse (in2, MyAwk::Source::NONE) == QSE_NULL) return -1;
|
||||
|
||||
awk.setInput (instr);
|
||||
awk.clearOutput ();
|
||||
|
||||
int x = awk.loop (&r);
|
||||
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput());
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
QSE::StdAwk::Value r;
|
||||
// execute the BEGIN, pattern-action, END blocks.
|
||||
return awk.loop (&r);
|
||||
}
|
||||
|
||||
static int awk_main (int argc, qse_char_t* argv[])
|
||||
{
|
||||
MyAwk awk;
|
||||
QSE::StdAwk awk;
|
||||
|
||||
int ret = awk.open ();
|
||||
if (ret >= 0) ret = run_awk (awk);
|
||||
|
||||
if (ret <= -1)
|
||||
{
|
||||
MyAwk::loc_t loc = awk.getErrorLocation();
|
||||
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
||||
print_error (loc, awk.getErrorMessage());
|
||||
}
|
||||
|
||||
|
@ -22,105 +22,14 @@
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
||||
#include <qse/cmn/str.h>
|
||||
|
||||
#include <locale.h>
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#if defined(QSE_CHAR_IS_WCHAR)
|
||||
typedef std::wstring String;
|
||||
#else
|
||||
typedef std::string String;
|
||||
#endif
|
||||
|
||||
typedef QSE::StdAwk StdAwk;
|
||||
typedef QSE::StdAwk::Run Run;
|
||||
typedef QSE::StdAwk::Value Value;
|
||||
|
||||
class MyAwk: public StdAwk
|
||||
{
|
||||
public:
|
||||
//
|
||||
// this class overrides console methods to use
|
||||
// string buffers for console input and output.
|
||||
//
|
||||
MyAwk () { }
|
||||
~MyAwk () { close (); }
|
||||
|
||||
void setInput (const char_t* instr)
|
||||
{
|
||||
this->input = instr;
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
|
||||
const char_t* getOutput () { return this->output.c_str(); }
|
||||
|
||||
protected:
|
||||
String input; // console input buffer
|
||||
const char_t* inptr;
|
||||
const char_t* inend;
|
||||
|
||||
String output; // console output buffer
|
||||
|
||||
int openConsole (Console& io)
|
||||
{
|
||||
if (io.getMode() == Console::READ)
|
||||
{
|
||||
this->inptr = this->input.c_str();
|
||||
this->inend = inptr + this->input.length();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->output.clear ();
|
||||
}
|
||||
|
||||
return 1; // return open-success
|
||||
}
|
||||
|
||||
int closeConsole (Console& io)
|
||||
{
|
||||
return 0; // return success
|
||||
}
|
||||
|
||||
int flushConsole (Console& io)
|
||||
{
|
||||
// there is nothing to flush since a string buffer
|
||||
// is used for a console output. just return success.
|
||||
return 0;
|
||||
}
|
||||
int nextConsole (Console& io)
|
||||
{
|
||||
// this stripped-down awk doesn't honor the nextfile statement
|
||||
// or the nextofile statement. just return failure.
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t readConsole (Console& io, char_t* data, size_t size)
|
||||
{
|
||||
if (this->inptr >= this->inend) return 0; // EOF
|
||||
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
|
||||
this->inptr += x;
|
||||
return x;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
|
||||
{
|
||||
try { this->output.append (data, size); }
|
||||
catch (...)
|
||||
{
|
||||
((Run*)io)->setError (QSE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static void print_error (
|
||||
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
|
||||
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
||||
{
|
||||
if (loc.line > 0 || loc.colm > 0)
|
||||
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
|
||||
@ -129,86 +38,73 @@ static void print_error (
|
||||
|
||||
}
|
||||
|
||||
static int run_awk (MyAwk& awk)
|
||||
static int run_awk (QSE::StdAwk& awk)
|
||||
{
|
||||
// sample input string
|
||||
const qse_char_t* instr = QSE_T(
|
||||
"aardvark 555-5553 1200/300 B\n"
|
||||
"alpo-net 555-3412 2400/1200/300 A\n"
|
||||
"barfly 555-7685 1200/300 A\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 C\n"
|
||||
"fooey 555-1234 2400/1200/300 B\n"
|
||||
"foot 555-6699 1200/300 B\n"
|
||||
"macfoo 555-6480 1200/300 A\n"
|
||||
"sdace 555-3430 2400/1200/300 A\n"
|
||||
"sabafoo 555-2127 1200/300 C\n");
|
||||
QSE::StdAwk::Run* run;
|
||||
|
||||
const qse_char_t* instr2 = QSE_T(
|
||||
"aardvark 555-5553 1200/300 A\n"
|
||||
"alpo-net 555-3412 2400/1200/300 B\n"
|
||||
"barfly 555-7685 1200/300 C\n"
|
||||
"bites 555-1675 2400/1200/300 A\n"
|
||||
"camelot 555-0542 300 C\n"
|
||||
"core 555-2912 1200/300 B\n"
|
||||
"fooey 555-1234 2400/1200/300 A\n"
|
||||
"foot 555-6699 1200/300 A\n"
|
||||
"macfoo 555-6480 1200/300 B\n"
|
||||
"sdace 555-3430 2400/1200/300 B\n"
|
||||
"sabafoo 555-2127 1200/300 A\n");
|
||||
const qse_char_t* script = QSE_T(
|
||||
"function add (a, b) { return a + b }\n"
|
||||
"function mul (a, b) { return a * b }\n"
|
||||
"function div (a, b) { return a / b }\n"
|
||||
"function sine (a) { return sin(a) }\n"
|
||||
);
|
||||
|
||||
// ARGV[0]
|
||||
if (awk.addArgument (QSE_T("awk13")) <= -1) return -1;
|
||||
QSE::StdAwk::SourceString in (script);
|
||||
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
|
||||
|
||||
// prepare a string to print lines with A in the fourth column
|
||||
MyAwk::SourceString in (QSE_T("$4 == \"A\" { print $2, $1, $3; }"));
|
||||
// parse the script and deparse it to awk06.out
|
||||
run = awk.parse (in, out);
|
||||
if (run == QSE_NULL) return -1;
|
||||
|
||||
// parse the script.
|
||||
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
|
||||
MyAwk::Value r;
|
||||
QSE::StdAwk::Value arg[2];
|
||||
if (arg[0].setInt (run, -20) <= -1) return -1;
|
||||
if (arg[1].setStr (run, QSE_T("51")) <= -1) return -1;
|
||||
|
||||
awk.setInput (instr); // locate the input string
|
||||
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
|
||||
// ret = add (-20, 51)
|
||||
QSE::StdAwk::Value ret;
|
||||
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
|
||||
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
// ret = mul (ret, 51);
|
||||
arg[0] = ret;
|
||||
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
|
||||
|
||||
awk.setInput (instr2);
|
||||
// ret = div (ret, 2);
|
||||
arg[0] = ret;
|
||||
if (arg[1].setFlt (run, 2) <= -1) return -1;
|
||||
if (awk.call (QSE_T("div"), &ret, arg, 2) <= -1) return -1;
|
||||
|
||||
// reset the runtime context so that the next loop() method
|
||||
// is performed over a new console stream.
|
||||
if (awk.resetRunContext() == QSE_NULL) return -1;
|
||||
// output the result in various types
|
||||
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
||||
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
|
||||
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
||||
|
||||
int x = awk.loop (&r);
|
||||
// ret = sine (ret);
|
||||
arg[0] = ret;
|
||||
if (awk.call (QSE_T("sine"), &ret, arg, 1) <= -1) return -1;
|
||||
|
||||
if (x >= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%s"), awk.getOutput());
|
||||
qse_printf (QSE_T("-----------------------------\n"));
|
||||
}
|
||||
}
|
||||
// output the result in various types
|
||||
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
||||
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
|
||||
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
||||
|
||||
return x;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int awk_main (int argc, qse_char_t* argv[])
|
||||
{
|
||||
MyAwk awk;
|
||||
QSE::StdAwk awk;
|
||||
|
||||
int ret = awk.open();
|
||||
|
||||
int ret = awk.open ();
|
||||
if (ret >= 0) ret = run_awk (awk);
|
||||
|
||||
if (ret <= -1)
|
||||
{
|
||||
MyAwk::loc_t loc = awk.getErrorLocation();
|
||||
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
||||
print_error (loc, awk.getErrorMessage());
|
||||
}
|
||||
|
||||
awk.close ();
|
||||
return ret;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
@ -231,6 +127,5 @@ int qse_main (int argc, qse_achar_t* argv[])
|
||||
setlocale (LC_ALL, "");
|
||||
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
|
||||
#endif
|
||||
|
||||
return qse_runmain (argc,argv,awk_main);
|
||||
}
|
||||
|
@ -23,12 +23,12 @@ sed01_LDADD = $(LDADD)
|
||||
if ENABLE_CXX
|
||||
|
||||
CXXLIB = -lqsesedxx -lqsecmnxx
|
||||
bin_PROGRAMS += sed02 sed03
|
||||
bin_PROGRAMS += sed21 sed22
|
||||
|
||||
sed02_SOURCES = sed02.cpp
|
||||
sed02_LDADD = $(CXXLIB) $(LDADD)
|
||||
sed21_SOURCES = sed21.cpp
|
||||
sed21_LDADD = $(CXXLIB) $(LDADD)
|
||||
|
||||
sed03_SOURCES = sed03.cpp
|
||||
sed03_LDADD = $(CXXLIB) $(LDADD)
|
||||
sed22_SOURCES = sed22.cpp
|
||||
sed22_LDADD = $(CXXLIB) $(LDADD)
|
||||
|
||||
endif
|
||||
|
@ -36,7 +36,7 @@ build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
@WCHAR_TRUE@@WIN32_TRUE@am__append_1 = $(UNICOWS_LIBS)
|
||||
bin_PROGRAMS = sed01$(EXEEXT) $(am__EXEEXT_1)
|
||||
@ENABLE_CXX_TRUE@am__append_2 = sed02 sed03
|
||||
@ENABLE_CXX_TRUE@am__append_2 = sed21 sed22
|
||||
subdir = samples/sed
|
||||
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
@ -52,7 +52,7 @@ mkinstalldirs = $(install_sh) -d
|
||||
CONFIG_HEADER = $(top_builddir)/include/qse/config.h
|
||||
CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
@ENABLE_CXX_TRUE@am__EXEEXT_1 = sed02$(EXEEXT) sed03$(EXEEXT)
|
||||
@ENABLE_CXX_TRUE@am__EXEEXT_1 = sed21$(EXEEXT) sed22$(EXEEXT)
|
||||
am__installdirs = "$(DESTDIR)$(bindir)"
|
||||
PROGRAMS = $(bin_PROGRAMS)
|
||||
am_sed01_OBJECTS = sed01.$(OBJEXT)
|
||||
@ -61,15 +61,15 @@ am__DEPENDENCIES_1 =
|
||||
@WCHAR_TRUE@@WIN32_TRUE@am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
|
||||
am__DEPENDENCIES_3 = $(am__DEPENDENCIES_2)
|
||||
sed01_DEPENDENCIES = $(am__DEPENDENCIES_3)
|
||||
am__sed02_SOURCES_DIST = sed02.cpp
|
||||
@ENABLE_CXX_TRUE@am_sed02_OBJECTS = sed02.$(OBJEXT)
|
||||
sed02_OBJECTS = $(am_sed02_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@sed02_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
am__sed21_SOURCES_DIST = sed21.cpp
|
||||
@ENABLE_CXX_TRUE@am_sed21_OBJECTS = sed21.$(OBJEXT)
|
||||
sed21_OBJECTS = $(am_sed21_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@sed21_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
|
||||
am__sed03_SOURCES_DIST = sed03.cpp
|
||||
@ENABLE_CXX_TRUE@am_sed03_OBJECTS = sed03.$(OBJEXT)
|
||||
sed03_OBJECTS = $(am_sed03_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@sed03_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
am__sed22_SOURCES_DIST = sed22.cpp
|
||||
@ENABLE_CXX_TRUE@am_sed22_OBJECTS = sed22.$(OBJEXT)
|
||||
sed22_OBJECTS = $(am_sed22_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@sed22_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
|
||||
DEFAULT_INCLUDES =
|
||||
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
|
||||
@ -93,9 +93,9 @@ CXXLD = $(CXX)
|
||||
CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(sed01_SOURCES) $(sed02_SOURCES) $(sed03_SOURCES)
|
||||
DIST_SOURCES = $(sed01_SOURCES) $(am__sed02_SOURCES_DIST) \
|
||||
$(am__sed03_SOURCES_DIST)
|
||||
SOURCES = $(sed01_SOURCES) $(sed21_SOURCES) $(sed22_SOURCES)
|
||||
DIST_SOURCES = $(sed01_SOURCES) $(am__sed21_SOURCES_DIST) \
|
||||
$(am__sed22_SOURCES_DIST)
|
||||
ETAGS = etags
|
||||
CTAGS = ctags
|
||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||
@ -280,10 +280,10 @@ LDADD = -lqsesed -lqsecmn $(am__append_1)
|
||||
sed01_SOURCES = sed01.c
|
||||
sed01_LDADD = $(LDADD)
|
||||
@ENABLE_CXX_TRUE@CXXLIB = -lqsesedxx -lqsecmnxx
|
||||
@ENABLE_CXX_TRUE@sed02_SOURCES = sed02.cpp
|
||||
@ENABLE_CXX_TRUE@sed02_LDADD = $(CXXLIB) $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed03_SOURCES = sed03.cpp
|
||||
@ENABLE_CXX_TRUE@sed03_LDADD = $(CXXLIB) $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed21_SOURCES = sed21.cpp
|
||||
@ENABLE_CXX_TRUE@sed21_LDADD = $(CXXLIB) $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed22_SOURCES = sed22.cpp
|
||||
@ENABLE_CXX_TRUE@sed22_LDADD = $(CXXLIB) $(LDADD)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
@ -364,12 +364,12 @@ clean-binPROGRAMS:
|
||||
sed01$(EXEEXT): $(sed01_OBJECTS) $(sed01_DEPENDENCIES) $(EXTRA_sed01_DEPENDENCIES)
|
||||
@rm -f sed01$(EXEEXT)
|
||||
$(LINK) $(sed01_OBJECTS) $(sed01_LDADD) $(LIBS)
|
||||
sed02$(EXEEXT): $(sed02_OBJECTS) $(sed02_DEPENDENCIES) $(EXTRA_sed02_DEPENDENCIES)
|
||||
@rm -f sed02$(EXEEXT)
|
||||
$(CXXLINK) $(sed02_OBJECTS) $(sed02_LDADD) $(LIBS)
|
||||
sed03$(EXEEXT): $(sed03_OBJECTS) $(sed03_DEPENDENCIES) $(EXTRA_sed03_DEPENDENCIES)
|
||||
@rm -f sed03$(EXEEXT)
|
||||
$(CXXLINK) $(sed03_OBJECTS) $(sed03_LDADD) $(LIBS)
|
||||
sed21$(EXEEXT): $(sed21_OBJECTS) $(sed21_DEPENDENCIES) $(EXTRA_sed21_DEPENDENCIES)
|
||||
@rm -f sed21$(EXEEXT)
|
||||
$(CXXLINK) $(sed21_OBJECTS) $(sed21_LDADD) $(LIBS)
|
||||
sed22$(EXEEXT): $(sed22_OBJECTS) $(sed22_DEPENDENCIES) $(EXTRA_sed22_DEPENDENCIES)
|
||||
@rm -f sed22$(EXEEXT)
|
||||
$(CXXLINK) $(sed22_OBJECTS) $(sed22_LDADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
@ -378,8 +378,8 @@ distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed01.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed02.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed03.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed21.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed22.Po@am__quote@
|
||||
|
||||
.c.o:
|
||||
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
|
@ -1,23 +1,3 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2012 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
|
||||
QSE is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
QSE is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <qse/sed/std.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
||||
@ -75,7 +55,7 @@ int qse_main (int argc, qse_achar_t* argv[])
|
||||
UINT codepage = GetConsoleOutputCP();
|
||||
if (codepage == CP_UTF8)
|
||||
{
|
||||
/*SetConsoleOUtputCP (CP_UTF8);*/
|
||||
/*SetConsoleOutputCP (CP_UTF8);*/
|
||||
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
||||
}
|
||||
else
|
||||
|
@ -1,23 +1,3 @@
|
||||
/**
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2012 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
|
||||
QSE is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
QSE is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <qse/sed/StdSed.hpp>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/mbwc.h>
|
Loading…
x
Reference in New Issue
Block a user