/[publicrep]/cgi/ezc/trunk/src/ezc.cpp
ViewVC logotype

Diff of /cgi/ezc/trunk/src/ezc.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 4 by tomek, Wed Jan 24 20:01:55 2007 UTC Revision 5 by tomek, Thu Jan 25 08:25:40 2007 UTC
# Line 52  std::string Ezc::MakeText() Line 52  std::string Ezc::MakeText()
52  {  {
53          output.clear();          output.clear();
54    
55          item_root.MakeText(output, user_functions);          item_root.MakeText(output, user_info_table);
56    
57  return output;  return output;
58  }  }
# Line 60  return output; Line 60  return output;
60    
61  void Ezc::Insert(const std::string & key, UserFunction ufunction)  void Ezc::Insert(const std::string & key, UserFunction ufunction)
62  {  {
63          user_functions.insert( std::make_pair(key, ufunction) );          UserInfo ui;
64            ui.user_function = ufunction;
65    
66            user_info_table.insert( std::make_pair(key, ui) );
67  }  }
68    
69    
# Line 209  std::string directive; Line 212  std::string directive;
212                  type = item_else;                  type = item_else;
213          }          }
214          else          else
215            if( directive == "for" )
216          {          {
217                    type = item_for;
218    
219                    ReadDirective(itext,directive);
220    
221                    if( !directive.empty() )
222                            directives.push_back(directive);
223            }
224            else
225            {
226                  directives.push_back(directive);                  directives.push_back(directive);
227                  type = item_normal;                  type = item_normal;
228          }          }
# Line 264  Item item; Line 277  Item item;
277    
278                  if( item.type == Item::item_ifany )                  if( item.type == Item::item_ifany )
279                          item_table.back()->CreateTreeReadIfany(itext);                          item_table.back()->CreateTreeReadIfany(itext);
280    
281                    if( item.type == Item::item_for )
282                            item_table.back()->CreateTreeReadFor(itext);
283          }          }
284  }  }
285    
286    bool Ezc::Item::CreateTreeReadDeleteLastEndItem()
287    {
288            if( item_table.empty() )
289                    return false;
290    
291            if( item_table.back()->LastItemType() == item_end )
292            {
293                    item_table.back()->item_table.erase(
294                                                    item_table.back()->item_table.begin() +
295                                                    item_table.back()->item_table.size() - 1 );
296    
297                    return true;
298            }
299    
300    return false;
301    }
302    
303  void Ezc::Item::CreateTreeReadIfany(const char * & itext)  void Ezc::Item::CreateTreeReadIfany(const char * & itext)
304  {  {
305  Item item;  Item item;
# Line 279  Item item; Line 311  Item item;
311          if( item_table.back()->LastItemType() == item_else )          if( item_table.back()->LastItemType() == item_else )
312          {          {
313                  // basically we don't have to erase it                  // basically we don't have to erase it
314                  item_table.back()->item_table.erase(                  CreateTreeReadDeleteLastEndItem();
                                                 item_table.back()->item_table.begin() +  
                                                 item_table.back()->item_table.size() - 1 );  
315    
316                  item.ClearTable();                  item.ClearTable();
317                  AddItem(item);                  AddItem(item);
318                  item_table.back()->CreateTree(itext);                  item_table.back()->CreateTree(itext);
319          }          }
320    
321          if( item_table.back()->LastItemType() != item_end )          if( !CreateTreeReadDeleteLastEndItem() )
322          {          {
323                  // [end] is missing                  // [end] is missing
324                  // it's probably the end of the input string                  // it's probably the end of the input string
325          }          }
326          else  }
327    
328    
329    void Ezc::Item::CreateTreeReadFor(const char * & itext)
330    {
331    Item item;
332    
333            item.type = item_container;
334            AddItem(item);
335            item_table.back()->CreateTree(itext);
336    
337            if( !CreateTreeReadDeleteLastEndItem() )
338          {          {
339                  // basically we don't have to do it                  // [end] is missing
340                  item_table.back()->item_table.erase(                  // it's probably the end of the input string
                                                 item_table.back()->item_table.begin() +  
                                                 item_table.back()->item_table.size() - 1 );  
341          }          }
342  }  }
343    
344    
345  void Ezc::Item::CreateTree(const char * & itext)  void Ezc::Item::CreateTree(const char * & itext)
346  {  {
347          if( type == item_container)          if( type == item_container)
# Line 310  void Ezc::Item::CreateTree(const char * & itext) Line 350  void Ezc::Item::CreateTree(const char * & itext)
350    
351    
352  //  //
353  void Ezc::Item::MakeTextContainer(std::string & otext, UserFunctions & user_functions)  void Ezc::Item::MakeTextContainer(std::string & otext, UserInfoTable & user_info_table)
354  {  {
355          std::vector<Item*>::iterator i = item_table.begin();          std::vector<Item*>::iterator i = item_table.begin();
356    
357          for( ; i != item_table.end() ; ++i )          for( ; i != item_table.end() ; ++i )
358                  (*i)->MakeText(otext, user_functions);                  (*i)->MakeText(otext, user_info_table);
359  }  }
360    
361    
# Line 328  void Ezc::Item::MakeTextMsgCantFind(std::string & otex Line 368  void Ezc::Item::MakeTextMsgCantFind(std::string & otex
368  }  }
369    
370    
371  void Ezc::Item::MakeTextNormal(std::string & otext, UserFunctions & user_functions)  void Ezc::Item::MakeTextNormal(std::string & otext, UserInfoTable & user_info_table)
372  {  {
373          if( directives.empty() )          if( directives.empty() )
374                  return;                  return;
375    
376          UserFunctions::iterator i = user_functions.find( directives[0] );          UserInfoTable::iterator i = user_info_table.find( directives[0] );
377    
378          if( i != user_functions.end() )          if( i != user_info_table.end() )
379          {          {
380                  EzcInfo info;                  Info info;
381                  (i->second)(info);                  info.result = false;
382                    info.iter   = 0;
383                    (i->second.user_function)(info);
384    
385                  otext += info.text;                  otext += info.text;
386          }          }
# Line 348  void Ezc::Item::MakeTextNormal(std::string & otext, Us Line 390  void Ezc::Item::MakeTextNormal(std::string & otext, Us
390          }          }
391  }  }
392    
393  void Ezc::Item::MakeTextIfany(std::string & otext, UserFunctions & user_functions)  void Ezc::Item::MakeTextIfany(std::string & otext, UserInfoTable & user_info_table)
394  {  {
395          std::vector<std::string>::iterator d = directives.begin();          std::vector<std::string>::iterator d = directives.begin();
396          int how_many_true = 0;          int how_many_true = 0;
397    
398          for( ; d != directives.end() ; ++d )          for( ; d != directives.end() ; ++d )
399          {          {
400                  UserFunctions::iterator i = user_functions.find( *d );                  UserInfoTable::iterator i = user_info_table.find( *d );
401    
402                  if( i != user_functions.end() )                  if( i != user_info_table.end() )
403                  {                  {
404                          EzcInfo info;                          Info info;
405                          (i->second)(info);                          info.result = false;
406                            info.iter   = 0;
407                            (i->second.user_function)(info);
408                                                    
409                          if( info.result )                          if( !info.text.empty() || info.result )
410                                  ++how_many_true;                                  ++how_many_true;
411                  }                  }
412                  else                  else
# Line 373  void Ezc::Item::MakeTextIfany(std::string & otext, Use Line 417  void Ezc::Item::MakeTextIfany(std::string & otext, Use
417    
418          if( how_many_true == directives.size() )          if( how_many_true == directives.size() )
419          {          {
420                  if( !item_table.empty() )                  // one element should be in the table (but we're testing)
421                          item_table[0]->MakeText(otext, user_functions);                  if( item_table.size() > 0 )
422                            item_table[0]->MakeText(otext, user_info_table);
423          }          }
424          else          else
425          {          {
426                    // second element can be (or not -- it's from [else])
427                  if( item_table.size() > 1 )                  if( item_table.size() > 1 )
428                          item_table[1]->MakeText(otext, user_functions);                          item_table[1]->MakeText(otext, user_info_table);
429          }          }
430  }  }
431    
432  void Ezc::Item::MakeText(std::string & otext, UserFunctions & user_functions)  
433    void Ezc::Item::MakeTextFor(std::string & otext, UserInfoTable & user_info_table)
434  {  {
435            if( directives.empty() )
436                    return;
437    
438    
439            UserInfoTable::iterator i = user_info_table.find( directives[0] );
440    
441            if( i != user_info_table.end() )
442            {
443                    Info info;
444                    info.result = false;
445                    info.iter   = 0;
446    
447                    i->second.iter = info.iter;
448    
449                    while( true )
450                    {
451                            (i->second.user_function)(info);
452                            ++info.iter;
453                            i->second.iter = info.iter;
454                    
455                            if( info.text.empty() && !info.result )
456                                    break;
457    
458                            if( item_table.size() > 0 )
459                                    item_table[0]->MakeText(otext, user_info_table);
460                    }
461            }
462            else
463            {
464                    MakeTextMsgCantFind(otext, directives[0]);
465            }
466    }
467    
468    
469    void Ezc::Item::MakeText(std::string & otext, UserInfoTable & user_info_table)
470    {
471          if( type == item_text )          if( type == item_text )
472          {          {
473                  otext += text;                  otext += text;
# Line 392  void Ezc::Item::MakeText(std::string & otext, UserFunc Line 475  void Ezc::Item::MakeText(std::string & otext, UserFunc
475          else          else
476          if( type == item_container )          if( type == item_container )
477          {          {
478                  MakeTextContainer(otext, user_functions);                  MakeTextContainer(otext, user_info_table);
479          }          }
480          else          else
481          if( type == item_normal )          if( type == item_normal )
482          {          {
483                  MakeTextNormal(otext, user_functions);                  MakeTextNormal(otext, user_info_table);
484          }          }
485          else          else
486          if( type == item_ifany )          if( type == item_ifany )
487          {          {
488                  MakeTextIfany(otext, user_functions);                  MakeTextIfany(otext, user_info_table);
489            }
490            else
491            if( type == item_for )
492            {
493                    MakeTextFor(otext, user_info_table);
494            }
495            else
496            if( type == item_err )
497            {
498                    otext += "<!-- ezc: wrong directive -->";
499          }          }
500    
501    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

svnadmin@ttmath.org
ViewVC Help
Powered by ViewVC 1.2.1