Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Stream problems

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Stream problems
#2975
Posted by: lea 2003-11-25 17:53:00
wxURL url(_T("myurl"));
wxInputStream *in_stream = url.GetInputStream();
wxString in;
char inp;

I have:
while((inp = in_stream->GetC()) != -1 ) {
              in += inp;
             messageField->AppendText(inp);
}

That works, but when i have:

while((inp = in_stream->GetC()) != -1 ) {
              in += inp;
}

the end of stream is not found and the loop is infinitve.

Whats wrong here?
I tried various other stream (buffered, text, etc...) and none found the stream end.
in_stream->Eof() does not work either, only -1 and only with this AppendText() call.

Thx.
Message2. Re: Stream problems
#2978
Posted by: 2003-11-25 22:23:32
Hi Lea, I think the inet streams are buggy, I've had simerlar problems. I got as far as this:

wxString UrlError(int a)
{
wxString temp;
switch(a)
{
case wxURL_NOERR:  temp =  "No error";break;  
case wxURL_SNTXERR:  temp =  "Syntax error in the URL string.";break;  
case wxURL_NOPROTO:  temp =  "Found no protocol which can get this URL.";break;  
case wxURL_NOHOST:  temp =  "An host name is required for this protocol.";break;  
case wxURL_NOPATH:  temp =  "A path is required for this protocol.";break;  
case wxURL_CONNERR:  temp =  "Connection error.";break;  
case wxURL_PROTOERR:  temp =  "An error occurred during negotiation.";break;
default: temp =  "Unknown error";break;
}
return temp;
}
int GetAsString(wxString& ac, wxString ca)
{
//    wxFileSystem* temp = new wxFileSystem();
//    wxInputStream *in_stream;
//    
//    in_stream = (temp->OpenFile(dlg.ThankYou()))->GetStream();    
//    //in_stream->Read(*out_stream);
//    wxStreamBuffer *out_stream(in_stream, 'wxStreamBuffer::read');
    
    wxURL url(ca);
    if(url.GetError() != wxURL_NOERR )
    {wxMessageBox(UrlError(url.GetError()));
    return -1;}
    wxInputStream *in_stream;
    in_stream = url.GetInputStream();
    if(url.GetError() != wxURL_NOERR )
    {wxMessageBox(UrlError(url.GetError()));
    return -1;}
    wxString buff;
    do{
    in_stream->Read(buff.GetWriteBuf(1024), 1024);
    buff.UngetWriteBuf();
    ac += buff;
    } while((in_stream->IsOk()));

    wxString blag;
    blag.sprintf("yip: %d yap: %d yop: %d", wxSTREAM_READ_ERROR , in_stream->LastError(), ac.Len());
    wxMessageBox(blag);
    if(in_stream->Eof()){
    return 1;
    }    
    
    return -1;
}
But it still has bizarre behaivour, like bailing a block early and giving an unexplained error when a '?' appears in the url, which makes no sense. I've given up on that method and am working on a library of functions using wxWindows' socket interface.
Message3. Re: Stream problems
#2979
Posted by: lea 2003-11-25 22:31:17
Hm, doesn't sound good!

I found this:
// Parse the URL
  wxURL url(urlname);
  if (url.GetError() != wxURL_NOERR)
  {
    m_text->AppendText(_("Error: couldn't parse URLn"));
    m_text->AppendText(_("=== URL test ends ===n"));
    return;
  }

  // Try to get the input stream (connects to the given URL)
  m_text->AppendText(_("Trying to establish connection...n"));
  wxYield();
  wxInputStream *data = url.GetInputStream();
  if (!data)
  {
    m_text->AppendText(_("Error: couldn't read from URLn"));
    m_text->AppendText(_("=== URL test ends ===n"));
    return;
  }

  // Print the contents type and file size
  wxString s;
  s.Printf(_("Contents type: %snFile size: %inStarting to download...n"),
             url.GetProtocol().GetContentType().c_str(),
             data->GetSize());
  m_text->AppendText(s);
  wxYield();

But this doesn't work either, the data->GetSize() is -1, but that is said in the docs.
So no help here.

I have a theory, what causes my problem.
Reading the inputstream is asyncron and does not wait with the next read for a ready stream.
So the stream is not ready when requested and goes to an illigal state.
When i put the text to screen, there is time for the stream to get ready.
But what is that for a bullshit? (sorry for the harsh words)

Any comments?
Message4. Re: Stream problems
#2980
Posted by: lea 2003-11-25 22:37:22
Ha, it worked with inputStream.IsOk().
Wired, wired...(i am nearly sure i tried that before)

Thanks Adam!
Message5. Re: Stream problems
#2981
Posted by: lea 2003-11-25 22:51:53
I tried this, but some chars (in the whole string) in the result where corrupted.

wxString in;
unsigned char buffer[128]; //same with char    
while(in_stream->IsOk()) {
     in_stream->Read(buffer, 128);
     in.Append(buffer);           
}

only this works for me

wxString in;
while(in_stream->IsOk()) {    
    in += in_stream->GetC();               
}

Are the streams really buggy?
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2023-2-4  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0