<commit_before>/*****************************************************************************
 * zsh.cpp: create zsh completion rule for vlc
 *****************************************************************************
 * Copyright © 2005-2011 the VideoLAN team
 *
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
            Rafaël Carré <funman@videolanorg>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <map>
#include <string>
#include <sstream>

#include <vlc/vlc.h>
#include <vlc_common.h>
#include <vlc_modules.h>
#include <vlc_plugin.h>
#include "../src/modules/modules.h" /* evil hack */

typedef std::pair<std::string, std::string> mpair;
typedef std::multimap<std::string, std::string> mumap;
mumap capabilities;

typedef std::pair<int, std::string> mcpair;
typedef std::multimap<int, std::string> mcmap;
mcmap categories;

static void ReplaceChars(char *str)
{
    if (str) {
        char *parser;
        while ((parser = strchr(str, ':'))) *parser=';' ;
        while ((parser = strchr(str, '"'))) *parser='\'' ;
        while ((parser = strchr(str, '`'))) *parser='\'' ;
    }
}

static void PrintOption(const module_config_t *item, const std::string &opt,
                        const std::string &excl, const std::string &args)
{
    char *longtext = item->psz_longtext;
    char *text = item->psz_text;
    char i_short = item->i_short;
    ReplaceChars(longtext);
    ReplaceChars(text);

    if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
        longtext = text;

    printf("  \"");

    const char *args_c = args.empty() ? "" : "=";
    if (i_short) {
        printf("(-%c", i_short);

        if (!excl.empty())
            printf("%s", excl.c_str());

        printf(")--%s%s[%s]", opt.c_str(), args_c, text);

        if (!args.empty())
            printf(":%s:%s", longtext, args.c_str());

        printf("\"\\\n  \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
    } else {
        if (!excl.empty())
            printf("(%s)", excl.c_str());
        printf("--%s", opt.c_str());
        if (!excl.empty())
            printf("%s", args_c);
    }

    printf("[%s]", text);
    if (!args.empty())
        printf( ":%s:%s", longtext, args.c_str());
    puts( "\"\\");
}

static void ParseOption(const module_config_t *item)
{
    std::string excl, args;
    std::string list;
    std::pair<mcmap::iterator, mcmap::iterator> range;
    std::pair<mumap::iterator, mumap::iterator> range_mod;

    if (item->b_removed)
        return;

    switch(item->i_type)
    {
    case CONFIG_ITEM_MODULE:
        range_mod = capabilities.equal_range(item->psz_type);
        args = "(" + (*range_mod.first).second;
        while (range_mod.first++ != range_mod.second)
            args += " " + range_mod.first->second;
        args += ")";
    break;

    case CONFIG_ITEM_MODULE_CAT:
        range = categories.equal_range(item->min.i);
        args = "(" + (*range.first).second;
        while (range.first++ != range.second)
            args += " " + range.first->second;
        args += ")";
    break;

    case CONFIG_ITEM_MODULE_LIST_CAT:
        range = categories.equal_range(item->min.i);
        args = std::string("_values -s , ") + item->psz_name;
        while (range.first != range.second)
            args += " '*" + range.first++->second + "'";
    break;

    case CONFIG_ITEM_LOADFILE:
    case CONFIG_ITEM_SAVEFILE:
        args = "_files";
        break;
    case CONFIG_ITEM_DIRECTORY:
        args = "_files -/";
        break;

    case CONFIG_ITEM_STRING:
    case CONFIG_ITEM_INTEGER:
        if (item->list_count == 0)
            break;

        for (int i = 0; i < item->list_count; i++) {
            std::string val;
            if (item->list_text) {
                const char *text = item->list_text[i];
                if (item->i_type == CONFIG_ITEM_INTEGER) {
                    std::stringstream s;
                    s << item->list.i[i];
                    val = s.str() + "\\:\\\"" + text;
                } else {
                    if (!item->list.psz[i] || !text)
                        continue;
                    val = item->list.psz[i] + std::string("\\:\\\"") + text;
                }
            } else
                val = std::string("\\\"") + item->list.psz[i];

            list = val + "\\\" " + list;
        }

        if (item->list_text)
            args = std::string("((") + list + "))";
        else
            args = std::string("(") + list + ")";

        break;

    case CONFIG_ITEM_BOOL:
        excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
        PrintOption(item, item->psz_name,                       excl, args);

        excl = std::string("--no") + item->psz_name + " --"    + item->psz_name;
        PrintOption(item, std::string("no-") + item->psz_name,  excl, args);

        excl = std::string("--no-")+ item->psz_name + " --"  + item->psz_name;
        PrintOption(item, std::string("no") + item->psz_name,   excl, args);
        return;

    case CONFIG_ITEM_KEY:
    case CONFIG_SECTION:
    case CONFIG_ITEM_FLOAT:
    default:
        break;
    }

    PrintOption(item, item->psz_name, "", args);
}

static void PrintModule(const module_t *mod)
{
    const char *name = mod->pp_shortcuts[0];
    if (!strcmp(name, "main"))
        return;

    if (mod->psz_capability)
        capabilities.insert(mpair(mod->psz_capability, name));

    module_config_t *max = &mod->p_config[mod->i_config_items];
    for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
        if (cfg->i_type == CONFIG_SUBCATEGORY)
            categories.insert(mcpair(cfg->value.i, name));

    if (!mod->parent)
        printf("%s ", name);
}

static void ParseModule(const module_t *mod)
{
    if (mod->parent)
        return;

    module_config_t *max = mod->p_config + mod->confsize;
    for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
        if (CONFIG_ITEM(cfg->i_type))
            ParseOption(cfg);
}

int main(int argc, const char **argv)
{
    libvlc_instance_t *libvlc = libvlc_new(argc - 1, argv + 1);
    if (!libvlc)
        return 1;

    size_t modules = 0;
    module_t **mod_list;

    mod_list = module_list_get(&modules);
    if (!mod_list || modules == 0)
        return 2;

    module_t **max = &mod_list[modules];

    puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
           "#This file is autogenerated by zsh.cpp\n"
           "typeset -A opt_args\n"
           "local context state line ret=1\n"
           "local modules\n");

    printf("vlc_modules=\"");
    for (module_t **mod = mod_list; mod < max; mod++)
        PrintModule(*mod);
    puts("\"\n");

    puts("_arguments -S -s \\");
    for (module_t **mod = mod_list; mod < max; mod++)
        ParseModule(*mod);
    puts("  \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
    puts("  \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
    puts("  \"(--help)-h[print help]\"\\");
    puts("  \"(-h)--help[print help]\"\\");
    puts("  \"(--longhelp)-H[print detailed help]\"\\");
    puts("  \"(-H)--longhelp[print detailed help]\"\\");
    puts("  \"(--list)-l[print a list of available modules]\"\\");
    puts("  \"(-l)--list[print a list of available modules]\"\\");
    puts("  \"--reset-config[reset the current config to the default values]\"\\");
    puts("  \"--config[use alternate config file]\"\\");
    puts("  \"--reset-plugins-cache[resets the current plugins cache]\"\\");
    puts("  \"--version[print version information]\"\\");
    puts("  \"*:Playlist item:->mrl\" && ret=0\n");

    puts("case $state in");
    puts("  mrl)");
    puts("    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
    puts("  ;;");
    puts("esac\n");

    puts("return ret");

    module_list_free(mod_list);
    libvlc_release(libvlc);
    return 0;
}
<commit_msg>zsh: Gracefully release libvlc on error<commit_after>/*****************************************************************************
 * zsh.cpp: create zsh completion rule for vlc
 *****************************************************************************
 * Copyright © 2005-2011 the VideoLAN team
 *
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
            Rafaël Carré <funman@videolanorg>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <map>
#include <string>
#include <sstream>

#include <vlc/vlc.h>
#include <vlc_common.h>
#include <vlc_modules.h>
#include <vlc_plugin.h>
#include "../src/modules/modules.h" /* evil hack */

typedef std::pair<std::string, std::string> mpair;
typedef std::multimap<std::string, std::string> mumap;
mumap capabilities;

typedef std::pair<int, std::string> mcpair;
typedef std::multimap<int, std::string> mcmap;
mcmap categories;

static void ReplaceChars(char *str)
{
    if (str) {
        char *parser;
        while ((parser = strchr(str, ':'))) *parser=';' ;
        while ((parser = strchr(str, '"'))) *parser='\'' ;
        while ((parser = strchr(str, '`'))) *parser='\'' ;
    }
}

static void PrintOption(const module_config_t *item, const std::string &opt,
                        const std::string &excl, const std::string &args)
{
    char *longtext = item->psz_longtext;
    char *text = item->psz_text;
    char i_short = item->i_short;
    ReplaceChars(longtext);
    ReplaceChars(text);

    if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
        longtext = text;

    printf("  \"");

    const char *args_c = args.empty() ? "" : "=";
    if (i_short) {
        printf("(-%c", i_short);

        if (!excl.empty())
            printf("%s", excl.c_str());

        printf(")--%s%s[%s]", opt.c_str(), args_c, text);

        if (!args.empty())
            printf(":%s:%s", longtext, args.c_str());

        printf("\"\\\n  \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
    } else {
        if (!excl.empty())
            printf("(%s)", excl.c_str());
        printf("--%s", opt.c_str());
        if (!excl.empty())
            printf("%s", args_c);
    }

    printf("[%s]", text);
    if (!args.empty())
        printf( ":%s:%s", longtext, args.c_str());
    puts( "\"\\");
}

static void ParseOption(const module_config_t *item)
{
    std::string excl, args;
    std::string list;
    std::pair<mcmap::iterator, mcmap::iterator> range;
    std::pair<mumap::iterator, mumap::iterator> range_mod;

    if (item->b_removed)
        return;

    switch(item->i_type)
    {
    case CONFIG_ITEM_MODULE:
        range_mod = capabilities.equal_range(item->psz_type);
        args = "(" + (*range_mod.first).second;
        while (range_mod.first++ != range_mod.second)
            args += " " + range_mod.first->second;
        args += ")";
    break;

    case CONFIG_ITEM_MODULE_CAT:
        range = categories.equal_range(item->min.i);
        args = "(" + (*range.first).second;
        while (range.first++ != range.second)
            args += " " + range.first->second;
        args += ")";
    break;

    case CONFIG_ITEM_MODULE_LIST_CAT:
        range = categories.equal_range(item->min.i);
        args = std::string("_values -s , ") + item->psz_name;
        while (range.first != range.second)
            args += " '*" + range.first++->second + "'";
    break;

    case CONFIG_ITEM_LOADFILE:
    case CONFIG_ITEM_SAVEFILE:
        args = "_files";
        break;
    case CONFIG_ITEM_DIRECTORY:
        args = "_files -/";
        break;

    case CONFIG_ITEM_STRING:
    case CONFIG_ITEM_INTEGER:
        if (item->list_count == 0)
            break;

        for (int i = 0; i < item->list_count; i++) {
            std::string val;
            if (item->list_text) {
                const char *text = item->list_text[i];
                if (item->i_type == CONFIG_ITEM_INTEGER) {
                    std::stringstream s;
                    s << item->list.i[i];
                    val = s.str() + "\\:\\\"" + text;
                } else {
                    if (!item->list.psz[i] || !text)
                        continue;
                    val = item->list.psz[i] + std::string("\\:\\\"") + text;
                }
            } else
                val = std::string("\\\"") + item->list.psz[i];

            list = val + "\\\" " + list;
        }

        if (item->list_text)
            args = std::string("((") + list + "))";
        else
            args = std::string("(") + list + ")";

        break;

    case CONFIG_ITEM_BOOL:
        excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
        PrintOption(item, item->psz_name,                       excl, args);

        excl = std::string("--no") + item->psz_name + " --"    + item->psz_name;
        PrintOption(item, std::string("no-") + item->psz_name,  excl, args);

        excl = std::string("--no-")+ item->psz_name + " --"  + item->psz_name;
        PrintOption(item, std::string("no") + item->psz_name,   excl, args);
        return;

    case CONFIG_ITEM_KEY:
    case CONFIG_SECTION:
    case CONFIG_ITEM_FLOAT:
    default:
        break;
    }

    PrintOption(item, item->psz_name, "", args);
}

static void PrintModule(const module_t *mod)
{
    const char *name = mod->pp_shortcuts[0];
    if (!strcmp(name, "main"))
        return;

    if (mod->psz_capability)
        capabilities.insert(mpair(mod->psz_capability, name));

    module_config_t *max = &mod->p_config[mod->i_config_items];
    for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
        if (cfg->i_type == CONFIG_SUBCATEGORY)
            categories.insert(mcpair(cfg->value.i, name));

    if (!mod->parent)
        printf("%s ", name);
}

static void ParseModule(const module_t *mod)
{
    if (mod->parent)
        return;

    module_config_t *max = mod->p_config + mod->confsize;
    for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
        if (CONFIG_ITEM(cfg->i_type))
            ParseOption(cfg);
}

int main(int argc, const char **argv)
{
    libvlc_instance_t *libvlc = libvlc_new(argc - 1, argv + 1);
    if (!libvlc)
        return 1;

    size_t modules = 0;
    module_t **mod_list;

    mod_list = module_list_get(&modules);
    if (!mod_list || modules == 0)
    {
        libvlc_release(libvlc);
        return 2;
    }

    module_t **max = &mod_list[modules];

    puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
           "#This file is autogenerated by zsh.cpp\n"
           "typeset -A opt_args\n"
           "local context state line ret=1\n"
           "local modules\n");

    printf("vlc_modules=\"");
    for (module_t **mod = mod_list; mod < max; mod++)
        PrintModule(*mod);
    puts("\"\n");

    puts("_arguments -S -s \\");
    for (module_t **mod = mod_list; mod < max; mod++)
        ParseModule(*mod);
    puts("  \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
    puts("  \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
    puts("  \"(--help)-h[print help]\"\\");
    puts("  \"(-h)--help[print help]\"\\");
    puts("  \"(--longhelp)-H[print detailed help]\"\\");
    puts("  \"(-H)--longhelp[print detailed help]\"\\");
    puts("  \"(--list)-l[print a list of available modules]\"\\");
    puts("  \"(-l)--list[print a list of available modules]\"\\");
    puts("  \"--reset-config[reset the current config to the default values]\"\\");
    puts("  \"--config[use alternate config file]\"\\");
    puts("  \"--reset-plugins-cache[resets the current plugins cache]\"\\");
    puts("  \"--version[print version information]\"\\");
    puts("  \"*:Playlist item:->mrl\" && ret=0\n");

    puts("case $state in");
    puts("  mrl)");
    puts("    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
    puts("  ;;");
    puts("esac\n");

    puts("return ret");

    module_list_free(mod_list);
    libvlc_release(libvlc);
    return 0;
}
<|endoftext|>


<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <net/checksum.hpp>
#include <immintrin.h>
#include <x86intrin.h>
#include <cassert>

namespace net {

uint16_t checksum(uint32_t tsum, const void* data, size_t length) noexcept
{
  const char* buffer = (const char*) data;
  int64_t sum = tsum;

  // VEX-align buffer
  while (((uintptr_t) buffer & 15) && length >= 4) {
    sum += *(uint32_t*) buffer;
    length -= 4; buffer += 4;
  }

#ifdef __AVX2__
  // run 4 32-bit adds in parallell
  union vec4 {
    __m256i  mm;
    int64_t  epi64[4];
  };
  vec4 vsum; vsum.mm = _mm256_set1_epi32(0);
  while (length >= 64)
  {
    __m128i tmp1 = _mm_load_si128((__m128i*) (buffer + 0));
    __m128i tmp2 = _mm_load_si128((__m128i*) (buffer + 16));
    __m128i tmp3 = _mm_load_si128((__m128i*) (buffer + 32));
    __m128i tmp4 = _mm_load_si128((__m128i*) (buffer + 48));

    // unpack 16x 32-bit values
    __m256i epi64_1 = _mm256_cvtepu32_epi64(tmp1);
    __m256i epi64_2 = _mm256_cvtepu32_epi64(tmp2);
    __m256i epi64_3 = _mm256_cvtepu32_epi64(tmp3);
    __m256i epi64_4 = _mm256_cvtepu32_epi64(tmp4);

    // sum 16x 32-bit values
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_1);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_2);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_3);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_4);

    length -= 64; buffer += 64;
  }
  // horizontal add
  sum += vsum.epi64[0];
  sum += vsum.epi64[1];
  sum += vsum.epi64[2];
  sum += vsum.epi64[3];
#endif

  // unrolled 8 32-bit adds
  while (length >= 32)
  {
    auto* v = (uint32_t*) buffer;
    sum += v[0];
    sum += v[1];
    sum += v[2];
    sum += v[3];
    sum += v[4];
    sum += v[5];
    sum += v[6];
    sum += v[7];
    length -= 32; buffer += 32;
  }

  while (length >= 4)
  {
    auto v = *(uint32_t*) buffer;
    sum += v;
    length -= 4; buffer += 4;
  }
  if (length & 2)
  {
    auto v = *(uint16_t*) buffer;
    sum += v;
    buffer += 2;
  }
  if (length & 1)
  {
    auto v = *(uint8_t*) buffer;
    sum += v;
  }
  // fold to 32-bit
  uint32_t a32 = sum & 0xffffffff;
  uint32_t b32 = sum >> 32;
  a32 += b32;
  if (a32 < b32) a32++;
  // fold again to 16-bit
  uint16_t a16 = a32 & 0xffff;
  uint16_t b16 = a32 >> 16;
  a16 += b16;
  if (a16 < b16) a16++;
  // return 2s complement
  return ~a16;
}

// Taken from https://tools.ietf.org/html/rfc3022#page-9
void checksum_adjust(uint8_t* chksum, const void* odata,
   int olen, const void* ndata, int nlen)
{
  assert(olen % 2 == 0 and nlen % 2 == 0);

  const auto* optr = reinterpret_cast<const uint8_t*>(odata);
  const auto* nptr = reinterpret_cast<const uint8_t*>(ndata);

  int32_t x, o32, n32;
  x=chksum[0]*256+chksum[1];
  x=~x & 0xFFFF;
  while (olen)
  {
    o32=optr[0]*256+optr[1]; optr+=2;
    x-=o32 & 0xffff;
    if (x<=0) { x--; x&=0xffff; }
    olen-=2;
  }
  while (nlen)
  {
    n32=nptr[0]*256+nptr[1]; nptr+=2;
    x+=n32 & 0xffff;
    if (x & 0x10000) { x++; x&=0xffff; }
    nlen-=2;
  }
  x=~x & 0xFFFF;
  chksum[0]=x/256; chksum[1]=x & 0xff;
}

} //< namespace net
<commit_msg>tcp: SSSE3 checksum algo added alongside AVX need -mssse3 for it to be compiled<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <net/checksum.hpp>
#include <net/util.hpp>

#if defined(__AVX2__)
 #include <immintrin.h>
#endif
#elif defined(__SSSE3__)
 #include <tmmintrin.h>
#endif

#include <cassert>
#include <common>

namespace net {

uint16_t checksum(uint32_t tsum, const void* data, size_t length) noexcept
{
  const char* buffer = (const char*) data;
  int64_t sum = tsum;
  if (UNLIKELY(length == 0))
    return 0xffff;

  if (UNLIKELY(buffer == 0))
    return 0xffff;

#if defined(__SSSE3__)
    static __m128i swap16a = _mm_setr_epi16(0x0001, 0xffff, 0x0203, 0xffff,
                                         0x0405, 0xffff, 0x0607, 0xffff);
    static __m128i swap16b = _mm_setr_epi16(0x0809, 0xffff, 0x0a0b, 0xffff,
                      0x0c0d, 0xffff, 0x0e0f, 0xffff);
    size_t count;
    __m128i zero = _mm_setzero_si128();
    __m128i suma=zero;
    __m128i sumb=zero;
    __m128i oldsum;

    //according to godbolt its sligtly better to count index than incrementing pointer
    for(count = 0; (count+64) < length; count+=64)
    {
      __m128i dblock1,dblock2;
      dblock1 = _mm_loadu_si128((__m128i *) (&buffer[count +  0]));
      dblock2 = _mm_loadu_si128((__m128i *) (&buffer[count + 16]));

      suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock1,swap16a));
      sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock1,swap16b));

      suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock2,swap16a));
      sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock2,swap16b));

      dblock1 = _mm_loadu_si128((__m128i *) (&buffer[count + 32]));
      dblock2 = _mm_loadu_si128((__m128i *) (&buffer[count + 48]));

      suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock1,swap16a));
      sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock1,swap16b));

      suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock2,swap16a));
      sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock2,swap16b));
    }
    //why are we not doing a 32 ?
    while ((count+16) <= length)
    {
      __m128i dblock;
      dblock= _mm_loadu_si128((__m128i *) (&buffer[count]));
      suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock,swap16a));
      sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock,swap16b));
      count+=16;
    }

    /*alignas(16) this can be unaligned as we most likely are accessing it unaligned anyays*/
    alignas(16) static const uint8_t shift_tab[48]={
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    };
    /* we could extend this to fast convert 4 words to LE from BE */
    alignas(16) static const uint8_t swap32[16]{
        0x03,0x02,0x01,0x00,0x80,0x80,0x80,0x80,
        0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80
    };
    int rest=16-(length-count);
    //this over reads but who cares?!
    if (LIKELY(rest != 16))
    {
       __m128i dblock;
       dblock = _mm_loadu_si128((__m128i *)(&buffer[count]));
       //shifting the data up then down gives us leading zeroes clearing out any over read data
       //the shift up shuffles the data bytewise into place
       dblock = _mm_shuffle_epi8(dblock, _mm_loadu_si128((__m128i *)(&shift_tab[16 - rest])));
       dblock = _mm_shuffle_epi8(dblock, _mm_loadu_si128((__m128i *)(&shift_tab[16 + rest])));
       suma = _mm_add_epi32(suma,_mm_shuffle_epi8(dblock,swap16a));
       sumb = _mm_add_epi32(sumb,_mm_shuffle_epi8(dblock,swap16b));
    }

     suma = _mm_add_epi32(suma, sumb);
     //add 0 and 1 to 0 and 2 and 3 to 1
     suma = _mm_hadd_epi32(suma, _mm_setzero_si128());
     //add 0 and 1 to 1 ..
     suma = _mm_hadd_epi32(suma, _mm_setzero_si128());

     oldsum = _mm_shuffle_epi8(_mm_cvtsi32_si128(tsum), _mm_loadu_si128((__m128i *)&swap32[0]));
     suma = _mm_add_epi32(suma,oldsum); //adds the old csum to this
     //fix endianess

     //extract the 32 bit sum from vector
     uint32_t vsum;
     vsum=(uint32_t) _mm_cvtsi128_si32(suma);

    //printf("Vsum + swapped(tsum) %08x\n",vsum);
    //maybe this only works if tsum is byteswapped ?
    while (vsum >>16)
    {
      vsum=(vsum & 0xFFFF)+(vsum>>16);
    }
    //allways right in this case as its allways little endian x86
    return ~net::ntohs((uint16_t)(vsum));
