Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

prprint.cpp

Go to the documentation of this file.
00001 // +-------------------------------------------------------------------------+
00002 // |               I__n__t__e__L__i__b           0.6.10 development          |
00003 // | Copyright (c) Andrey Vikt. Stolyarov <crocodil_AT_croco.net> 2000-2007. |
00004 // |                                                                         |
00005 // | This is free software. The library part is available under              |
00006 // |                               GNU LESSER GENERAL PUBLIC LICENSE v.2.1.  |
00007 // | GNU LGPL v2.1 is found in docs/gnu_gpl2.txt,  or at  http://www.gnu.org |
00008 // |     Please see also docs/readme.txt and visit http://www.intelib.org    |
00009 // |                                                                         |
00010 // | !!! THERE IS NO WARRANTY OF ANY KIND, NEITHER EXPRESSED NOR IMPLIED !!! |
00011 // +-------------------------------------------------------------------------+
00012 
00013 
00014 
00015 
00016 #include "../sexpress/sexpress.hpp"
00017 #include "../sexpress/sstring.hpp"
00018 #include "prprint.hpp"
00019 
00020 
00021 
00022 static SString make_indent(int indent)
00023 {
00024     SString res = "";
00025     for(int i=0; i<indent; i++) res += " ";
00026     return res;
00027 }
00028 
00029 
00030 bool pretty_print(SReference ref, 
00031                   prettyprint_callback_function fun, 
00032                   void *userdata, 
00033                   int indent, int margin, int indentstep)
00034 {
00035     SString res = make_indent(indent);
00036 
00037     SExpressionCons *dp = ref.DynamicCastGetPtr<SExpressionCons>();
00038 
00039     if(!dp) {
00040         res += ref->TextRepresentation();
00041         return fun(res.c_str(), userdata);
00042     }
00043 
00044     /* well... dot pair */
00045     SString rep = ref->TextRepresentation();
00046     if(indent+rep.length()<= (unsigned int) margin) {
00047         res += rep;
00048         return fun(res.c_str(), userdata);
00049     }
00050 
00051     /* can't print at once... */
00052 
00053     res+="(";
00054     /* now check the length of the first element */
00055     rep = dp->Car()->TextRepresentation();
00056     if(indent+rep.length()+1/*brace*/ <= (unsigned int)margin) {
00057         res += rep;   
00058         ref = dp->Cdr();
00059     } 
00060     if(!fun(res.c_str(), userdata)) return false;
00061     res = "";
00062 
00063     while((dp=ref.DynamicCastGetPtr<SExpressionCons>())) {
00064         if(!pretty_print(dp->Car(), fun, userdata, 
00065                          indent+indentstep, margin, indentstep))
00066         {
00067             return false;
00068         }
00069         ref = dp->Cdr();
00070     }
00071     if(!ref.IsEmptyList()) {
00072         /* dotted list */
00073         res = make_indent(indent+indentstep+1)+".";
00074         if(!fun(res.c_str(), userdata)) return false;
00075         if(!pretty_print(ref, fun, userdata, 
00076                          indent+indentstep, margin, indentstep))
00077         {
00078             return false;
00079         }
00080     }
00081     res = make_indent(indent)+")";
00082     return fun(res.c_str(), userdata);
00083 }

Generated on Tue Dec 18 00:39:44 2007 for InteLib by  doxygen 1.4.1