Skip to content

Commit

Permalink
add clang-format and etags scripts; re=indent code
Browse files Browse the repository at this point in the history
  • Loading branch information
purcaro committed Mar 2, 2014
1 parent d83bdc8 commit 3f7c91d
Show file tree
Hide file tree
Showing 19 changed files with 710 additions and 779 deletions.
5 changes: 5 additions & 0 deletions scripts/clang-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIRS="src"

find $DIRS -regex ".*\.[cChH]\(pp\)?" | xargs clang-format-3.4 -i --style=Google
14 changes: 14 additions & 0 deletions scripts/etags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python

import os

path = os.path.join(os.path.dirname(__file__), "../")
path = os.path.abspath(path)

regex = '-regex ".*\.[cChH]\(pp\)?"'
exclude = '-not -path "*/external/*" -not -name "*#*"'
cmd = 'find {p} {r} {e} -print | xargs etags '.format(p=path, e=exclude, r=regex)

print cmd

os.system(cmd)
251 changes: 123 additions & 128 deletions src/Curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,132 +35,127 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib>
#include <boost/algorithm/string.hpp>

namespace cURL
{
GlobalState::GlobalState()
{
curl_global_init(CURL_GLOBAL_ALL);
}

GlobalState::~GlobalState()
{
curl_global_cleanup();
}

EasyInterface::EasyInterface(const std::string& url, const std::string& etag, bool compress) :
m_curlHandle(curl_easy_init()),
m_url(url),
m_requestHeaders(nullptr),
m_responseData(),
m_responseHeaders()
{
m_responseHeaders.reserve(20);

curl_easy_setopt(m_curlHandle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, 0); // disable certificates
curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, m_errorBuffer); // error buffer
curl_easy_setopt(m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
curl_easy_setopt(m_curlHandle, CURLOPT_MAXREDIRS, CURL_MAX_REDIRECTS); // number of redirects to follow
//curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1); // VERBROSE

if (compress)
{
curl_easy_setopt(m_curlHandle, CURLOPT_ENCODING, "deflate"); // request zlib compression
}

// set up a minimum transfer rate - less then a 100 bytes in 5 minutes is a failure
curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_LIMIT, 100);
curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_TIME , 300);

if (!etag.empty())
{
std::string etagHeader("If-None-Match: ");
etagHeader += etag;
m_requestHeaders = curl_slist_append(m_requestHeaders, etagHeader.c_str());
curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_requestHeaders);
}

curl_easy_setopt(m_curlHandle, CURLOPT_HEADERFUNCTION, &EasyInterface::HeaderFunction);
curl_easy_setopt(m_curlHandle, CURLOPT_HEADERDATA, this);

curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, &EasyInterface::WriteFunction);
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
}

EasyInterface::~EasyInterface()
{
if (m_requestHeaders)
curl_slist_free_all(m_requestHeaders);
curl_easy_cleanup(m_curlHandle);
}

void EasyInterface::SetProgressOn()
{
curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 0);
}

const std::string& EasyInterface::GetResponseData() const
{
return m_responseData;
}

const std::vector<std::string>& EasyInterface::GetResponseHeaders() const
{
return m_responseHeaders;
}

long EasyInterface::PerformRequest()
{
return curl_easy_perform(m_curlHandle) == CURLE_OK ? GetResponseCode() : 0;
}

std::string EasyInterface::GetResponseEtag() const
{
for (std::vector<std::string>::const_iterator i = m_responseHeaders.begin(); i != m_responseHeaders.end(); ++i)
if (boost::algorithm::istarts_with(*i, "etag:"))
return i->substr(6, i->size() - 8); // subtract 2 extra bytes for the trailing CRLF
return std::string();
}

size_t EasyInterface::WriteFunction(void* data, size_t mult, size_t size, void* state)
{
const size_t dataSize = size * mult;
if (dataSize && state)
reinterpret_cast<EasyInterface*>(state)->HandleWriteData(reinterpret_cast<char*>(data), dataSize);
return dataSize;
}