#elif defined(__AVX2__)
  // VEX-align buffer
  while (((uintptr_t) buffer & 15) && length >= 4) {
    sum += *(uint32_t*) buffer;
    length -= 4; buffer += 4;
  }
  // run 4 32-bit adds in parallell
  union vec4 {
    __m256i  mm;
    int64_t  epi64[4];
  };
  vec4 vsum; vsum.mm = _mm256_set1_epi32(0);
  while (length >= 64)
  {
    __m128i tmp1 = _mm_load_si128((__m128i*) (buffer + 0));
    __m128i tmp2 = _mm_load_si128((__m128i*) (buffer + 16));
    __m128i tmp3 = _mm_load_si128((__m128i*) (buffer + 32));
    __m128i tmp4 = _mm_load_si128((__m128i*) (buffer + 48));

    // unpack 16x 32-bit values
    __m256i epi64_1 = _mm256_cvtepu32_epi64(tmp1);
    __m256i epi64_2 = _mm256_cvtepu32_epi64(tmp2);
    __m256i epi64_3 = _mm256_cvtepu32_epi64(tmp3);
    __m256i epi64_4 = _mm256_cvtepu32_epi64(tmp4);

    // sum 16x 32-bit values
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_1);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_2);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_3);
    vsum.mm = _mm256_add_epi64(vsum.mm, epi64_4);

    length -= 64; buffer += 64;
  }
  // horizontal add
  sum += vsum.epi64[0];
  sum += vsum.epi64[1];
  sum += vsum.epi64[2];
  sum += vsum.epi64[3];
