Re: seeking within ostringstream's
Adrian wrote:
On Sep 14, 3:34 am, James Kanze <james.ka...@gmail.com> wrote:
I'm not too sure what you're doing, but it sounds like what you
want is something like std::string or std::vector<char>, using
resize( ' ' ) as needed.
I am trying to clean up and rewrite some old code that is creating
rows for screen display that uses cursor position and printf type
args.
But that doesn't really tell me much. The obvious way to
represent rows in a screen display would be something like:
typedef std::vector< char > Row ;
std::vector< Row > display ;
Thing is I can never tell exactly how long the row could be. And the
version of snprintf on this platform returns -1 if printed string is
longer then buffer rather then what should be then length.
What I'd probably do is use something like the above. Then,
given a request to insert an int 'value' at position i, j, I'd
write something like:
std::ostringstream tmp ;
tmp << value ;
if ( display.size() <= i ) {
display.resize( i + 1 ) ;
}
Row& r = display[ i ] ;
std::string s( tmp.str() ) ;
size_t lastCol = j + s.size() ;
if ( r.size() <= lastCol ) {
r.resize( lastCol + 1, ' ' ) ;
}
std::copy( s.begin(), s.end(), r.begin() + j ) ;
I was hoping with seeking in a stringstream I could keep almost the
same arguments
Example of old stuff below:
pn1(0, row, "%-10s", HemName_of(obj));
pn1(11, row, "%-2s", Area_sh_nms[Mt_AreaFromCnum(Mt_Cnum_of(obj))]);
pn1(14, row, "%-8d", Mt_Cnum_of(obj));
pn1(23, row, "%-6s", Mt_ObjTypeName_of(obj));
pn1(30, row, "%-7s", Mt_CmdtyName_of(obj));
The only real problem will be handling the formatting strings.
And it shouldn't be too difficult to parse them, mapping them to
fmtflags, width and precision arguments for the ostringstream.
Or check out boost::format, which I think can handle about 95%
of the formatting flags---only a few of the more exotic cases
aren't handled. (The syntax of boost::format is rather wierd,
since it uses %, rather than <<, as the inserter, which rapidly
leads to fairly unreadable code. But if you're only inserting
one value at a time, and that value is always a parameter to the
function, it shouldn't be too bad. I used to have a Format
class myself; as a result of a challenge, I'd actually
implemented 100% of the printf formatting, plus the X/Open
extensions, along with all sorts of hooks to make it easy for
user defined types to do as well, if they had semantics more or
less like those of a floating point or integral number. I
stopped maintaining it sometime back, however, because I never
used it; the ostream formatting is so much more readable.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34