Repository: tesseract-ocr/tesseract
File: src/api/baseapi.cpp (C++)

Code before:
  if (stream_filelist) {
    return ProcessPagesFileList(stdin, nullptr, retry_config,
                                timeout_millisec, renderer,
                                tesseract_->tessedit_page_number);
  }

  // At this point we are officially in autodection territory.
  // That means any data in stdin must be buffered, to make it
  // seekable.
  std::string buf;
  const l_uint8 *data = nullptr;
  if (stdInput) {
    buf.assign((std::istreambuf_iterator<char>(std::cin)),
               (std::istreambuf_iterator<char>()));
    data = reinterpret_cast<const l_uint8 *>(buf.data());
  } else if (strncmp(filename, "http:", 5) == 0 ||
             strncmp(filename, "https:", 6) == 0 ) {
    // Get image or image list by URL.
#ifdef HAVE_LIBCURL
    CURL* curl = curl_easy_init();
    if (curl ==  nullptr) {
      fprintf(stderr, "Error, curl_easy_init failed\n");
      return false;
    } else {
      CURLcode curlcode;
      curlcode = curl_easy_setopt(curl, CURLOPT_URL, filename);
      ASSERT_HOST(curlcode == CURLE_OK);
      curlcode = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
      ASSERT_HOST(curlcode == CURLE_OK);
      curlcode = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
      ASSERT_HOST(curlcode == CURLE_OK);
      curlcode = curl_easy_perform(curl);
      ASSERT_HOST(curlcode == CURLE_OK);
      curl_easy_cleanup(curl);
      data = reinterpret_cast<const l_uint8 *>(buf.data());
    }
#else
    fprintf(stderr, "Error, this tesseract has no URL support\n");
    return false;
#endif
  } else {
    // Check whether the input file can be read.
    if (FILE* file = fopen(filename, "rb")) {
      fclose(file);
    } else {
      fprintf(stderr, "Error, cannot read input file %s: %s\n",
              filename, strerror(errno));
      return false;
    }
  }


Review comment (suggestion):
Capture `curlcode` by ref? Otherwise it won't be changed.

Code after:
  if (stream_filelist) {
    return ProcessPagesFileList(stdin, nullptr, retry_config,
                                timeout_millisec, renderer,
                                tesseract_->tessedit_page_number);
  }

  // At this point we are officially in autodection territory.
  // That means any data in stdin must be buffered, to make it
  // seekable.
  std::string buf;
  const l_uint8 *data = nullptr;
  if (stdInput) {
    buf.assign((std::istreambuf_iterator<char>(std::cin)),
               (std::istreambuf_iterator<char>()));
    data = reinterpret_cast<const l_uint8 *>(buf.data());
  } else if (strncmp(filename, "http:", 5) == 0 ||
             strncmp(filename, "https:", 6) == 0 ) {
    // Get image or image list by URL.
#ifdef HAVE_LIBCURL
    CURL* curl = curl_easy_init();
    if (curl ==  nullptr) {
      fprintf(stderr, "Error, curl_easy_init failed\n");
      return false;
    } else {
      CURLcode curlcode;
      auto error = [curl, curlcode](const char* function) {
        fprintf(stderr, "Error, %s failed with error %s\n", function,
                curl_easy_strerror(curlcode));
        curl_easy_cleanup(curl);
        return false;
      };
      curlcode = curl_easy_setopt(curl, CURLOPT_URL, filename);
      if (curlcode != CURLE_OK) {
        return error("curl_easy_setopt");
      }
      curlcode = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
      if (curlcode != CURLE_OK) {
        return error("curl_easy_setopt");
      }
      curlcode = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
      if (curlcode != CURLE_OK) {
        return error("curl_easy_setopt");
      }
      curlcode = curl_easy_perform(curl);
      if (curlcode != CURLE_OK) {
        return error("curl_easy_perform");
      }
      curl_easy_cleanup(curl);
      data = reinterpret_cast<const l_uint8 *>(buf.data());
    }
#else