size_t EasyInterface::HeaderFunction(void* data, size_t mult, size_t size, void* state)
{
const size_t dataSize = size * mult;
if (dataSize && state)
reinterpret_cast<EasyInterface*>(state)->HandleHeaderData(reinterpret_cast<char*>(data), dataSize);
return dataSize;
}

long EasyInterface::GetResponseCode() const
{
long code = 0;
return curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &code) == CURLE_OK ? code : 0;
}

void EasyInterface::HandleWriteData(const char* data, const size_t dataSize)
{
m_responseData.append(data, dataSize);
}

void EasyInterface::HandleHeaderData(const char* data, const size_t dataSize)
{
m_responseHeaders.push_back(std::string(data, dataSize));

data = m_responseHeaders.back().c_str();
if (!boost::algorithm::istarts_with(data, "content-length:"))
return;

int contentLength = std::atoi(data + 16);
if (contentLength)
m_responseData.reserve(contentLength);
}
namespace cURL {
GlobalState::GlobalState() { curl_global_init(CURL_GLOBAL_ALL); }

GlobalState::~GlobalState() { curl_global_cleanup(); }

EasyInterface::EasyInterface(const std::string& url, const std::string& etag,
bool compress)
: m_curlHandle(curl_easy_init()),
m_url(url),
m_requestHeaders(nullptr),
m_responseData(),
m_responseHeaders() {
m_responseHeaders.reserve(20);

curl_easy_setopt(m_curlHandle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER,
0); // disable certificates
curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER,
m_errorBuffer); // error buffer
curl_easy_setopt(m_curlHandle, CURLOPT_FOLLOWLOCATION,
1); // follow redirects
curl_easy_setopt(m_curlHandle, CURLOPT_MAXREDIRS,
CURL_MAX_REDIRECTS); // number of redirects to follow
// curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1); //
// VERBROSE

if (compress) {
curl_easy_setopt(m_curlHandle, CURLOPT_ENCODING,
"deflate"); // request zlib compression
}

// set up a minimum transfer rate - less then a 100 bytes in 5 minutes is a
// failure
curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_LIMIT, 100);
curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_TIME, 300);

if (!etag.empty()) {
std::string etagHeader("If-None-Match: ");
etagHeader += etag;
m_requestHeaders = curl_slist_append(m_requestHeaders, etagHeader.c_str());
curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_requestHeaders);
}

curl_easy_setopt(m_curlHandle, CURLOPT_HEADERFUNCTION,
&EasyInterface::HeaderFunction);
curl_easy_setopt(m_curlHandle, CURLOPT_HEADERDATA, this);

curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION,
&EasyInterface::WriteFunction);
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
}

EasyInterface::~EasyInterface() {
if (m_requestHeaders) curl_slist_free_all(m_requestHeaders);
curl_easy_cleanup(m_curlHandle);
}

void EasyInterface::SetProgressOn() {
curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 0);
}

const std::string& EasyInterface::GetResponseData() const {
return m_responseData;
}

const std::vector<std::string>& EasyInterface::GetResponseHeaders() const {
return m_responseHeaders;
}

long EasyInterface::PerformRequest() {
return curl_easy_perform(m_curlHandle) == CURLE_OK ? GetResponseCode() : 0;
}

std::string EasyInterface::GetResponseEtag() const {
for (std::vector<std::string>::const_iterator i = m_responseHeaders.begin();
i != m_responseHeaders.end(); ++i)
if (boost::algorithm::istarts_with(*i, "etag:"))
return i->substr(
6, i->size() - 8); // subtract 2 extra bytes for the trailing CRLF
return std::string();
}

size_t EasyInterface::WriteFunction(void* data, size_t mult, size_t size,
void* state) {
const size_t dataSize = size * mult;
if (dataSize && state)
reinterpret_cast<EasyInterface*>(state)
->HandleWriteData(reinterpret_cast<char*>(data), dataSize);
return dataSize;
}