#endif

  // unrolled 8 32-bit adds
  while (length >= 32)
  {
    auto* v = (uint32_t*) buffer;
    sum += v[0];
    sum += v[1];
    sum += v[2];
    sum += v[3];
    sum += v[4];
    sum += v[5];
    sum += v[6];
    sum += v[7];
    length -= 32; buffer += 32;
  }

  while (length >= 4)
  {
    auto v = *(uint32_t*) buffer;
    sum += v;
    length -= 4; buffer += 4;
  }
  if (length & 2)
  {
    auto v = *(uint16_t*) buffer;
    sum += v;
    buffer += 2;
  }
  if (length & 1)
  {
    auto v = *(uint8_t*) buffer;
    sum += v;
  }
  // fold to 32-bit
  uint32_t a32 = sum & 0xffffffff;
  uint32_t b32 = sum >> 32;
  a32 += b32;
  if (a32 < b32) a32++;
  // fold again to 16-bit
  uint16_t a16 = a32 & 0xffff;
  uint16_t b16 = a32 >> 16;
  a16 += b16;
  if (a16 < b16) a16++;
  // return 2s complement
  return ~a16;
}

// Taken from https://tools.ietf.org/html/rfc3022#page-9
void checksum_adjust(uint8_t* chksum, const void* odata,
   int olen, const void* ndata, int nlen)
{
  assert(olen % 2 == 0 and nlen % 2 == 0);

  const auto* optr = reinterpret_cast<const uint8_t*>(odata);
  const auto* nptr = reinterpret_cast<const uint8_t*>(ndata);

  int32_t x, o32, n32;
  x=chksum[0]*256+chksum[1];
  x=~x & 0xFFFF;
  while (olen)
  {
    o32=optr[0]*256+optr[1]; optr+=2;
    x-=o32 & 0xffff;
    if (x<=0) { x--; x&=0xffff; }
    olen-=2;
  }
  while (nlen)
  {
    n32=nptr[0]*256+nptr[1]; nptr+=2;
    x+=n32 & 0xffff;
    if (x & 0x10000) { x++; x&=0xffff; }
    nlen-=2;
  }
  x=~x & 0xFFFF;
  chksum[0]=x/256; chksum[1]=x & 0xff;
}

} //< namespace net
<|endoftext|>


