--- parser3/src/classes/image.C 2001/04/10 13:24:49 1.2 +++ parser3/src/classes/image.C 2001/04/10 14:18:28 1.4 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: image.C,v 1.2 2001/04/10 13:24:49 paf Exp $ + $Id: image.C,v 1.4 2001/04/10 14:18:28 paf Exp $ */ #include "pa_config_includes.h" @@ -27,7 +27,7 @@ VStateless_class *image_class; class Measure_reader { public: - enum { READ_CHUNK_SIZE=10*0x400 }; // 10K + enum { READ_CHUNK_SIZE=0x400 }; // 1K typedef size_t (*Func)(void *& buf, size_t limit, void *info); Measure_reader(Func afunc, void *ainfo) : @@ -36,8 +36,12 @@ public: } size_t read(unsigned char *& buf, size_t limit) { - if(!size) // nothing left - size=(*func)(chunk, READ_CHUNK_SIZE, info); + if(offset+limit>size) // nothing left + if(offset==0 || limit==1) { // only one-byte continuations allowed + size=(*func)(chunk, READ_CHUNK_SIZE, info); + offset=0; + } else + return 0; // as if EOF if(!size) // EOF return 0; @@ -125,7 +129,7 @@ void measure_jpeg(Pool& pool, const Stri const unsigned char COM=0xFE; unsigned char *screenD_buf; - unsigned char *h_buf; + unsigned char *h_buf=0; bool flag=false; @@ -171,7 +175,7 @@ void measure_jpeg(Pool& pool, const Stri } } while(*marker!=EOI); - if(flag) { + if(flag && h_buf) { JPG_Frame& h=*reinterpret_cast(h_buf); width=bytes_to_int(h.width[0], h.width[1]); height=bytes_to_int(h.height[0], h.height[1]); @@ -268,8 +272,61 @@ static void _measure(Request& r, const S static_cast(r.self)->set(*file_name, width, height); } +struct Attrib_info { + String *tag; + Hash *skip; +}; +static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) { + Attrib_info& ai=*static_cast(info); + + if(ai.skip && ai.skip->get(key)) + return; + + Value& value=*static_cast(val); + // src="a.gif" width=123 ismap[=-1] + *ai.tag << " " << key; + if(value.is_string() || value.as_double()>=0) { + *ai.tag << "="; + if(value.is_string()) + *ai.tag << "\""; + *ai.tag << value.as_string(); + if(value.is_string()) + *ai.tag << "\""; + } +} +/// ^image.html[] +/// ^image.html[hash] +static void _html(Request& r, const String& method_name, Array *params) { + Pool& pool=r.pool(); + + String tag(pool); + tag << "(r.self)->fields(); + Hash *temporary=0; + + if(params->size()) { + if(temporary=static_cast(params->get(0))->get_hash()) { + Attrib_info attrib_info={&tag, 0}; + temporary->for_each(append_attrib_pair, &attrib_info); + } else + PTHROW(0, 0, + &method_name, + "parameter must be hash"); + } + + Attrib_info attrib_info={&tag, temporary}; + fields.for_each(append_attrib_pair, &attrib_info); + tag << ">"; + r.write_pass_lang(tag); +} + // initialize void initialize_image_class(Pool& pool, VStateless_class& vclass) { // ^image:measure[DATA] vclass.add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1); + + /// ^image.html[] + /// ^image.html[hash] + vclass.add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1); }