diff --git a/src/form.cc b/src/form.cc index d849e18..eea20ae 100644 --- a/src/form.cc +++ b/src/form.cc @@ -27,6 +27,9 @@ #include "uicmd.hh" #include "dialog.hh" +#include +#include + using namespace lout; using namespace dw; using namespace dw::core; @@ -102,7 +105,7 @@ public: //BUG: for now everything is public DilloHtmlEnc content_type; char *submit_charset; - lout::misc::SimpleVector *inputs; + std::vector< std::shared_ptr< DilloHtmlInput > > inputs; int num_entry_fields; @@ -114,12 +117,12 @@ public: DilloHtmlEnc content_type, const char *charset, bool enabled); ~DilloHtmlForm (); - DilloHtmlInput *getInput (Resource *resource); - DilloHtmlInput *getRadioInput (const char *name); + std::shared_ptr< DilloHtmlInput > getInput (Resource *resource); + std::shared_ptr< DilloHtmlInput > getRadioInput (const char *name); void submit(DilloHtmlInput *active_input, EventButton *event); void reset (); void display_hiddens(bool display); - void addInput(DilloHtmlInput *input, DilloHtmlInputType type); + void addInput(std::unique_ptr< DilloHtmlInput > input, DilloHtmlInputType type); void setEnabled(bool enabled); }; @@ -278,58 +281,56 @@ static void Html_add_input(DilloHtml *html, DilloHtmlInputType type, const char *init_str, bool init_val) { _MSG("name=[%s] init_str=[%s] init_val=[%d]\n", name, init_str, init_val); - DilloHtmlInput *input = new DilloHtmlInput(type, embed, name, init_str, + auto input = std::make_unique< DilloHtmlInput >(type, embed, name, init_str, init_val); if (html->InFlags & IN_FORM) { - html->getCurrentForm()->addInput(input, type); + html->getCurrentForm()->addInput( std::move( input ), type ); } else { - int ni = html->inputs_outside_form->size(); - html->inputs_outside_form->increase(); - html->inputs_outside_form->set(ni, input); - if (html->bw->NumPendingStyleSheets > 0) { input->setEnabled(false); } + + html->inputs_outside_form.push_back( std::move( input ) ); } } /** * Find radio input by name */ -static DilloHtmlInput *Html_get_radio_input(DilloHtml *html, const char *name) +static std::shared_ptr< DilloHtmlInput > Html_get_radio_input(DilloHtml *html, const char *name) { if (name) { - lout::misc::SimpleVector* inputs; + std::vector< std::shared_ptr< DilloHtmlInput > > *inputs; if (html->InFlags & IN_FORM) - inputs = html->getCurrentForm()->inputs; + inputs = &html->getCurrentForm()->inputs; else - inputs = html->inputs_outside_form; + inputs = &html->inputs_outside_form; for (int idx = 0; idx < inputs->size(); idx++) { - DilloHtmlInput *input = inputs->get(idx); + auto input = inputs->at(idx); if (input->type == DILLO_HTML_INPUT_RADIO && input->name && !dStrAsciiCasecmp(input->name, name)) return input; } } - return NULL; + return nullptr; } /** * Get the current input if available. */ -static DilloHtmlInput *Html_get_current_input(DilloHtml *html) +static std::shared_ptr< DilloHtmlInput > Html_get_current_input(DilloHtml &html) { - lout::misc::SimpleVector* inputs; + std::vector< std::shared_ptr< DilloHtmlInput > > *inputs; - if (html->InFlags & IN_FORM) - inputs = html->getCurrentForm()->inputs; + if (html.InFlags & IN_FORM) + inputs = &html.getCurrentForm()->inputs; else - inputs = html->inputs_outside_form; + inputs = &html.inputs_outside_form; return (inputs && inputs->size() > 0) ? - inputs->get (inputs->size() - 1) : NULL; + inputs->at (inputs->size() - 1) : nullptr; } /** @@ -472,7 +473,7 @@ void Html_tag_open_input(DilloHtml *html, const char *tag, int tagsize) } else if (!dStrAsciiCasecmp(type, "radio")) { inp_type = DILLO_HTML_INPUT_RADIO; RadioButtonResource *rb_r = NULL; - DilloHtmlInput *input = Html_get_radio_input(html, name); + std::shared_ptr input = Html_get_radio_input(html, name); if (input) rb_r = (RadioButtonResource*) input->embed->getResource(); resource = factory->createRadioButtonResource(rb_r, false); @@ -695,7 +696,6 @@ void Html_tag_content_textarea(DilloHtml *html, const char *tag, int tagsize) void Html_tag_close_textarea(DilloHtml *html) { char *str; - DilloHtmlInput *input; int i; if (html->InFlags & IN_TEXTAREA && !S_TOP(html)->display_none) { @@ -718,7 +718,7 @@ void Html_tag_close_textarea(DilloHtml *html) /* The HTML3.2 spec says it can have "text and character entities". */ str = a_Html_parse_entities(html, html->Stash->str, html->Stash->len); - input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input) { input->init_str = str; ((MultiLineTextResource *)input->embed->getResource ())->setText(str); @@ -793,7 +793,7 @@ void Html_tag_close_select(DilloHtml *html) html->InFlags &= ~IN_SELECT; html->InFlags &= ~IN_OPTION; - DilloHtmlInput *input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input) { DilloHtmlSelect *select = input->select; if (input->type == DILLO_HTML_INPUT_SELECT) { @@ -822,7 +822,7 @@ void Html_tag_open_optgroup(DilloHtml *html, const char *tag, int tagsize) html->InFlags |= IN_OPTGROUP; - DilloHtmlInput *input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input && (input->type == DILLO_HTML_INPUT_SELECT || input->type == DILLO_HTML_INPUT_SEL_LIST)) { @@ -851,7 +851,7 @@ void Html_tag_close_optgroup(DilloHtml *html) html->InFlags &= ~IN_OPTION; } - DilloHtmlInput *input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input && (input->type == DILLO_HTML_INPUT_SELECT || input->type == DILLO_HTML_INPUT_SEL_LIST)) { @@ -875,7 +875,7 @@ void Html_tag_open_option(DilloHtml *html, const char *tag, int tagsize) Html_option_finish(html); html->InFlags |= IN_OPTION; - DilloHtmlInput *input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input && (input->type == DILLO_HTML_INPUT_SELECT || input->type == DILLO_HTML_INPUT_SEL_LIST)) { @@ -1000,7 +1000,6 @@ DilloHtmlForm::DilloHtmlForm (DilloHtml *html2, action = a_Url_dup(action2); content_type = content_type2; submit_charset = dStrdup(charset); - inputs = new misc::SimpleVector (4); num_entry_fields = 0; showing_hiddens = false; this->enabled = enabled; @@ -1014,9 +1013,7 @@ DilloHtmlForm::~DilloHtmlForm () { a_Url_free(action); dFree(submit_charset); - for (int j = 0; j < inputs->size(); j++) - delete inputs->get(j); - delete(inputs); + inputs.clear(); if (form_receiver) delete(form_receiver); } @@ -1027,7 +1024,7 @@ void DilloHtmlForm::eventHandler(Resource *resource, EventButton *event) if (event && (event->button == 3)) { a_UIcmd_form_popup(html->bw, html->page_url, this, showing_hiddens); } else { - DilloHtmlInput *input = getInput(resource); + auto input = getInput(resource); if (input) { input->activate (this, num_entry_fields, event); } else { @@ -1166,10 +1163,10 @@ Dstr *DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit) Dlist *values = dList_new(5); DataStr = dStr_sized_new(4096); - for (int i = 0; i < inputs->size(); i++) { - DilloHtmlInput *input = inputs->get (i); + for (int i = 0; i < inputs.size(); i++) { + auto input = inputs.at (i); Dstr *name = dStr_new(input->name); - bool is_active_submit = (input == active_submit); + bool is_active_submit = (input.get() == active_submit); int valcount; name = encodeText(char_encoder, &name); @@ -1282,10 +1279,10 @@ char *DilloHtmlForm::makeMultipartBoundary(iconv_t char_encoder, char *ret = NULL; /* fill DataStr with names, filenames, and values */ - for (int i = 0; i < inputs->size(); i++) { + for (int i = 0; i < inputs.size(); i++) { Dstr *dstr; - DilloHtmlInput *input = inputs->get (i); - bool is_active_submit = (input == active_submit); + auto input = inputs.at (i); + bool is_active_submit = (input.get() == active_submit); input->appendValuesTo(values, is_active_submit); if (input->name) { @@ -1533,9 +1530,9 @@ void DilloHtmlForm::imageInputMultipartAppend(Dstr *data, const char *boundary, */ void DilloHtmlForm::reset () { - int size = inputs->size(); + int size = inputs.size(); for (int i = 0; i < size; i++) - inputs->get(i)->reset(); + inputs.at(i)->reset(); } /** @@ -1543,9 +1540,9 @@ void DilloHtmlForm::reset () */ void DilloHtmlForm::display_hiddens(bool display) { - int size = inputs->size(); + int size = inputs.size(); for (int i = 0; i < size; i++) { - DilloHtmlInput *input = inputs->get(i); + auto input = inputs.at(i); if (input->type == DILLO_HTML_INPUT_HIDDEN) { input->embed->setDisplayed(display); } @@ -1555,20 +1552,18 @@ void DilloHtmlForm::display_hiddens(bool display) void DilloHtmlForm::setEnabled(bool enabled) { - for (int i = 0; i < inputs->size(); i++) - inputs->get(i)->setEnabled(enabled); + for (int i = 0; i < inputs.size(); i++) + inputs.at(i)->setEnabled(enabled); } /** * Add a new input. */ -void DilloHtmlForm::addInput(DilloHtmlInput *input, DilloHtmlInputType type) +void DilloHtmlForm::addInput(std::unique_ptr< DilloHtmlInput > input, DilloHtmlInputType type) { input->connectTo (form_receiver); input->setEnabled (enabled); - int ni = inputs->size (); - inputs->increase (); - inputs->set (ni,input); + inputs.push_back( std::move( input ) ); /* some stats */ if (type == DILLO_HTML_INPUT_PASSWORD || @@ -1580,29 +1575,29 @@ void DilloHtmlForm::addInput(DilloHtmlInput *input, DilloHtmlInputType type) /** * Return the input with a given resource. */ -DilloHtmlInput *DilloHtmlForm::getInput (Resource *resource) +std::shared_ptr< DilloHtmlInput > DilloHtmlForm::getInput (Resource *resource) { - for (int idx = 0; idx < inputs->size(); idx++) { - DilloHtmlInput *input = inputs->get(idx); + for (int idx = 0; idx < inputs.size(); idx++) { + auto input = inputs.at(idx); if (input->embed && resource == input->embed->getResource()) return input; } - return NULL; + return nullptr; } /** * Return a Radio input for the given name. */ -DilloHtmlInput *DilloHtmlForm::getRadioInput (const char *name) +std::shared_ptr< DilloHtmlInput > DilloHtmlForm::getRadioInput (const char *name) { - for (int idx = 0; idx < inputs->size(); idx++) { - DilloHtmlInput *input = inputs->get(idx); + for (int idx = 0; idx < inputs.size(); idx++) { + auto input = inputs.at(idx); if (input->type == DILLO_HTML_INPUT_RADIO && input->name && !dStrAsciiCasecmp(input->name, name)) return input; } - return NULL; + return nullptr; } /* @@ -1624,7 +1619,7 @@ void DilloHtmlReceiver::activate (Resource *resource) void DilloHtmlReceiver::enter (Resource *resource) { DilloHtml *html = form->html; - DilloHtmlInput *input = form->getInput(resource); + auto input = form->getInput(resource); const char *msg = ""; if ((input->type == DILLO_HTML_INPUT_SUBMIT) || @@ -2057,7 +2052,7 @@ static Embed *Html_input_image(DilloHtml *html, const char *tag, int tagsize) */ static void Html_option_finish(DilloHtml *html) { - DilloHtmlInput *input = Html_get_current_input(html); + auto input = Html_get_current_input(*html); if (input && (input->type == DILLO_HTML_INPUT_SELECT || input->type == DILLO_HTML_INPUT_SEL_LIST)) { diff --git a/src/html.cc b/src/html.cc index acfc436..a5596ff 100644 --- a/src/html.cc +++ b/src/html.cc @@ -507,7 +507,6 @@ DilloHtml::DilloHtml(BrowserWindow *p_bw, const DilloUrl *url, /* Init page-handling variables */ forms.reserve(1); - inputs_outside_form = new misc::SimpleVector (1); links.reserve(64); images = new misc::SimpleVector (16); @@ -553,9 +552,7 @@ DilloHtml::~DilloHtml() a_Html_form_delete (forms.at(i)); forms.clear(); - for (int i = 0; i < inputs_outside_form->size(); i++) - a_Html_input_delete(inputs_outside_form->get(i)); - delete(inputs_outside_form); + inputs_outside_form.clear(); for (auto &link: links) // TODO: `DilloUrl` has to become RAII... diff --git a/src/html_common.hh b/src/html_common.hh index a586562..9e877ab 100644 --- a/src/html_common.hh +++ b/src/html_common.hh @@ -214,7 +214,7 @@ public: //BUG: for now everything is public /* Variables required after parsing (for page functionality) */ /* -------------------------------------------------------------------*/ std::vector< DilloHtmlForm * > forms; - lout::misc::SimpleVector *inputs_outside_form; + std::vector< std::shared_ptr< DilloHtmlInput > > inputs_outside_form; std::vector< std::unique_ptr< DilloUrl > > links; lout::misc::SimpleVector *images; dw::ImageMapsList maps;