<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket_stream/socket_stream_metrics.h"

#include "base/basictypes.h"
#include "base/metrics/histogram.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::Histogram;
using base::StatisticsRecorder;

namespace net {

TEST(SocketStreamMetricsTest, Initialize) {
  if (!StatisticsRecorder::IsActive()) {
    // Create the recorder if not yet started, as SocketStreamMetrics
    // relys on the StatisticsRecorder to be present. This is useful when
    // tests are run with --gtest_filter='SocketStreamMetricsTest*'.
    static StatisticsRecorder *recorder = NULL;
    recorder = new StatisticsRecorder;
  }
}

TEST(SocketStreamMetricsTest, ProtocolType) {
  Histogram* histogram;

  // First we'll preserve the original values. We need to do this
  // as histograms can get affected by other tests. In particular,
  // SocketStreamTest and WebSocketTest can affect the histograms.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ProtocolType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
  SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
  SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
  SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss3(GURL("wss://www.example.com/"));

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ProtocolType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);  // Ensure |original| size is same as |sample|.
  sample.Subtract(original);  // Cancel the original values.
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::PROTOCOL_UNKNOWN));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
}

TEST(SocketStreamMetricsTest, ConnectionType) {
  Histogram* histogram;

  // First we'll preserve the original values.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ConnectionType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  for (int i = 0; i < 1; ++i)
    metrics.OnStartConnection();
  for (int i = 0; i < 2; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION);
  for (int i = 0; i < 3; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION);
  for (int i = 0; i < 4; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION);

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);
  sample.Subtract(original);
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::ALL_CONNECTIONS));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::TUNNEL_CONNECTION));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::SOCKS_CONNECTION));
  EXPECT_EQ(4, sample.counts(SocketStreamMetrics::SSL_CONNECTION));
}

TEST(SocketStreamMetricsTest, WireProtocolType) {
  Histogram* histogram;

  // First we'll preserve the original values.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.WireProtocolType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  for (int i = 0; i < 3; ++i)
    metrics.OnCountWireProtocolType(
        SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET);
  for (int i = 0; i < 7; ++i)
    metrics.OnCountWireProtocolType(SocketStreamMetrics::WIRE_PROTOCOL_SPDY);

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.WireProtocolType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);
  sample.Subtract(original);
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET));
  EXPECT_EQ(7, sample.counts(SocketStreamMetrics::WIRE_PROTOCOL_SPDY));
}

TEST(SocketStreamMetricsTest, OtherNumbers) {
  Histogram* histogram;

  // First we'll preserve the original values.
  int64 original_received_bytes = 0;
  int64 original_received_counts = 0;
  int64 original_sent_bytes = 0;
  int64 original_sent_counts = 0;

  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_counts = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_counts = original.sum();
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  metrics.OnWaitConnection();
  metrics.OnStartConnection();
  metrics.OnConnected();
  metrics.OnRead(1);
  metrics.OnRead(10);
  metrics.OnWrite(2);
  metrics.OnWrite(20);
  metrics.OnWrite(200);
  metrics.OnClose();

  Histogram::SampleSet sample;

  // ConnectionLatency.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionLatency", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ConnectionEstablish.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionEstablish", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // Duration.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.Duration", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ReceivedBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(11, sample.sum() - original_received_bytes);  // 11 bytes read.

  // ReceivedCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(2, sample.sum() - original_received_counts);  // 2 read requests.

  // SentBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(222, sample.sum() - original_sent_bytes);  // 222 bytes sent.

  // SentCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(3, sample.sum() - original_sent_counts);  // 3 write requests.
}

}  // namespace net
<commit_msg>GCC 4.6 -Wunused-but-set-variable cleanup.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket_stream/socket_stream_metrics.h"

#include "base/basictypes.h"
#include "base/metrics/histogram.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::Histogram;
using base::StatisticsRecorder;

namespace net {

TEST(SocketStreamMetricsTest, ProtocolType) {
  Histogram* histogram;

  // First we'll preserve the original values. We need to do this
  // as histograms can get affected by other tests. In particular,
  // SocketStreamTest and WebSocketTest can affect the histograms.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ProtocolType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
  SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
  SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
  SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss3(GURL("wss://www.example.com/"));

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ProtocolType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);  // Ensure |original| size is same as |sample|.
  sample.Subtract(original);  // Cancel the original values.
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::PROTOCOL_UNKNOWN));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
}

TEST(SocketStreamMetricsTest, ConnectionType) {
  Histogram* histogram;

  // First we'll preserve the original values.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ConnectionType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  for (int i = 0; i < 1; ++i)
    metrics.OnStartConnection();
  for (int i = 0; i < 2; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION);
  for (int i = 0; i < 3; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION);
  for (int i = 0; i < 4; ++i)
    metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION);

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);
  sample.Subtract(original);
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::ALL_CONNECTIONS));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::TUNNEL_CONNECTION));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::SOCKS_CONNECTION));
  EXPECT_EQ(4, sample.counts(SocketStreamMetrics::SSL_CONNECTION));
}

TEST(SocketStreamMetricsTest, WireProtocolType) {
  Histogram* histogram;

  // First we'll preserve the original values.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.WireProtocolType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  for (int i = 0; i < 3; ++i)
    metrics.OnCountWireProtocolType(
        SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET);
  for (int i = 0; i < 7; ++i)
    metrics.OnCountWireProtocolType(SocketStreamMetrics::WIRE_PROTOCOL_SPDY);

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.WireProtocolType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);
  sample.Subtract(original);
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET));
  EXPECT_EQ(7, sample.counts(SocketStreamMetrics::WIRE_PROTOCOL_SPDY));
}

TEST(SocketStreamMetricsTest, OtherNumbers) {
  Histogram* histogram;

  // First we'll preserve the original values.
  int64 original_received_bytes = 0;
  int64 original_received_counts = 0;
  int64 original_sent_bytes = 0;
  int64 original_sent_counts = 0;

  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_counts = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_counts = original.sum();
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  metrics.OnWaitConnection();
  metrics.OnStartConnection();
  metrics.OnConnected();
  metrics.OnRead(1);
  metrics.OnRead(10);
  metrics.OnWrite(2);
  metrics.OnWrite(20);
  metrics.OnWrite(200);
  metrics.OnClose();

  Histogram::SampleSet sample;

  // ConnectionLatency.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionLatency", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ConnectionEstablish.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionEstablish", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // Duration.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.Duration", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ReceivedBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(11, sample.sum() - original_received_bytes);  // 11 bytes read.

  // ReceivedCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(2, sample.sum() - original_received_counts);  // 2 read requests.

  // SentBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(222, sample.sum() - original_sent_bytes);  // 222 bytes sent.

  // SentCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(3, sample.sum() - original_sent_counts);  // 3 write requests.
}

}  // namespace net
<|endoftext|>


<commit_before>/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2010, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */
// PCL
#include <boost/thread/thread.hpp>
#include <Eigen/Geometry>
#include <pcl/common/common.h>
#define MEASURE_FUNCTION_TIME
#include <pcl/common/time.h> //fps calculations
#include <pcl/io/pcd_io.h>
#include <pcl/io/openni_grabber.h>
#include <cfloat>
#include <pcl/visualization/point_cloud_handlers.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/histogram_visualizer.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>