size_t EasyInterface::HeaderFunction(void* data, size_t mult, size_t size,
void* state) {
const size_t dataSize = size * mult;
if (dataSize && state)
reinterpret_cast<EasyInterface*>(state)
->HandleHeaderData(reinterpret_cast<char*>(data), dataSize);
return dataSize;
}

long EasyInterface::GetResponseCode() const {
long code = 0;
return curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &code) ==
CURLE_OK
? code
: 0;
}

void EasyInterface::HandleWriteData(const char* data, const size_t dataSize) {
m_responseData.append(data, dataSize);
}

void EasyInterface::HandleHeaderData(const char* data, const size_t dataSize) {
m_responseHeaders.push_back(std::string(data, dataSize));

data = m_responseHeaders.back().c_str();
if (!boost::algorithm::istarts_with(data, "content-length:")) return;

int contentLength = std::atoi(data + 16);
if (contentLength) m_responseData.reserve(contentLength);
}
}
79 changes: 42 additions & 37 deletions src/Curl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,51 @@ POSSIBILITY OF SUCH DAMAGE.
#include "export_cfg.hpp"
#define nullptr 0

namespace cURL
{
static const int CURL_MAX_REDIRECTS = 10;
namespace cURL {
static const int CURL_MAX_REDIRECTS = 10;

class GlobalState
{
public:
GlobalState();
~GlobalState();
class GlobalState {
public:
GlobalState();
~GlobalState();

private:
GlobalState(const GlobalState& globalState) { } // disallow copying
};
private:
GlobalState(const GlobalState& globalState) {} // disallow copying
};

class EasyInterface
{
public:
EasyInterface(const std::string& url,const std::string& etag, bool compress = false);
~EasyInterface();
class EasyInterface {
public:
EasyInterface(const std::string& url, const std::string& etag,
bool compress = false);
~EasyInterface();

const std::string& GetResponseData() const;
const std::vector< std::string >& GetResponseHeaders() const;
long PerformRequest();
std::string GetResponseEtag() const;
void SetProgressOn();
const char* GetErrors() {return m_errorBuffer;};
private:
EasyInterface(const EasyInterface& easy) : m_curlHandle(nullptr) { } // disallow copying
static size_t WriteFunction(void* data, size_t mult, size_t size, void* state);
static size_t HeaderFunction(void* data, size_t mult, size_t size, void* state);
long GetResponseCode() const;
void HandleWriteData(const char* data, const size_t dataSize);
void HandleHeaderData(const char* data, const size_t dataSize);
private:
CURL* const m_curlHandle;
const std::string m_url;
curl_slist* m_requestHeaders;
std::string m_responseData;
std::vector< std::string > m_responseHeaders;
char m_errorBuffer[CURL_ERROR_SIZE];
};
const std::string& GetResponseData() const;
const std::vector<std::string>& GetResponseHeaders() const;
long PerformRequest();
std::string GetResponseEtag() const;
void SetProgressOn();
const char* GetErrors() {
return m_errorBuffer;
};

private:
EasyInterface(const EasyInterface& easy)
: m_curlHandle(nullptr) {} // disallow copying
static size_t WriteFunction(void* data, size_t mult, size_t size,
void* state);
static size_t HeaderFunction(void* data, size_t mult, size_t size,
void* state);
long GetResponseCode() const;
void HandleWriteData(const char* data, const size_t dataSize);
void HandleHeaderData(const char* data, const size_t dataSize);

private:
CURL* const m_curlHandle;
const std::string m_url;
curl_slist* m_requestHeaders;
std::string m_responseData;
std::vector<std::string> m_responseHeaders;
char m_errorBuffer[CURL_ERROR_SIZE];
};
}
#endif
Loading

0 comments on commit 3f7c91d

Please sign in to comment.