Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Is there a token function?
Is there a token function? [message #2716] Sat, 22 April 2006 22:06 Go to next message
forlano is currently offline  forlano
Messages: 1202
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

my application now needs to read a file. I've already my old C function for this purpose. Because I want to make available the source code of the new rewritten application I would like to use all the U++ facilities and get rid all C functions and its flavour.

The question: at some moment my file become formatted and ';' delimited... like this:
NAME;            COUNTRY;BIRTHDAY;G;TITLE;IDFIDE;ELOFIDE; IDNAT;ELONAT;K;ISAVAL
AAAAAAA             ;---;00.00.00;m; GM;       0;    0;       0;    0; 0;1
BBBBBBB             ;---;00.00.00;m; GM;       0;    0;       0;    0; 0;1
CCCCCCC             ;---;00.00.00;m; GM;       0;    0;       0;    0; 0;1
...

I need to extract the field beetwen consecutive ';' in each row-record. In the past I've used the strtok() function. Is there something equivalent in U++ ? I've investigated all the code available but without success.

I've found a C++ token function here
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programmi ng-HOWTO-7.html
I'll try to adapt it in U++ if there is not yet one.

Luigi

PS: BTW, I'm going to use the cin operator to read the txt file. Please stop me if there is some smartest way to do it.

PPS: I spent the afternoon to learn I/O in C++. Surfing the web I found this reference that says something (but not useful in concrete sense) about serialization
http://www.parashift.com/c++-faq-lite/serialization.html#faq -36.11
I reminded that U++ has such feature. I wonder if it can be useful in my case or it is an argument too advanced for the moment and/or not useful for such application (it needs to save data at some moment inside 2 text files).
Re: Is there a token function? [message #2717 is a reply to message #2716] Sat, 22 April 2006 22:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14105
Registered: November 2005
Ultimate Member
Well, if you are not to process 100MB+ files that are not well divided into lines and has similar lexical syntax to C, the easiest and fastest U++ way is to LoadFile into String and then use CParser.

If you DO want to process 100MB+ files and/or they are divided into lines, usually you can use similar approach, just use CParser per each single line. Or process it in any other way - in this particular case, I believe "Split" would be the best option (as you have fields separated by ';').

BTW, what you present is kind of import I have to provide for my customers quite often. Skeleton of such parser usually looks like

FileIn in(path_to_file);
in.GetLine(); // to eat "header" line
try {
  while(!in.IsEof()) {
    Vector<String> field = Split(in.GetLine(), ';');
    if(field.GetCount() != 11)
       throw CParser::Error("");
    String name = field[0];
    String coutnry = field[1];
    CParser p(field[2]);
    Date d;
    d.day = p.ReadInt();
    p.PassChar('.');
    d.month = p.ReadInt();
    p.PassChar('.');
    d.year = p.ReadInt();
    if(d.year < 20)
       d.year += 2000;
    else
    if(d.year < 100)
       d.year += 1900;
  }
}
catch(CParser::Error) {
// invalid file 
}

[Updated on: Sun, 23 April 2006 10:03] by Moderator

Report message to a moderator

Re: Is there a token function? [message #2723 is a reply to message #2717] Sun, 23 April 2006 11:50 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Why there is no String::GetLine()? or I can't find?
Re: Is there a token function? [message #2725 is a reply to message #2723] Sun, 23 April 2006 13:31 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1202
Registered: March 2006
Location: Italy
Senior Contributor
fudadmin wrote on Sun, 23 April 2006 11:50

Why there is no String::GetLine()? or I can't find?


There is. After typing
FileIn in(filename);
if I add a '.' - FileIn in(filename).; - appear a list in which I can see GetLine().

Re: Is there a token function? [message #2726 is a reply to message #2725] Sun, 23 April 2006 13:35 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
forlano wrote on Sun, 23 April 2006 12:31

fudadmin wrote on Sun, 23 April 2006 11:50

Why there is no String::GetLine()? or I can't find?


There is. After typing
FileIn in(filename);
if I add a '.' - FileIn in(filename).; - appear a list in which I can see GetLine().




String s;
s.GetLine(); ???
Re: Is there a token function? [message #2727 is a reply to message #2726] Sun, 23 April 2006 13:55 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1202
Registered: March 2006
Location: Italy
Senior Contributor
fudadmin wrote on Sun, 23 April 2006 13:35

forlano wrote on Sun, 23 April 2006 12:31

fudadmin wrote on Sun, 23 April 2006 11:50

Why there is no String::GetLine()? or I can't find?


There is. After typing
FileIn in(filename);
if I add a '.' - FileIn in(filename).; - appear a list in which I can see GetLine().




String s;
s.GetLine(); ???



FileIn in;
String s;
...
s = in.GetLine();

I'm using it to get a record from a file. Maybe you mean from stdin?
Re: Is there a token function? [message #2728 is a reply to message #2727] Sun, 23 April 2006 13:57 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
forlano wrote on Sun, 23 April 2006 12:55

fudadmin wrote on Sun, 23 April 2006 13:35

forlano wrote on Sun, 23 April 2006 12:31

fudadmin wrote on Sun, 23 April 2006 11:50

Why there is no String::GetLine()? or I can't find?


There is. After typing
FileIn in(filename);
if I add a '.' - FileIn in(filename).; - appear a list in which I can see GetLine().




String s;
s.GetLine(); ???



FileIn in;
String s;
...
s = in.GetLine();

I'm using it to get a record from a file. Maybe you mean from stdin?



What if I don't have any files?
Re: Is there a token function? [message #2729 is a reply to message #2728] Sun, 23 April 2006 13:59 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
I want to create lines, then to check and then to write to file...
Re: Is there a token function? [message #2730 is a reply to message #2729] Sun, 23 April 2006 14:19 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1202
Registered: March 2006
Location: Italy
Senior Contributor
fudadmin wrote on Sun, 23 April 2006 13:59

I want to create lines, then to check and then to write to file...


I was not able to find what you need. Perhaps it is absent Maybe you can use the standard method that work with usual "string" class:
getline(cin, my_string, '\n');

But I suspect should be an equivalent getline for U++.
Re: Is there a token function? [message #2732 is a reply to message #2728] Sun, 23 April 2006 14:50 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14105
Registered: November 2005
Ultimate Member
fudadmin wrote on Sun, 23 April 2006 07:57

forlano wrote on Sun, 23 April 2006 12:55

fudadmin wrote on Sun, 23 April 2006 13:35

forlano wrote on Sun, 23 April 2006 12:31

fudadmin wrote on Sun, 23 April 2006 11:50

Why there is no String::GetLine()? or I can't find?


There is. After typing
FileIn in(filename);
if I add a '.' - FileIn in(filename).; - appear a list in which I can see GetLine().




String s;
s.GetLine(); ???



FileIn in;
String s;
...
s = in.GetLine();

I'm using it to get a record from a file. Maybe you mean from stdin?



What if I don't have any files?



Quite unclear what is the question, however, a good oportunity to introduce fact that you can turn any sort of character data into stream using

StringStream

or

MemStream

or

MemReadStream

Mirek
Re: Is there a token function? [message #2735 is a reply to message #2732] Sun, 23 April 2006 15:33 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Yes, I'had seen that in streams. I just wanted to know why GetLine is absent in String?
Re: Is there a token function? [message #2736 is a reply to message #2735] Sun, 23 April 2006 15:36 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
P.S. It doesn't mean I'm asking for it! Smile
Re: Is there a token function? [message #2737 is a reply to message #2735] Sun, 23 April 2006 15:44 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14105
Registered: November 2005
Ultimate Member
fudadmin wrote on Sun, 23 April 2006 09:33

Yes, I'had seen that in streams. I just wanted to know why GetLine is absent in String?


What should it do? I can't imagine anything reasonable...

Mirek

[Updated on: Sun, 23 April 2006 15:45]

Report message to a moderator

Re: Is there a token function? [message #2744 is a reply to message #2737] Mon, 24 April 2006 01:01 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
luzr wrote on Sun, 23 April 2006 14:44

fudadmin wrote on Sun, 23 April 2006 09:33

Yes, I'had seen that in streams. I just wanted to know why GetLine is absent in String?


What should it do? I can't imagine anything reasonable...

Mirek


In fact, I want something like in FOX:
 /// Return num partition(s) beginning at start from a string separated by delimiters delim.
  FXString section(FXchar delim,FXint start,FXint num=1) const;

  /// Return num partition(s) beginning at start from a string separated by set of delimiters from delim of size n
  FXString section(const FXchar* delim,FXint n,FXint start,FXint num) const;

 
  FXString section(const FXchar* delim,FXint start,FXint num=1) const;

 
  FXString section(const FXString& delim,FXint start,FXint num=1) const;


I think, it's much more convienient than to have separate Vector<String> and Split for each delimiter if you work with text manipulations...

And, as I understand you have coded something similar in Uvs2 (textdiff) but that's not in the library...
Re: Is there a token function? [message #2745 is a reply to message #2744] Mon, 24 April 2006 01:14 Go to previous message
mirek is currently offline  mirek
Messages: 14105
Registered: November 2005
Ultimate Member
Split is not limited to single character delimiter - that is just a special common case.

However, it is true that whole Split idea is performance-wise suboptimal. OTOH, what you present here from Fox seems to be even less optimal....

Mirek
Previous Topic: Is there a function to remove leading white space?
Next Topic: TimeDate.cpp [BUG][FIXED]
Goto Forum:
  


Current Time: Fri Nov 01 01:09:52 CET 2024

Total time taken to generate the page: 0.03799 seconds