#define SHOW_FPS 1
#if SHOW_FPS
#define FPS_CALC(_WHAT_) \
do \
{ \
    static unsigned count = 0;\
    static double last = pcl::getTime ();\
    if (++count == 100) \
    { \
      double now = pcl::getTime (); \
      std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" <<  std::endl; \
      count = 0; \
      last = now; \
    } \
}while(false)
#else
#define FPS_CALC(_WHAT_) \
do \
{ \
}while(false)
#endif

using pcl::console::print_color;
using pcl::console::print_error;
using pcl::console::print_error;
using pcl::console::print_warn;
using pcl::console::print_info;
using pcl::console::print_debug;
using pcl::console::print_value;
using pcl::console::print_highlight;
using pcl::console::TT_BRIGHT;
using pcl::console::TT_RED;
using pcl::console::TT_GREEN;
using pcl::console::TT_BLUE;

boost::mutex mutex_;
pcl::PointCloud<pcl::PointXYZ>::ConstPtr g_cloud;
bool new_cloud = false;

void
printHelp (int argc, char **argv)
{
  //print_error ("Syntax is: %s <file_name 1..N>.pcd <options>\n", argv[0]);
  print_error ("Syntax is: %s <options>\n", argv[0]);
  print_info ("  where options are:\n");
  print_info ("                     -dev device_id           = device to be used\n");
  print_info ("                                                maybe \"#n\", with n being the number of the device in device list.\n");
  print_info ("                                                maybe \"bus@addr\", with bus and addr being the usb bus and address where device is connected.\n");
  print_info ("                                                maybe \"serial\", with serial being the serial number of the device.\n");
  print_info ("\n");
}

// Create the PCLVisualizer object
boost::shared_ptr<pcl::visualization::PCLVisualizer> p;

struct EventHelper
{
  void
  cloud_cb (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr & cloud)
  {
    FPS_CALC ("callback");
    if (mutex_.try_lock ())
    {
      g_cloud = cloud;
      new_cloud = true;
      mutex_.unlock ();
    }
  }
};

/* ---[ */
int
main (int argc, char** argv)
{
  srand (time (0));

  if (argc > 1)
  {
    for (int i = 1; i < argc; i++)
    {
      if (std::string (argv[i]) == "-h")
      {
        printHelp (argc, argv);
        return (-1);
      }
    }
  }

  p.reset (new pcl::visualization::PCLVisualizer (argc, argv, "OpenNI Viewer"));

  std::string device_id = "";
  pcl::console::parse_argument (argc, argv, "-dev", device_id);

  pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);

  EventHelper h;
  boost::function<void(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&) > f = boost::bind (&EventHelper::cloud_cb, &h, _1);
  boost::signals2::connection c1 = interface->registerCallback (f);

  interface->start ();
  while (!p->wasStopped ())
  {
    p->spinOnce ();
    if (new_cloud && mutex_.try_lock ())
    {
      new_cloud = false;
      if (g_cloud)
      {
        FPS_CALC ("drawing");
        if (!p->updatePointCloud<pcl::PointXYZ> (g_cloud, "OpenNICloud"))
        {
          p->addPointCloud<pcl::PointXYZ> (g_cloud, "OpenNICloud");
          p->resetCameraViewpoint ("OpenNICloud");
        }
      }
      mutex_.unlock ();
    }
    else
      boost::this_thread::sleep (boost::posix_time::microseconds (1000));
  }

  interface->stop ();
}
/* ]--- */
<commit_msg>openni_viewer now gets 30Hz XYZRGB<commit_after>/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2010, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */
// PCL
#include <boost/thread/thread.hpp>
#include <Eigen/Geometry>
#include <pcl/common/common.h>
#define MEASURE_FUNCTION_TIME
#include <pcl/common/time.h> //fps calculations
#include <pcl/io/pcd_io.h>
#include <pcl/io/openni_grabber.h>
#include <cfloat>
#include <pcl/visualization/point_cloud_handlers.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/histogram_visualizer.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>

#define SHOW_FPS 1
#if SHOW_FPS
#define FPS_CALC(_WHAT_) \
do \
{ \
    static unsigned count = 0;\
    static double last = pcl::getTime ();\
    if (++count == 100) \
    { \
      double now = pcl::getTime (); \
      std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" <<  std::endl; \
      count = 0; \
      last = now; \
    } \
}while(false)
#else
#define FPS_CALC(_WHAT_) \
do \
{ \
}while(false)
#endif

using pcl::console::print_color;
using pcl::console::print_error;
using pcl::console::print_error;
using pcl::console::print_warn;
using pcl::console::print_info;
using pcl::console::print_debug;
using pcl::console::print_value;
using pcl::console::print_highlight;
using pcl::console::TT_BRIGHT;
using pcl::console::TT_RED;
using pcl::console::TT_GREEN;
using pcl::console::TT_BLUE;

boost::mutex mutex_;
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr g_cloud;
bool new_cloud = false;

void
printHelp (int argc, char **argv)
{
  //print_error ("Syntax is: %s <file_name 1..N>.pcd <options>\n", argv[0]);
  print_error ("Syntax is: %s <options>\n", argv[0]);
  print_info ("  where options are:\n");
  print_info ("                     -dev device_id           = device to be used\n");
  print_info ("                                                maybe \"#n\", with n being the number of the device in device list.\n");
  print_info ("                                                maybe \"bus@addr\", with bus and addr being the usb bus and address where device is connected.\n");
  print_info ("                                                maybe \"serial\", with serial being the serial number of the device.\n");
  print_info ("\n");
}

// Create the PCLVisualizer object
boost::shared_ptr<pcl::visualization::PCLVisualizer> p;

struct EventHelper
{
  void
  cloud_cb (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr & cloud)
  {
    FPS_CALC ("callback");
    if (mutex_.try_lock ())
    {
      g_cloud = cloud;
      new_cloud = true;
      mutex_.unlock ();
    }
  }
};

/* ---[ */
int
main (int argc, char** argv)
{
  srand (time (0));

  if (argc > 1)
  {
    for (int i = 1; i < argc; i++)
    {
      if (std::string (argv[i]) == "-h")
      {
        printHelp (argc, argv);
        return (-1);
      }
    }
  }

  p.reset (new pcl::visualization::PCLVisualizer (argc, argv, "OpenNI Viewer"));

  std::string device_id = "";
  pcl::console::parse_argument (argc, argv, "-dev", device_id);

  pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);

  EventHelper h;
  boost::function<void(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&) > f = boost::bind (&EventHelper::cloud_cb, &h, _1);
  boost::signals2::connection c1 = interface->registerCallback (f);

  interface->start ();
  while (!p->wasStopped ())
  {
    p->spinOnce ();
    if (new_cloud && mutex_.try_lock ())
    {
      new_cloud = false;
      if (g_cloud)
      {
        FPS_CALC ("drawing");
        pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> handler (g_cloud);
        if (!p->updatePointCloud<pcl::PointXYZRGB> (g_cloud, handler, "OpenNICloud"))
        {
          p->addPointCloud<pcl::PointXYZRGB> (g_cloud, handler, "OpenNICloud");
          p->resetCameraViewpoint ("OpenNICloud");
        }
      }
      mutex_.unlock ();
    }
    else
      boost::this_thread::sleep (boost::posix_time::microseconds (1000));
  }

  interface->stop ();
}
/* ]--- */
<|endoftext|>


<commit_before>
# include <dsn/tool/simulator.h>
# include "rrdb.check.h"

namespace dsn { namespace apps { class rrdb_checker : public ::dsn::tools::checker
{
public:
    rrdb_checker(const char* name) : ::dsn::tools::checker(name)
    {
        for (auto& app : _apps)
        {
            // TODO: identify your own type of service apps
            //auto meta = dynamic_cast<meta_service_app *>(app.second);
            //if (meta != nullptr) _meta_servers.push_back(meta);
        }
    }

    virtual void check() override
    {
        // nothing to check
        //if (_meta_servers.size() == 0)
        //    return;

        // check all invariances
        /*
        auto meta = meta_leader();
        if (!meta) return;

        for (auto& r : _replica_servers)
        {
            if (!r->is_started())
                continue;

            auto ep = r->primary_address();
            if (!meta->_service->_failure_detector->is_worker_connected(ep))
            {
                dassert(!r->_stub->is_connected(), "when meta server says a replica is dead, it must be dead");
            }
        }
        */
    }

private:
    //std::vector<meta_service_app*>        _meta_servers;
};

void install_checkers(configuration_ptr config)
{
    auto sim = dynamic_cast<::dsn::tools::simulator*>(::dsn::tools::get_current_tool());
    if (nullptr == sim)
        return;

    sim->add_checker(new rrdb_checker("rrdb.global-checker"));
}

} } <commit_msg>remove outdated check file, using rrdb.check.h instead<commit_after><|endoftext|>


<commit_before>/* exonerate_fastparse.cpp
 *
 * Reads raw exonerate output (from STDIN)
 *
 */

#define BUFFER_SIZE 4096

#include <cstdlib>
#include <stdio.h>
#include <string>
#include <string.h>
#include <sstream>
#include <iostream>
using namespace std;

string get_full_header();
string get_with_introns_header();

string parse_vulgar_simple(string line, int stop, char delim);
string parse_vulgar_full(string line, int stop, char delim);
string parse_vulgar_with_introns(string line, int stop, char delim);
string parse_vulgar_verbose(string line, int stop, char delim);
int get_first_integer(string line);
int get_stop_position(string line, int offset);

int main()
{
    cout << get_with_introns_header() << endl;

    // buffer to hold data input
    char buffer[BUFFER_SIZE];

    // string to hold line input
    string line;

    // index for ordering within a record
    int position = 0;

    // flag for being within the alignment region of a record
    bool between = true;

    // query start position for current alignment segment
    int running_position = 0;

    // position of first stop codon, 0 indicates none found
    int stop = 0;

    // temporary storage for output of get_stop_position
    int tmp_stop;

    while(fgets(buffer, BUFFER_SIZE, stdin)){

        // cast the cstring as a string object
        line = buffer;

        if(between){
            if(line.length() > 3 && line.substr(2,6) == "Target"){
                position = 1;
                between = false;
            }
            continue;
        }

        switch(position % 5) {
            case 2: if(line[0] == 'v'){
                        cout << parse_vulgar_with_introns(line, stop, '\t') << endl;
                        between = true;
                        stop = 0;
                    } else {
                        running_position = get_first_integer(line);
                    }
                    break;
            case 4: if(stop == 0) {
                        tmp_stop = get_stop_position(line, running_position);
                        if(tmp_stop != 0 && stop == 0){
                            stop = tmp_stop;
                        }
                    }
                    break;
        }

        position++;
    }
}

int get_stop_position(string line, int pos){
    for(unsigned int i = 0; i < line.length(); i++){
        // All capital letters signal a new amino acid
        if(line[i] >= 'A' && line[i] <= 'Z'){
            pos++;
        }
        // I am interested only in the position of the first stop,
        // so return when first '*' is encountered
        else if(line[i] == '*'){
            return(pos);
        }
    }
    // Return 0 if no stop is found
    return 0;
}

int get_first_integer(string line){
    int x;
    std::stringstream ss;
    ss << line;
    ss >> x;
    return x;
}

string get_full_header(){
    string header = "query_seqid\t"
                    "query_start\t"
                    "query_stop\t"
                    "query_strand\t"
                    "target_contig\t"
                    "target_start\t"
                    "target_stop\t"
                    "target_strand\t"
                    "score\t"
                    "first_stop\t"
                    "has_frameshift\t"
                    "split_codons\t"
                    "introns\t"
                    "max_intron";
    return header;
}

string get_with_introns_header(){
    string header = get_full_header();
    header += "\tintron_lengths";
    return header;
}

string parse_vulgar_full(string line, int stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 10 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    out << stop << delim;
    bool shifted = false;
    int nintrons = 0;
    int splits = 0;
    int longest_intron = 0;
    while(s >> word){
        switch(word[0]){
            case 'F': shifted = true;
                      break;
            case 'I': { nintrons++;
                        s >> word;
                        s >> word;
                        int len = atoi(word.c_str());
                        if(len > longest_intron)
                            longest_intron = len;
                      }
                      break;
            case 'S': splits++;
                      break;
        }
    }
    out << shifted  << delim
        << splits   << delim
        << nintrons << delim
        << longest_intron;
    return out.str();
}

string parse_vulgar_with_introns(string line, int stop, char delim){
    stringstream out;
    stringstream s(line);
    out << parse_vulgar_full(line, stop, delim) << delim;
    string word;
    bool has_intron = false;
    while(s >> word){
        if(word[0] == 'I'){
           s >> word;
           s >> word;
           if(has_intron)
               out << ',';
           out << word;
           has_intron = true;
        }
    }
    if(! has_intron)
        out << '-';
    return out.str();
}

string parse_vulgar_simple(string line, bool stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 9 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    s >> word;
    out << word;
    return out.str();
}

string parse_vulgar_verbose(string line, bool stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 10 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    out << stop << endl;
    for(int i = 0; s >> word; i++){
        if(i % 3 == 0)
            out << "> ";
        out << word;
        out << ((i % 3 != 2) ? delim  : '\n');
    }
    return out.str();
}
<commit_msg>added argument handling and help message<commit_after>/* exonerate_fastparse.cpp
 *
 * Reads raw exonerate output (from STDIN)
 *
 */

#define BUFFER_SIZE 4096

#include <cstdlib>
#include <stdio.h>
#include <string>
#include <string.h>
#include <sstream>
#include <iostream>
#include <getopt.h>
using namespace std;

string get_sans_introns_header();
string get_with_introns_header();
string get_simple_header();

string parse_simple(string line, int stop, char delim);
string parse_sans_introns(string line, int stop, char delim);
string parse_with_introns(string line, int stop, char delim);
string parse_verbose(string line, int stop, char delim);
int get_first_integer(string line);
int get_stop_position(string line, int offset);

int main(int argc, char **argv)
{

    int format = 0;
    int c;

    while (1) {
        static struct option long_options[] =
        {
            {"simple",       no_argument, &format, 0},
            {"sans-introns", no_argument, &format, 1},
            {"with-introns", no_argument, &format, 2},
            {"verbose",      no_argument, &format, 3},
            {"help",         no_argument, 0, 'h'},
            {0,0,0,0}
        };
        int option_index = 0;
        c = getopt_long (argc, argv, "h", long_options, &option_index);

        /* Detect the end of the options. */
        if (c == -1)
            break;

        switch(c) {
            case 0:
                break;
            case 'h':
                cout << "Usage: exonerate-fastparse [options] filename" << endl
                     << "Format options:" << endl
                     << "   --simple - writes tabular output with the following columns:" << endl
                     << "      1. query_seqid"   << endl
                     << "      2. query_start"   << endl
                     << "      3. query_stop"    << endl
                     << "      4. query_strand"  << endl
                     << "      5. target_seqid"  << endl
                     << "      6. target_start"  << endl
                     << "      7. target_stop"   << endl
                     << "      8. target_strand" << endl
                     << "      9. score"         << endl
                     << "   --sans-introns - adds 5 columns to the simple output" << endl
                     << "      10. first_stop"     << endl
                     << "      11. has_frameshift" << endl
                     << "      12. split_codons"   << endl
                     << "      13. introns"        << endl
                     << "      14. max_introns"    << endl
                     << "   --with-introns - adds 1 column to the sans-introns output" << endl
                     << "      15. intron_lengths - a comma-delimited list of intron lengths" << endl
                     << "   --verbose\tprints simple tabular output and then one line for each gff entry" << endl;
                return 0;
        }
    }

    string (*parse)(string, int, char);

    switch(format) {
        case 0:
            cout << get_simple_header() << endl;
            parse = parse_simple;
            break;
        case 1:
            cout << get_sans_introns_header() << endl;
            parse = parse_sans_introns;
            break;
        case 2:
            cout << get_with_introns_header() << endl;
            parse = parse_with_introns;
            break;
        case 3:
            parse = parse_verbose;
            break;
    }

    // buffer to hold data input
    char buffer[BUFFER_SIZE];

    // string to hold line input
    string line;

    // index for ordering within a record
    int position = 0;

    // flag for being within the alignment region of a record
    bool between = true;

    // query start position for current alignment segment
    int running_position = 0;

    // position of first stop codon, 0 indicates none found
    int stop = 0;

    // temporary storage for output of get_stop_position
    int tmp_stop;

    while(fgets(buffer, BUFFER_SIZE, stdin)){

        // cast the cstring as a string object
        line = buffer;

        if(between){
            if(line.length() > 3 && line.substr(2,6) == "Target"){
                position = 1;
                between = false;
            }
            continue;
        }

        switch(position % 5) {
            case 2: if(line[0] == 'v'){
                        cout << parse(line, stop, '\t') << endl;
                        between = true;
                        stop = 0;
                    } else {
                        running_position = get_first_integer(line);
                    }
                    break;
            case 4: if(stop == 0) {
                        tmp_stop = get_stop_position(line, running_position);
                        if(tmp_stop != 0 && stop == 0){
                            stop = tmp_stop;
                        }
                    }
                    break;
        }

        position++;
    }
}

int get_stop_position(string line, int pos){
    for(unsigned int i = 0; i < line.length(); i++){
        // All capital letters signal a new amino acid
        if(line[i] >= 'A' && line[i] <= 'Z'){
            pos++;
        }
        // I am interested only in the position of the first stop,
        // so return when first '*' is encountered
        else if(line[i] == '*'){
            return(pos);
        }
    }
    // Return 0 if no stop is found
    return 0;
}

int get_first_integer(string line){
    int x;
    std::stringstream ss;
    ss << line;
    ss >> x;
    return x;
}

string get_simple_header(){
    string header = "query_seqid\t"
                    "query_start\t"
                    "query_stop\t"
                    "query_strand\t"
                    "target_contig\t"
                    "target_start\t"
                    "target_stop\t"
                    "target_strand\t"
                    "score\t";
    return header;
}

string get_sans_introns_header(){
    string header = get_simple_header();
    header += "first_stop\t"
              "has_frameshift\t"
              "split_codons\t"
              "introns\t"
              "max_intron";
    return header;
}

string get_with_introns_header(){
    string header = get_sans_introns_header();
    header += "\tintron_lengths";
    return header;
}

string parse_sans_introns(string line, int stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 10 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    out << stop << delim;
    bool shifted = false;
    int nintrons = 0;
    int splits = 0;
    int longest_intron = 0;
    while(s >> word){
        switch(word[0]){
            case 'F': shifted = true;
                      break;
            case 'I': { nintrons++;
                        s >> word;
                        s >> word;
                        int len = atoi(word.c_str());
                        if(len > longest_intron)
                            longest_intron = len;
                      }
                      break;
            case 'S': splits++;
                      break;
        }
    }
    out << shifted  << delim
        << splits   << delim
        << nintrons << delim
        << longest_intron;
    return out.str();
}

string parse_with_introns(string line, int stop, char delim){
    stringstream out;
    stringstream s(line);
    out << parse_sans_introns(line, stop, delim) << delim;
    string word;
    bool has_intron = false;
    while(s >> word){
        if(word[0] == 'I'){
           s >> word;
           s >> word;
           if(has_intron)
               out << ',';
           out << word;
           has_intron = true;
        }
    }
    if(! has_intron)
        out << '-';
    return out.str();
}

string parse_simple(string line, int stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 9 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    s >> word;
    out << word;
    return out.str();
}

string parse_verbose(string line, int stop, char delim){
    stringstream s(line);
    stringstream out;
    string word;
    for(int i = 0; i < 10 && s >> word; i++){
        if(i > 0){
            out << word << delim;
        }
    }
    out << stop << endl;
    for(int i = 0; s >> word; i++){
        if(i % 3 == 0)
            out << "> ";
        out << word;
        out << ((i % 3 != 2) ? delim  : '\n');
    }
    return out.str();
}
<|endoftext|>


<commit_before>/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * Razor - a lightweight, Qt based, desktop toolset
 * http://razor-qt.org
 *
 * Copyright: 2012 Razor team
 * Authors:
 *   Petr Vanek <petr@scribus.info>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include <QtGui/QPainter>
#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QtCore/QDateTime>
#include <QtDBus/QDBusArgument>

#include <qtxdg/xdgicon.h>

#include "notification.h"
#include "notificationwidgets.h"

#include <QtDebug>
// this *must* go last due Qt's moc
#include <razorqt/xfitman.h>

#define ICONSIZE QSize(32, 32)

Notification::Notification(const QString &application,
                           const QString &summary, const QString &body,
                           const QString &icon, int timeout,
                           const QStringList& actions, const QVariantMap& hints,
                           QWidget *parent)
    : QWidget(parent),
      m_timer(0),
      m_actionWidget(0)
{
    setupUi(this);
    setObjectName("Notification");
    setMouseTracking(true);

    setMaximumWidth(parent->width());
    setMinimumWidth(parent->width());

    setValues(application, summary, body, icon, timeout, actions, hints);

    connect(closeButton, SIGNAL(clicked()), this, SLOT(closeButton_clicked()));
}

void Notification::setValues(const QString &application,
                             const QString &summary, const QString &body,
                             const QString &icon, int timeout,
                             const QStringList& actions, const QVariantMap& hints)
{
    // Basic properties *********************

    // Notifications spec set real order here:
    // An implementation which only displays one image or icon must
    // choose which one to display using the following order:
    //  - "image-data"
    //  - "image-path"
    //  - app_icon parameter
    //  - for compatibility reason, "icon_data"
    if (!hints["image_data"].isNull())
    {
        m_pixmap = getPixmapFromHint(hints["image_data"]);
//        qDebug() << application << "from image_data" << m_pixmap.isNull();
    }
    else if (!hints["image_path"].isNull())
    {
        m_pixmap = getPixmapFromString(hints["image_path"].toString());
//        qDebug() << application << "from image_path" << m_pixmap.isNull();
    }
    else if (!icon.isEmpty())
    {
        m_pixmap = getPixmapFromString(icon);
//        qDebug() << application << "from icon" << icon << m_pixmap.isNull();
    }
    else if (!hints["icon_data"].isNull())
    {
       m_pixmap = getPixmapFromHint(hints["icon_data"]);
//       qDebug() << application << "from icon_data" << m_pixmap.isNull();
    }
    // issue #325: Do not display icon if it's not found...
    if (m_pixmap.isNull())
    {
        iconLabel->hide();
    }
    else
    {
        if (m_pixmap.size().width() > ICONSIZE.width()
            || m_pixmap.size().height() > ICONSIZE.height())
        {
            m_pixmap = m_pixmap.scaled(ICONSIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }
        iconLabel->setPixmap(m_pixmap);
        iconLabel->show();
    }

    // application
	appLabel->setVisible(!application.isEmpty());
    appLabel->setText(application);

    // summary
    summaryLabel->setVisible(!summary.isEmpty());
    summaryLabel->setText(summary);

    if (application == summary)
        summaryLabel->setVisible(false);

    // body
    bodyLabel->setVisible(!body.isEmpty());
    bodyLabel->setText(body);

    // Timeout
    // Special values:
    //  < 0: server decides timeout
    //    0: infifite
    if (m_timer)
    {
        m_timer->stop();
        m_timer->deleteLater();
    }

    // -1 for server decides is handled in notifyd to save QSettings instance
    if (timeout > 0)
    {
        m_timer = new NotificationTimer(this);
        connect(m_timer, SIGNAL(timeout()), this, SIGNAL(timeout()));
        m_timer->start(timeout);
    }

    // Categories *********************
    // TODO/FIXME: Categories - how to handle it?
    if (!hints["category"].isNull())
    {
        qDebug() << "Notification" << application << "category" << hints["category"];
    }

    // Urgency Levels *********************
    // Type    Description
    // 0   Low
    // 1   Normal
    // 2   Critical
    // TODO/FIXME: Urgencies - how to handle it?
    if (!hints["urgency"].isNull())
    {
        qDebug() << "Notification" << application << "urgency" << hints["urgency"];
    }

    // Actions
    if (actions.count() && m_actionWidget == 0)
    {
        if (actions.count() < 4)
            m_actionWidget = new NotificationActionsButtonsWidget(actions, this);
        else
            m_actionWidget = new NotificationActionsComboWidget(actions, this);
        connect(m_actionWidget, SIGNAL(actionTriggered(const QString &)),
                this, SIGNAL(actionTriggered(const QString &)));
        actionsLayout->addWidget(m_actionWidget);
        m_actionWidget->show();
    }

    adjustSize();
    // ensure layout expansion
    setMinimumHeight(qMax(rect().height(), childrenRect().height()));
}

void Notification::closeButton_clicked()
{
    if (m_timer)
        m_timer->stop();
    emit userCanceled();
}

void Notification::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

QPixmap Notification::getPixmapFromHint(const QVariant &argument) const
{
    int width, height, rowstride, bitsPerSample, channels;
    bool hasAlpha;
    QByteArray data;

    const QDBusArgument arg = argument.value<QDBusArgument>();
    arg.beginStructure();
    arg >> width;
    arg >> height;
    arg >> rowstride;
    arg >> hasAlpha;
    arg >> bitsPerSample;
    arg >> channels;
    arg >> data;
    arg.endStructure();
    QImage img = QImage((uchar*)data.constData(), width, height, QImage::Format_ARGB32).rgbSwapped();

    return QPixmap::fromImage(img);
}

QPixmap Notification::getPixmapFromString(const QString &str) const
{
    QUrl url(str);
    if (url.isValid() && QFile::exists(url.toLocalFile()))
    {
//        qDebug() << "    getPixmapFromString by URL" << url;
        return QPixmap(url.toLocalFile());
    }
    else
    {
//        qDebug() << "    getPixmapFromString by XdgIcon theme" << str << ICONSIZE << XdgIcon::themeName();
//        qDebug() << "       " << XdgIcon::fromTheme(str) << "isnull:" << XdgIcon::fromTheme(str).isNull();
        // They say: do not display an icon if it;s not found - see #325
        return XdgIcon::fromTheme(str/*, XdgIcon::defaultApplicationIcon()*/).pixmap(ICONSIZE);
    }
}

void Notification::enterEvent(QEvent * event)
{
    if (m_timer)
        m_timer->pause();
}

void Notification::leaveEvent(QEvent * event)
{
    if (m_timer)
        m_timer->resume();
}

void Notification::mouseReleaseEvent(QMouseEvent * event)
{
//    qDebug() << "CLICKED" << event;
    QString appName;
    QString windowTitle;

    if (m_actionWidget && m_actionWidget->hasDefaultAction())
    {
        emit actionTriggered(m_actionWidget->defaultAction());
        return;
    }

    foreach (Window i, xfitMan().getClientList())
    {
        appName = xfitMan().getApplicationName(i);
        windowTitle = xfitMan().getWindowTitle(i);
        //qDebug() << "    " << i << "APPNAME" << appName << "TITLE" << windowTitle;
        if (appName.isEmpty())
        {
            QWidget::mouseReleaseEvent(event);
            return;
        }
        if (appName == appLabel->text() || windowTitle == appLabel->text())
        {
//            qDebug() << "         FOUND!";
            xfitMan().raiseWindow(i);
            closeButton_clicked();
            return;
        }
    }
}

NotificationTimer::NotificationTimer(QObject *parent)
    : QTimer(parent)
{
}

void NotificationTimer::start(int msec)
{
    m_startMsec = QDateTime::currentMSecsSinceEpoch();
    m_intervalMsec = msec;
    QTimer::start(msec);
}

void NotificationTimer::pause()
{
    if (!isActive())
        return;

    stop();
    m_intervalMsec -= QDateTime::currentMSecsSinceEpoch() - m_startMsec;
}

void NotificationTimer::resume()
{
    if (isActive())
        return;

    start(m_intervalMsec);
}
<commit_msg>compiles with qt 4.6.x as well<commit_after>/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * Razor - a lightweight, Qt based, desktop toolset
 * http://razor-qt.org
 *
 * Copyright: 2012 Razor team
 * Authors:
 *   Petr Vanek <petr@scribus.info>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include <QtGui/QPainter>
#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QtCore/QDateTime>
#include <QtDBus/QDBusArgument>

#include <qtxdg/xdgicon.h>

#include "notification.h"
#include "notificationwidgets.h"

#include <QtDebug>
// this *must* go last due Qt's moc
#include <razorqt/xfitman.h>

#define ICONSIZE QSize(32, 32)

Notification::Notification(const QString &application,
                           const QString &summary, const QString &body,
                           const QString &icon, int timeout,
                           const QStringList& actions, const QVariantMap& hints,
                           QWidget *parent)
    : QWidget(parent),
      m_timer(0),
      m_actionWidget(0)
{
    setupUi(this);
    setObjectName("Notification");
    setMouseTracking(true);

    setMaximumWidth(parent->width());
    setMinimumWidth(parent->width());

    setValues(application, summary, body, icon, timeout, actions, hints);

    connect(closeButton, SIGNAL(clicked()), this, SLOT(closeButton_clicked()));
}

void Notification::setValues(const QString &application,
                             const QString &summary, const QString &body,
                             const QString &icon, int timeout,
                             const QStringList& actions, const QVariantMap& hints)
{
    // Basic properties *********************

    // Notifications spec set real order here:
    // An implementation which only displays one image or icon must
    // choose which one to display using the following order:
    //  - "image-data"
    //  - "image-path"
    //  - app_icon parameter
    //  - for compatibility reason, "icon_data"
    if (!hints["image_data"].isNull())
    {
        m_pixmap = getPixmapFromHint(hints["image_data"]);
//        qDebug() << application << "from image_data" << m_pixmap.isNull();
    }
    else if (!hints["image_path"].isNull())
    {
        m_pixmap = getPixmapFromString(hints["image_path"].toString());
//        qDebug() << application << "from image_path" << m_pixmap.isNull();
    }
    else if (!icon.isEmpty())
    {
        m_pixmap = getPixmapFromString(icon);
//        qDebug() << application << "from icon" << icon << m_pixmap.isNull();
    }
    else if (!hints["icon_data"].isNull())
    {
       m_pixmap = getPixmapFromHint(hints["icon_data"]);
//       qDebug() << application << "from icon_data" << m_pixmap.isNull();
    }
    // issue #325: Do not display icon if it's not found...
    if (m_pixmap.isNull())
    {
        iconLabel->hide();
    }
    else
    {
        if (m_pixmap.size().width() > ICONSIZE.width()
            || m_pixmap.size().height() > ICONSIZE.height())
        {
            m_pixmap = m_pixmap.scaled(ICONSIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }
        iconLabel->setPixmap(m_pixmap);
        iconLabel->show();
    }

    // application
	appLabel->setVisible(!application.isEmpty());
    appLabel->setText(application);

    // summary
    summaryLabel->setVisible(!summary.isEmpty());
    summaryLabel->setText(summary);

    if (application == summary)
        summaryLabel->setVisible(false);

    // body
    bodyLabel->setVisible(!body.isEmpty());
    bodyLabel->setText(body);

    // Timeout
    // Special values:
    //  < 0: server decides timeout
    //    0: infifite
    if (m_timer)
    {
        m_timer->stop();
        m_timer->deleteLater();
    }

    // -1 for server decides is handled in notifyd to save QSettings instance
    if (timeout > 0)
    {
        m_timer = new NotificationTimer(this);
        connect(m_timer, SIGNAL(timeout()), this, SIGNAL(timeout()));
        m_timer->start(timeout);
    }

    // Categories *********************
    // TODO/FIXME: Categories - how to handle it?
    if (!hints["category"].isNull())
    {
        qDebug() << "Notification" << application << "category" << hints["category"];
    }

    // Urgency Levels *********************
    // Type    Description
    // 0   Low
    // 1   Normal
    // 2   Critical
    // TODO/FIXME: Urgencies - how to handle it?
    if (!hints["urgency"].isNull())
    {
        qDebug() << "Notification" << application << "urgency" << hints["urgency"];
    }

    // Actions
    if (actions.count() && m_actionWidget == 0)
    {
        if (actions.count() < 4)
            m_actionWidget = new NotificationActionsButtonsWidget(actions, this);
        else
            m_actionWidget = new NotificationActionsComboWidget(actions, this);
        connect(m_actionWidget, SIGNAL(actionTriggered(const QString &)),
                this, SIGNAL(actionTriggered(const QString &)));
        actionsLayout->addWidget(m_actionWidget);
        m_actionWidget->show();
    }

    adjustSize();
    // ensure layout expansion
    setMinimumHeight(qMax(rect().height(), childrenRect().height()));
}

void Notification::closeButton_clicked()
{
    if (m_timer)
        m_timer->stop();
    emit userCanceled();
}

void Notification::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

QPixmap Notification::getPixmapFromHint(const QVariant &argument) const
{
    int width, height, rowstride, bitsPerSample, channels;
    bool hasAlpha;
    QByteArray data;

    const QDBusArgument arg = argument.value<QDBusArgument>();
    arg.beginStructure();
    arg >> width;
    arg >> height;
    arg >> rowstride;
    arg >> hasAlpha;
    arg >> bitsPerSample;
    arg >> channels;
    arg >> data;
    arg.endStructure();
    QImage img = QImage((uchar*)data.constData(), width, height, QImage::Format_ARGB32).rgbSwapped();

    return QPixmap::fromImage(img);
}

QPixmap Notification::getPixmapFromString(const QString &str) const
{
    QUrl url(str);
    if (url.isValid() && QFile::exists(url.toLocalFile()))
    {
//        qDebug() << "    getPixmapFromString by URL" << url;
        return QPixmap(url.toLocalFile());
    }
    else
    {
//        qDebug() << "    getPixmapFromString by XdgIcon theme" << str << ICONSIZE << XdgIcon::themeName();
//        qDebug() << "       " << XdgIcon::fromTheme(str) << "isnull:" << XdgIcon::fromTheme(str).isNull();
        // They say: do not display an icon if it;s not found - see #325
        return XdgIcon::fromTheme(str/*, XdgIcon::defaultApplicationIcon()*/).pixmap(ICONSIZE);
    }
}

void Notification::enterEvent(QEvent * event)
{
    if (m_timer)
        m_timer->pause();
}

void Notification::leaveEvent(QEvent * event)
{
    if (m_timer)
        m_timer->resume();
}

void Notification::mouseReleaseEvent(QMouseEvent * event)
{
//    qDebug() << "CLICKED" << event;
    QString appName;
    QString windowTitle;

    if (m_actionWidget && m_actionWidget->hasDefaultAction())
    {
        emit actionTriggered(m_actionWidget->defaultAction());
        return;
    }

    foreach (Window i, xfitMan().getClientList())
    {
        appName = xfitMan().getApplicationName(i);
        windowTitle = xfitMan().getWindowTitle(i);
        //qDebug() << "    " << i << "APPNAME" << appName << "TITLE" << windowTitle;
        if (appName.isEmpty())
        {
            QWidget::mouseReleaseEvent(event);
            return;
        }
        if (appName == appLabel->text() || windowTitle == appLabel->text())
        {
//            qDebug() << "         FOUND!";
            xfitMan().raiseWindow(i);
            closeButton_clicked();
            return;
        }
    }
}

NotificationTimer::NotificationTimer(QObject *parent)
    : QTimer(parent)
{
}

void NotificationTimer::start(int msec)
{
#if QT_VERSION >= 0x040700
    m_startMsec = QDateTime::currentMSecsSinceEpoch();
#else
    m_startMsec = QDateTime::fromString("1970-01-01T00:00:00", Qt::ISODate).msecsTo( QDateTime::currentDateTime());
#endif
    m_intervalMsec = msec;
    QTimer::start(msec);
}

void NotificationTimer::pause()
{
    if (!isActive())
        return;

    stop();
#if QT_VERSION >= 0x040700
    m_intervalMsec -= QDateTime::currentMSecsSinceEpoch() - m_startMsec;
#else
    m_startMsec = QDateTime::fromString("1970-01-01T00:00:00", Qt::ISODate).msecsTo( QDateTime::currentDateTime()) - m_startMsec;
#endif
}

void NotificationTimer::resume()
{
    if (isActive())
        return;

    start(m_intervalMsec);
}
<|endoftext|>