text:
/***************************************************************************
 * pack.c:
 *
 * Generic routines to pack Mini-SEED records using an MSrecord as a
 * header template and data source.
 *
 * Written by Chad Trabant,
 *   IRIS Data Management Center
 *
 * modified: 2015.273
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "libmseed.h"
#include "packdata.h"

/* Function(s) internal to this file */
static int msr_pack_header_raw (MSRecord *msr, char *rawrec, int maxheaderlen,
                                flag swapflag, flag normalize,
                                struct blkt_1001_s **blkt1001,
                                char *srcname, flag verbose);
static int msr_update_header (MSRecord *msr, char *rawrec, flag swapflag,
                              struct blkt_1001_s *blkt1001,
                              char *srcname, flag verbose);
static int msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes,
                          int32_t *lastintsample, flag comphistory,
                          char sampletype, flag encoding, flag swapflag,
                          char *srcname, flag verbose);

/* Header and data byte order flags controlled by environment variables */
/* -2 = not checked, -1 = checked but not set, or 0 = LE and 1 = BE */
flag packheaderbyteorder = -2;
flag packdatabyteorder   = -2;

/***************************************************************************
 * msr_pack:
 *
 * Pack data into SEED data records.  Using the record header values
 * in the MSRecord as a template the common header fields are packed
 * into the record header, blockettes in the blockettes chain are
 * packed and data samples are packed in the encoding format indicated
 * by the MSRecord->encoding field.  A Blockette 1000 will be added if
 * one is not present.
 *
 * The MSRecord->datasamples array and MSRecord->numsamples value will
 * not be changed by this routine.  It is the responsibility of the
 * calling routine to adjust the data buffer if desired.
 *
 * As each record is filled and finished they are passed to
 * record_handler which expects 1) a char * to the record, 2) the
 * length of the record and 3) a pointer supplied by the original
 * caller containing optional private data (handlerdata).  It is the
 * responsibility of record_handler to process the record, the memory
 * will be re-used or freed when record_handler returns.
 *
 * If the flush flag != 0 all of the data will be packed into data
 * records even though the last one will probably not be filled.
 *
 * Default values are: data record & quality indicator = 'D', record
 * length = 4096, encoding = 11 (Steim2) and byteorder = 1 (MSBF).
 * The defaults are triggered when the the msr->dataquality is 0 or
 * msr->reclen, msr->encoding and msr->byteorder are -1 respectively.
 *
 * Returns the number of records created on success and -1 on error.
 ***************************************************************************/
int
msr_pack (MSRecord *msr, void (*record_handler) (char *, int, void *),
          void *handlerdata, int64_t *packedsamples, flag flush, flag verbose)
{
  uint16_t *HPnumsamples;
  uint16_t *HPdataoffset;
  struct blkt_1001_s *HPblkt1001 = NULL;

  char *rawrec;
  char *envvariable;
  char srcname[50];

  flag headerswapflag = 0;
  flag dataswapflag   = 0;

  int samplesize;
  int headerlen;
  int dataoffset;
  int maxdatabytes;
  int maxsamples;
  int recordcnt = 0;
  int packsamples, packoffset;
  int64_t totalpackedsamples;
  hptime_t segstarttime;

  if (!msr)
    return -1;

  if (!record_handler)
  {
    ms_log (2, "msr_pack(): record_handler() function pointer not set!\n");
    return -1;
  }

  /* Allocate stream processing state space if needed */
  if (!msr->ststate)
  {
    msr->ststate = (StreamState *)malloc (sizeof (StreamState));
    if (!msr->ststate)
    {
      ms_log (2, "msr_pack(): Could not allocate memory for StreamState\n");
      return -1;
    }
    memset (msr->ststate, 0, sizeof (StreamState));
  }

  /* Generate source name for MSRecord */
  if (msr_srcname (msr, srcname, 1) == NULL)
  {
    ms_log (2, "msr_unpack_data(): Cannot generate srcname\n");
    return MS_GENERROR;
  }

  /* Track original segment start time for new start time calculation */
  segstarttime = msr->starttime;

  /* Read possible environmental variables that force byteorder */
  if (packheaderbyteorder == -2)
  {
    if ((envvariable = getenv ("PACK_HEADER_BYTEORDER")))
    {
      if (*envvariable != '0' && *envvariable != '1')
      {
        ms_log (2, "Environment variable PACK_HEADER_BYTEORDER must be set to '0' or '1'\n");
        return -1;
      }
      else if (*envvariable == '0')
      {
        packheaderbyteorder = 0;
        if (verbose > 2)
          ms_log (1, "PACK_HEADER_BYTEORDER=0, packing little-endian header\n");
      }
      else
      {
        packheaderbyteorder = 1;
        if (verbose > 2)
          ms_log (1, "PACK_HEADER_BYTEORDER=1, packing big-endian header\n");
      }
    }
    else
    {
      packheaderbyteorder = -1;
    }
  }
  if (packdatabyteorder == -2)
  {
    if ((envvariable = getenv ("PACK_DATA_BYTEORDER")))
    {
      if (*envvariable != '0' && *envvariable != '1')
      {
        ms_log (2, "Environment variable PACK_DATA_BYTEORDER must be set to '0' or '1'\n");
        return -1;
      }
      else if (*envvariable == '0')
      {
        packdatabyteorder = 0;
        if (verbose > 2)
          ms_log (1, "PACK_DATA_BYTEORDER=0, packing little-endian data samples\n");
      }
      else
      {
        packdatabyteorder = 1;
        if (verbose > 2)
          ms_log (1, "PACK_DATA_BYTEORDER=1, packing big-endian data samples\n");
      }
    }
    else
    {
      packdatabyteorder = -1;
    }
  }

  /* Set default indicator, record length, byte order and encoding if needed */
  if (msr->dataquality == 0)
    msr->dataquality = 'D';
  if (msr->reclen == -1)
    msr->reclen = 4096;
  if (msr->byteorder == -1)
    msr->byteorder = 1;
  if (msr->encoding == -1)
    msr->encoding = DE_STEIM2;

  /* Cleanup/reset sequence number */
  if (msr->sequence_number <= 0 || msr->sequence_number > 999999)
    msr->sequence_number = 1;

  if (msr->reclen < MINRECLEN || msr->reclen > MAXRECLEN)
  {
    ms_log (2, "msr_pack(%s): Record length is out of range: %d\n",
            srcname, msr->reclen);
    return -1;
  }

  if (msr->numsamples <= 0)
  {
    ms_log (2, "msr_pack(%s): No samples to pack\n", srcname);
    return -1;
  }

  samplesize = ms_samplesize (msr->sampletype);

  if (!samplesize)
  {
    ms_log (2, "msr_pack(%s): Unknown sample type '%c'\n",
            srcname, msr->sampletype);
    return -1;
  }

  /* Sanity check for msr/quality indicator */
  if (!MS_ISDATAINDICATOR (msr->dataquality))
  {
    ms_log (2, "msr_pack(%s): Record header & quality indicator unrecognized: '%c'\n",
            srcname, msr->dataquality);
    ms_log (2, "msr_pack(%s): Packing failed.\n", srcname);
    return -1;
  }

  /* Allocate space for data record */
  rawrec = (char *)malloc (msr->reclen);

  if (rawrec == NULL)
  {
    ms_log (2, "msr_pack(%s): Cannot allocate memory\n", srcname);
    return -1;
  }

  /* Set header pointers to known offsets into FSDH */
  HPnumsamples = (uint16_t *)(rawrec + 30);
  HPdataoffset = (uint16_t *)(rawrec + 44);

  /* Check to see if byte swapping is needed */
  if (msr->byteorder != ms_bigendianhost ())
    headerswapflag = dataswapflag = 1;

  /* Check if byte order is forced */
  if (packheaderbyteorder >= 0)
  {
    headerswapflag = (msr->byteorder != packheaderbyteorder) ? 1 : 0;
  }

  if (packdatabyteorder >= 0)
  {
    dataswapflag = (msr->byteorder != packdatabyteorder) ? 1 : 0;
  }

  if (verbose > 2)
  {
    if (headerswapflag && dataswapflag)
      ms_log (1, "%s: Byte swapping needed for packing of header and data samples\n", srcname);
    else if (headerswapflag)
      ms_log (1, "%s: Byte swapping needed for packing of header\n", srcname);
    else if (dataswapflag)
      ms_log (1, "%s: Byte swapping needed for packing of data samples\n", srcname);
    else
      ms_log (1, "%s: Byte swapping NOT needed for packing\n", srcname);
  }

  /* Add a blank 1000 Blockette if one is not present, the blockette values
     will be populated in msr_pack_header_raw()/msr_normalize_header() */
  if (!msr->Blkt1000)
  {
    struct blkt_1000_s blkt1000;
    memset (&blkt1000, 0, sizeof (struct blkt_1000_s));

    if (verbose > 2)
      ms_log (1, "%s: Adding 1000 Blockette\n", srcname);

    if (!msr_addblockette (msr, (char *)&blkt1000, sizeof (struct blkt_1000_s), 1000, 0))
    {
      ms_log (2, "msr_pack(%s): Error adding 1000 Blockette\n", srcname);
      free (rawrec);
      return -1;
    }
  }

  headerlen = msr_pack_header_raw (msr, rawrec, msr->reclen, headerswapflag, 1,
                                   &HPblkt1001, srcname, verbose);

  if (headerlen == -1)
  {
    ms_log (2, "msr_pack(%s): Error packing header\n", srcname);
    free (rawrec);
    return -1;
  }

  /* Determine offset to encoded data */
  if (msr->encoding == DE_STEIM1 || msr->encoding == DE_STEIM2)
  {
    dataoffset = 64;
    while (dataoffset < headerlen)
      dataoffset += 64;

    /* Zero memory between blockettes and data if any */
    memset (rawrec + headerlen, 0, dataoffset - headerlen);
  }
  else
  {
    dataoffset = headerlen;
  }

  *HPdataoffset = (uint16_t)dataoffset;
  if (headerswapflag)
    ms_gswap2 (HPdataoffset);

  /* Determine the max data bytes and sample count */
  maxdatabytes = msr->reclen - dataoffset;

  if (msr->encoding == DE_STEIM1)
  {
    maxsamples = (int)(maxdatabytes / 64) * STEIM1_FRAME_MAX_SAMPLES;
  }
  else if (msr->encoding == DE_STEIM2)
  {
    maxsamples = (int)(maxdatabytes / 64) * STEIM2_FRAME_MAX_SAMPLES;
  }
  else
  {
    maxsamples = maxdatabytes / samplesize;
  }

  /* Pack samples into records */
  *HPnumsamples      = 0;
  totalpackedsamples = 0;
  packoffset         = 0;
  if (packedsamples)
    *packedsamples = 0;

  while ((msr->numsamples - totalpackedsamples) > maxsamples || flush)
  {
    packsamples = msr_pack_data (rawrec + dataoffset,
                                 (char *)msr->datasamples + packoffset,
                                 (int)(msr->numsamples - totalpackedsamples), maxdatabytes,
                                 &msr->ststate->lastintsample, msr->ststate->comphistory,
                                 msr->sampletype, msr->encoding, dataswapflag,
                                 srcname, verbose);

    if (packsamples < 0)
    {
      ms_log (2, "msr_pack(%s): Error packing data samples\n", srcname);
      free (rawrec);
      return -1;
    }

    packoffset += packsamples * samplesize;

    /* Update number of samples */
    *HPnumsamples = (uint16_t)packsamples;
    if (headerswapflag)
      ms_gswap2 (HPnumsamples);

    if (verbose > 0)
      ms_log (1, "%s: Packed %d samples\n", srcname, packsamples);

    /* Send record to handler */
    record_handler (rawrec, msr->reclen, handlerdata);

    totalpackedsamples += packsamples;
    if (packedsamples)
      *packedsamples = totalpackedsamples;
    msr->ststate->packedsamples += packsamples;

    /* Update record header for next record */
    msr->sequence_number = (msr->sequence_number >= 999999) ? 1 : msr->sequence_number + 1;
    if (msr->samprate > 0)
      msr->starttime = segstarttime + (hptime_t) (totalpackedsamples / msr->samprate * HPTMODULUS + 0.5);

    msr_update_header (msr, rawrec, headerswapflag, HPblkt1001, srcname, verbose);

    recordcnt++;
    msr->ststate->packedrecords++;

    /* Set compression history flag for subsequent records (Steim encodings) */
    if (!msr->ststate->comphistory)
      msr->ststate->comphistory = 1;

    if (totalpackedsamples >= msr->numsamples)
      break;
  }

  if (verbose > 2)
    ms_log (1, "%s: Packed %d total samples\n", srcname, totalpackedsamples);

  free (rawrec);

  return recordcnt;
} /* End of msr_pack() */

/***************************************************************************
 * msr_pack_header:
 *
 * Pack data header/blockettes into the SEED record at
 * MSRecord->record.  Unlike msr_pack no default values are applied,
 * the header structures are expected to be self describing and no
 * Blockette 1000 will be added.  This routine is only useful for
 * re-packing a record header.
 *
 * Returns the header length in bytes on success and -1 on error.
 ***************************************************************************/
int
msr_pack_header (MSRecord *msr, flag normalize, flag verbose)
{
  char srcname[50];
  char *envvariable;
  flag headerswapflag = 0;
  int headerlen;
  int maxheaderlen;

  if (!msr)
    return -1;

  /* Generate source name for MSRecord */
  if (msr_srcname (msr, srcname, 1) == NULL)
  {
    ms_log (2, "msr_unpack_data(): Cannot generate srcname\n");
    return MS_GENERROR;
  }

  /* Read possible environmental variables that force byteorder */
  if (packheaderbyteorder == -2)
  {
    if ((envvariable = getenv ("PACK_HEADER_BYTEORDER")))
    {
      if (*envvariable != '0' && *envvariable != '1')
      {
        ms_log (2, "Environment variable PACK_HEADER_BYTEORDER must be set to '0' or '1'\n");
        return -1;
      }
      else if (*envvariable == '0')
      {
        packheaderbyteorder = 0;
        if (verbose > 2)
          ms_log (1, "PACK_HEADER_BYTEORDER=0, packing little-endian header\n");
      }
      else
      {
        packheaderbyteorder = 1;
        if (verbose > 2)
          ms_log (1, "PACK_HEADER_BYTEORDER=1, packing big-endian header\n");
      }
    }
    else
    {
      packheaderbyteorder = -1;
    }
  }

  if (msr->reclen < MINRECLEN || msr->reclen > MAXRECLEN)
  {
    ms_log (2, "msr_pack_header(%s): record length is out of range: %d\n",
            srcname, msr->reclen);
    return -1;
  }

  if (msr->byteorder != 0 && msr->byteorder != 1)
  {
    ms_log (2, "msr_pack_header(%s): byte order is not defined correctly: %d\n",
            srcname, msr->byteorder);
    return -1;
  }

  if (msr->fsdh)
  {
    maxheaderlen = (msr->fsdh->data_offset > 0) ? msr->fsdh->data_offset : msr->reclen;
  }
  else
  {
    maxheaderlen = msr->reclen;
  }

  /* Check to see if byte swapping is needed */
  if (msr->byteorder != ms_bigendianhost ())
    headerswapflag = 1;

  /* Check if byte order is forced */
  if (packheaderbyteorder >= 0)
  {
    headerswapflag = (msr->byteorder != packheaderbyteorder) ? 1 : 0;
  }

  if (verbose > 2)
  {
    if (headerswapflag)
      ms_log (1, "%s: Byte swapping needed for packing of header\n", srcname);
    else
      ms_log (1, "%s: Byte swapping NOT needed for packing of header\n", srcname);
  }

  headerlen = msr_pack_header_raw (msr, msr->record, maxheaderlen,
                                   headerswapflag, normalize, NULL,
                                   srcname, verbose);

  return headerlen;
} /* End of msr_pack_header() */

/***************************************************************************
 * msr_pack_header_raw:
 *
 * Pack data header/blockettes into the specified SEED data record.
 *
 * Returns the header length in bytes on success or -1 on error.
 ***************************************************************************/
static int
msr_pack_header_raw (MSRecord *msr, char *rawrec, int maxheaderlen,
                     flag swapflag, flag normalize,
                     struct blkt_1001_s **blkt1001,
                     char *srcname, flag verbose)
{
  struct blkt_link_s *cur_blkt;
  struct fsdh_s *fsdh;
  int16_t offset;
  int blktcnt = 0;
  int nextoffset;

  if (!msr || !rawrec)
    return -1;

  /* Make sure a fixed section of data header is available */
  if (!msr->fsdh)
  {
    msr->fsdh = (struct fsdh_s *)calloc (1, sizeof (struct fsdh_s));

    if (msr->fsdh == NULL)
    {
      ms_log (2, "msr_pack_header_raw(%s): Cannot allocate memory\n", srcname);
      return -1;
    }
  }

  /* Update the SEED structures associated with the MSRecord */
  if (normalize)
    if (msr_normalize_header (msr, verbose) < 0)
    {
      ms_log (2, "msr_pack_header_raw(%s): error normalizing header values\n", srcname);
      return -1;
    }

  if (verbose > 2)
    ms_log (1, "%s: Packing fixed section of data header\n", srcname);

  if (maxheaderlen > msr->reclen)
  {
    ms_log (2, "msr_pack_header_raw(%s): maxheaderlen of %d is beyond record length of %d\n",
            srcname, maxheaderlen, msr->reclen);
    return -1;
  }

  if (maxheaderlen < (int)sizeof (struct fsdh_s))
  {
    ms_log (2, "msr_pack_header_raw(%s): maxheaderlen of %d is too small, must be >= %d\n",
            srcname, maxheaderlen, sizeof (struct fsdh_s));
    return -1;
  }

  fsdh   = (struct fsdh_s *)rawrec;
  offset = 48;

  /* Roll-over sequence number if necessary */
  if (msr->sequence_number > 999999)
    msr->sequence_number = 1;

  /* Copy FSDH associated with the MSRecord into the record */
  memcpy (fsdh, msr->fsdh, sizeof (struct fsdh_s));

  /* Swap byte order? */
  if (swapflag)
  {
    MS_SWAPBTIME (&fsdh->start_time);
    ms_gswap2 (&fsdh->numsamples);
    ms_gswap2 (&fsdh->samprate_fact);
    ms_gswap2 (&fsdh->samprate_mult);
    ms_gswap4 (&fsdh->time_correct);
    ms_gswap2 (&fsdh->data_offset);
    ms_gswap2 (&fsdh->blockette_offset);
  }

  /* Traverse blockette chain and pack blockettes at 'offset' */
  cur_blkt = msr->blkts;

  while (cur_blkt && offset < maxheaderlen)
  {
    /* Check that the blockette fits */
    if ((offset + 4 + cur_blkt->blktdatalen) > maxheaderlen)
    {
      ms_log (2, "msr_pack_header_raw(%s): header exceeds maxheaderlen of %d\n",
              srcname, maxheaderlen);
      break;
    }

    /* Pack blockette type and leave space for next offset */
    memcpy (rawrec + offset, &cur_blkt->blkt_type, 2);
    if (swapflag)
      ms_gswap2 (rawrec + offset);
    nextoffset = offset + 2;
    offset += 4;

    if (cur_blkt->blkt_type == 100)
    {
      struct blkt_100_s *blkt_100 = (struct blkt_100_s *)(rawrec + offset);
      memcpy (blkt_100, cur_blkt->blktdata, sizeof (struct blkt_100_s));
      offset += sizeof (struct blkt_100_s);

      if (swapflag)
      {
        ms_gswap4 (&blkt_100->samprate);
      }
    }

    else if (cur_blkt->blkt_type == 200)
    {
      struct blkt_200_s *blkt_200 = (struct blkt_200_s *)(rawrec + offset);
      memcpy (blkt_200, cur_blkt->blktdata, sizeof (struct blkt_200_s));
      offset += sizeof (struct blkt_200_s);

      if (swapflag)
      {
        ms_gswap4 (&blkt_200->amplitude);
        ms_gswap4 (&blkt_200->period);
        ms_gswap4 (&blkt_200->background_estimate);
        MS_SWAPBTIME (&blkt_200->time);
      }
    }

    else if (cur_blkt->blkt_type == 201)
    {
      struct blkt_201_s *blkt_201 = (struct blkt_201_s *)(rawrec + offset);
      memcpy (blkt_201, cur_blkt->blktdata, sizeof (struct blkt_201_s));
      offset += sizeof (struct blkt_201_s);

      if (swapflag)
      {
        ms_gswap4 (&blkt_201->amplitude);
        ms_gswap4 (&blkt_201->period);
        ms_gswap4 (&blkt_201->background_estimate);
        MS_SWAPBTIME (&blkt_201->time);
      }
    }

    else if (cur_blkt->blkt_type == 300)
    {
      struct blkt_300_s *blkt_300 = (struct blkt_300_s *)(rawrec + offset);
      memcpy (blkt_300, cur_blkt->blktdata, sizeof (struct blkt_300_s));
      offset += sizeof (struct blkt_300_s);

      if (swapflag)
      {
        MS_SWAPBTIME (&blkt_300->time);
        ms_gswap4 (&blkt_300->step_duration);
        ms_gswap4 (&blkt_300->interval_duration);
        ms_gswap4 (&blkt_300->amplitude);
        ms_gswap4 (&blkt_300->reference_amplitude);
      }
    }

    else if (cur_blkt->blkt_type == 310)
    {
      struct blkt_310_s *blkt_310 = (struct blkt_310_s *)(rawrec + offset);
      memcpy (blkt_310, cur_blkt->blktdata, sizeof (struct blkt_310_s));
      offset += sizeof (struct blkt_310_s);

      if (swapflag)
      {
        MS_SWAPBTIME (&blkt_310->time);
        ms_gswap4 (&blkt_310->duration);
        ms_gswap4 (&blkt_310->period);
        ms_gswap4 (&blkt_310->amplitude);
        ms_gswap4 (&blkt_310->reference_amplitude);
      }
    }

    else if (cur_blkt->blkt_type == 320)
    {
      struct blkt_320_s *blkt_320 = (struct blkt_320_s *)(rawrec + offset);
      memcpy (blkt_320, cur_blkt->blktdata, sizeof (struct blkt_320_s));
      offset += sizeof (struct blkt_320_s);

      if (swapflag)
      {
        MS_SWAPBTIME (&blkt_320->time);
        ms_gswap4 (&blkt_320->duration);
        ms_gswap4 (&blkt_320->ptp_amplitude);
        ms_gswap4 (&blkt_320->reference_amplitude);
      }
    }

    else if (cur_blkt->blkt_type == 390)
    {
      struct blkt_390_s *blkt_390 = (struct blkt_390_s *)(rawrec + offset);
      memcpy (blkt_390, cur_blkt->blktdata, sizeof (struct blkt_390_s));
      offset += sizeof (struct blkt_390_s);

      if (swapflag)
      {
        MS_SWAPBTIME (&blkt_390->time);
        ms_gswap4 (&blkt_390->duration);
        ms_gswap4 (&blkt_390->amplitude);
      }
    }

    else if (cur_blkt->blkt_type == 395)
    {
      struct blkt_395_s *blkt_395 = (struct blkt_395_s *)(rawrec + offset);
      memcpy (blkt_395, cur_blkt->blktdata, sizeof (struct blkt_395_s));
      offset += sizeof (struct blkt_395_s);

      if (swapflag)
      {
        MS_SWAPBTIME (&blkt_395->time);
      }
    }

    else if (cur_blkt->blkt_type == 400)
    {
      struct blkt_400_s *blkt_400 = (struct blkt_400_s *)(rawrec + offset);
      memcpy (blkt_400, cur_blkt->blktdata, sizeof (struct blkt_400_s));
      offset += sizeof (struct blkt_400_s);

      if (swapflag)
      {
        ms_gswap4 (&blkt_400->azimuth);
        ms_gswap4 (&blkt_400->slowness);
        ms_gswap2 (&blkt_400->configuration);
      }
    }

    else if (cur_blkt->blkt_type == 405)
    {
      struct blkt_405_s *blkt_405 = (struct blkt_405_s *)(rawrec + offset);
      memcpy (blkt_405, cur_blkt->blktdata, sizeof (struct blkt_405_s));
      offset += sizeof (struct blkt_405_s);

      if (swapflag)
      {
        ms_gswap2 (&blkt_405->delay_values);
      }

      if (verbose > 0)
      {
        ms_log (1, "msr_pack_header_raw(%s): WARNING Blockette 405 cannot be fully supported\n",
                srcname);
      }
    }

    else if (cur_blkt->blkt_type == 500)
    {
      struct blkt_500_s *blkt_500 = (struct blkt_500_s *)(rawrec + offset);
      memcpy (blkt_500, cur_blkt->blktdata, sizeof (struct blkt_500_s));
      offset += sizeof (struct blkt_500_s);

      if (swapflag)
      {
        ms_gswap4 (&blkt_500->vco_correction);
        MS_SWAPBTIME (&blkt_500->time);
        ms_gswap4 (&blkt_500->exception_count);
      }
    }

    else if (cur_blkt->blkt_type == 1000)
    {
      struct blkt_1000_s *blkt_1000 = (struct blkt_1000_s *)(rawrec + offset);
      memcpy (blkt_1000, cur_blkt->blktdata, sizeof (struct blkt_1000_s));
      offset += sizeof (struct blkt_1000_s);

      /* This guarantees that the byte order is in sync with msr_pack() */
      if (packdatabyteorder >= 0)
        blkt_1000->byteorder = packdatabyteorder;
    }

    else if (cur_blkt->blkt_type == 1001)
    {
      struct blkt_1001_s *blkt_1001 = (struct blkt_1001_s *)(rawrec + offset);
      memcpy (blkt_1001, cur_blkt->blktdata, sizeof (struct blkt_1001_s));
      offset += sizeof (struct blkt_1001_s);

      /* Track location of Blockette 1001 if requested */
      if (blkt1001)
        *blkt1001 = blkt_1001;
    }

    else if (cur_blkt->blkt_type == 2000)
    {
      struct blkt_2000_s *blkt_2000 = (struct blkt_2000_s *)(rawrec + offset);
      memcpy (blkt_2000, cur_blkt->blktdata, cur_blkt->blktdatalen);
      offset += cur_blkt->blktdatalen;

      if (swapflag)
      {
        ms_gswap2 (&blkt_2000->length);
        ms_gswap2 (&blkt_2000->data_offset);
        ms_gswap4 (&blkt_2000->recnum);
      }

      /* Nothing done to pack the opaque headers and data, they should already
         be packed into the blockette payload */
    }

    else
    {
      memcpy (rawrec + offset, cur_blkt->blktdata, cur_blkt->blktdatalen);
      offset += cur_blkt->blktdatalen;
    }

    /* Pack the offset to the next blockette */
    if (cur_blkt->next)
    {
      memcpy (rawrec + nextoffset, &offset, 2);
      if (swapflag)
        ms_gswap2 (rawrec + nextoffset);
    }
    else
    {
      memset (rawrec + nextoffset, 0, 2);
    }

    blktcnt++;
    cur_blkt = cur_blkt->next;
  }

  fsdh->numblockettes = blktcnt;

  if (verbose > 2)
    ms_log (1, "%s: Packed %d blockettes\n", srcname, blktcnt);

  return offset;
} /* End of msr_pack_header_raw() */

/***************************************************************************
 * msr_update_header:
 *
 * Update the header values that change between records: start time,
 * sequence number, etc.
 *
 * Returns 0 on success or -1 on error.
 ***************************************************************************/
static int
msr_update_header (MSRecord *msr, char *rawrec, flag swapflag,
                   struct blkt_1001_s *blkt1001, char *srcname, flag verbose)
{
  struct fsdh_s *fsdh;
  hptime_t hptimems;
  int8_t usecoffset;
  char seqnum[7];

  if (!msr || !rawrec)
    return -1;

  if (verbose > 2)
    ms_log (1, "%s: Updating fixed section of data header\n", srcname);

  fsdh = (struct fsdh_s *)rawrec;

  /* Pack values into the fixed section of header */
  snprintf (seqnum, 7, "%06d", msr->sequence_number);
  memcpy (fsdh->sequence_number, seqnum, 6);

  /* Get start time rounded to tenths of milliseconds and microsecond offset */
  ms_hptime2tomsusecoffset (msr->starttime, &hptimems, &usecoffset);

  /* Update fixed-section start time */
  ms_hptime2btime (hptimems, &(fsdh->start_time));

  /* Swap byte order? */
  if (swapflag)
  {
    MS_SWAPBTIME (&fsdh->start_time);
  }

  /* Update microsecond offset value if Blockette 1001 is present */
  if (msr->Blkt1001 && blkt1001)
  {
    /* Update microseconds offset in blockette chain entry */
    msr->Blkt1001->usec = usecoffset;

    /* Update microseconds offset in packed header */
    blkt1001->usec = usecoffset;
  }

  return 0;
} /* End of msr_update_header() */

/************************************************************************
 *  msr_pack_data:
 *
 *  Pack Mini-SEED data samples.  The input data samples specified as
 *  'src' will be packed with 'encoding' format and placed in 'dest'.
 *
 *  If a pointer to a 32-bit integer sample is provided in the
 *  argument 'lastintsample' and 'comphistory' is true the sample
 *  value will be used to seed the difference buffer for Steim1/2
 *  encoding and provide a compression history.  It will also be
 *  updated with the last sample packed in order to be used with a
 *  subsequent call to this routine.
 *
 *  Return number of samples packed on success and a negative on error.
 ************************************************************************/
static int
msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes,
               int32_t *lastintsample, flag comphistory, char sampletype,
               flag encoding, flag swapflag, char *srcname, flag verbose)
{
  int nsamples;
  int32_t *intbuff;
  int32_t d0;

  /* Check for encode debugging environment variable */
  if (getenv ("ENCODE_DEBUG"))
    encodedebug = 1;

  /* Decide if this is a format that we can encode */
  switch (encoding)
  {
  case DE_ASCII:
    if (sampletype != 'a')
    {
      ms_log (2, "%s: Sample type must be ascii (a) for ASCII text encoding not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    if (verbose > 1)
      ms_log (1, "%s: Packing ASCII data\n", srcname);

    nsamples = msr_encode_text (src, maxsamples, dest, maxdatabytes);

    break;

  case DE_INT16:
    if (sampletype != 'i')
    {
      ms_log (2, "%s: Sample type must be integer (i) for INT16 encoding not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    if (verbose > 1)
      ms_log (1, "%s: Packing INT16 data samples\n", srcname);

    nsamples = msr_encode_int16 (src, maxsamples, dest, maxdatabytes, swapflag);

    break;

  case DE_INT32:
    if (sampletype != 'i')
    {
      ms_log (2, "%s: Sample type must be integer (i) for INT32 encoding not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    if (verbose > 1)
      ms_log (1, "%s: Packing INT32 data samples\n", srcname);

    nsamples = msr_encode_int32 (src, maxsamples, dest, maxdatabytes, swapflag);

    break;

  case DE_FLOAT32:
    if (sampletype != 'f')
    {
      ms_log (2, "%s: Sample type must be float (f) for FLOAT32 encoding not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    if (verbose > 1)
      ms_log (1, "%s: Packing FLOAT32 data samples\n", srcname);

    nsamples = msr_encode_float32 (src, maxsamples, dest, maxdatabytes, swapflag);

    break;

  case DE_FLOAT64:
    if (sampletype != 'd')
    {
      ms_log (2, "%s: Sample type must be double (d) for FLOAT64 encoding not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    if (verbose > 1)
      ms_log (1, "%s: Packing FLOAT64 data samples\n", srcname);

    nsamples = msr_encode_float64 (src, maxsamples, dest, maxdatabytes, swapflag);

    break;

  case DE_STEIM1:
    if (sampletype != 'i')
    {
      ms_log (2, "%s: Sample type must be integer (i) for Steim1 compression not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    intbuff = (int32_t *)src;

    /* If a previous sample is supplied use it for compression history otherwise cold-start */
    d0 = (lastintsample && comphistory) ? (intbuff[0] - *lastintsample) : 0;

    if (verbose > 1)
      ms_log (1, "%s: Packing Steim1 data frames\n", srcname);

    nsamples = msr_encode_steim1 (src, maxsamples, dest, maxdatabytes, d0, swapflag);

    /* If a previous sample is supplied update it with the last sample value */
    if (lastintsample && nsamples > 0)
      *lastintsample = intbuff[nsamples - 1];

    break;

  case DE_STEIM2:
    if (sampletype != 'i')
    {
      ms_log (2, "%s: Sample type must be integer (i) for Steim2 compression not '%c'\n",
              srcname, sampletype);
      return -1;
    }

    intbuff = (int32_t *)src;

    /* If a previous sample is supplied use it for compression history otherwise cold-start */
    d0 = (lastintsample && comphistory) ? (intbuff[0] - *lastintsample) : 0;

    if (verbose > 1)
      ms_log (1, "%s: Packing Steim2 data frames\n", srcname);

    nsamples = msr_encode_steim2 (src, maxsamples, dest, maxdatabytes, d0, srcname, swapflag);

    /* If a previous sample is supplied update it with the last sample value */
    if (lastintsample && nsamples > 0)
      *lastintsample = intbuff[nsamples - 1];

    break;

  default:
    ms_log (2, "%s: Unable to pack format %d\n", srcname, encoding);

    return -1;
  }

  return nsamples;
} /* End of msr_pack_data() */


text:
from recipe_engine.post_process import StepSuccess, LogEquals, StepTextEquals
from recipe_engine.post_process import StepException, StepFailure
from recipe_engine.post_process import DropExpectation

PYTHON_VERSION_COMPATIBILITY = 'PY2+3'

DEPS = [
  'step',
]

def RunSteps(api):
  api.step.empty('hello', step_text='stuff', log_text='other\nstuff')

  api.step.empty('multi hello', log_text=['multi', 'line'])

  api.step.empty(
      'alternate log', log_name='details', log_text='some\nlog\ncontent')

  try:
    api.step.empty('bye', status=api.step.FAILURE)
    assert False, 'unreachable'  # pragma: no cover
  except api.step.StepFailure:
    pass

  try:
    api.step.empty('bigfail', status=api.step.INFRA_FAILURE)
    assert False, 'unreachable'  # pragma: no cover
  except api.step.StepFailure:
    pass

  api.step.empty('quiet fail', status=api.step.FAILURE, raise_on_failure=False)

def GenTests(api):
  yield api.test(
      'basic',
      api.post_process(StepSuccess, 'hello'),
      api.post_process(LogEquals, 'hello', 'stdout', 'other\nstuff'),
      api.post_process(StepTextEquals, 'hello', 'stuff'),

      api.post_process(StepSuccess, 'multi hello'),
      api.post_process(LogEquals, 'multi hello', 'stdout', 'multi\nline'),

      api.post_process(StepSuccess, 'alternate log'),
      api.post_process(LogEquals, 'alternate log', 'details',
                       'some\nlog\ncontent'),

      api.post_process(StepException, 'bigfail'),

      api.post_process(StepFailure, 'quiet fail'),

      api.post_process(DropExpectation),
  )

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FxCop.Sdk;

namespace CustomRules
{
    public class MissingHttpMethodRule : BaseRule
    {
        public MissingHttpMethodRule() : base("MissingHttpMethodRule") { }

        public override TargetVisibilities TargetVisibility
        {
            get
            {
                return TargetVisibilities.ExternallyVisible;
            }
        }

        public override ProblemCollection Check(Member member)
        {
            if (member.IsControllerAction() && !member.HasActionMethodSelectorAttribute())
            {
                Problems.Add(new Problem(this.GetResolution()));
            }
            return Problems;
        }
    }
}


text:
package org.apache.aries.tx.control.jdbc.local.impl;

import static java.util.Arrays.asList;
import static java.util.Optional.ofNullable;
import static org.osgi.framework.Constants.OBJECTCLASS;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_DATABASE_NAME;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_DATASOURCE_NAME;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_DESCRIPTION;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_NETWORK_PROTOCOL;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_PASSWORD;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_PORT_NUMBER;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_ROLE_NAME;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_SERVER_NAME;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_URL;
import static org.osgi.service.jdbc.DataSourceFactory.JDBC_USER;
import static org.osgi.service.jdbc.DataSourceFactory.OSGI_JDBC_DRIVER_CLASS;

import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
import org.osgi.service.jdbc.DataSourceFactory;
import org.osgi.service.transaction.control.jdbc.JDBCConnectionProvider;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManagedServiceFactoryImpl implements ManagedServiceFactory {

	private static final Logger LOG = LoggerFactory.getLogger(ManagedServiceFactoryImpl.class);

	private static final String DSF_TARGET_FILTER = "aries.dsf.target.filter";
	private static final String JDBC_PROP_NAMES = "aries.jdbc.property.names";
	private static final List<String> JDBC_PROPERTIES = asList(JDBC_DATABASE_NAME, JDBC_DATASOURCE_NAME,
			JDBC_DESCRIPTION, JDBC_NETWORK_PROTOCOL, JDBC_PASSWORD, JDBC_PORT_NUMBER, JDBC_ROLE_NAME, JDBC_SERVER_NAME,
			JDBC_URL, JDBC_USER);

	private final Map<String, ManagedJDBCResourceProvider> managedInstances = new ConcurrentHashMap<>();

	private final BundleContext context;

	public ManagedServiceFactoryImpl(BundleContext context) {
		this.context = context;
	}

	@Override
	public String getName() {
		return "Aries JDBCConnectionProvider (Local only) service";
	}

	@Override
	public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {

		Map<String, Object> propsMap = new HashMap<>();

		Enumeration<String> keys = properties.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			propsMap.put(key, properties.get(key));
		}

		Properties jdbcProps = getJdbcProps(pid, propsMap);

		try {
			ManagedJDBCResourceProvider mjrp = new ManagedJDBCResourceProvider(context, pid, jdbcProps, propsMap);
			ofNullable(managedInstances.put(pid, mjrp)).ifPresent(ManagedJDBCResourceProvider::stop);
			mjrp.start();
		} catch (InvalidSyntaxException e) {
			LOG.error("The configuration {} contained an invalid target filter {}", pid, e.getFilter());
			throw new ConfigurationException(DSF_TARGET_FILTER, "The target filter was invalid", e);
		}
	}

	public void stop() {
		managedInstances.values().forEach(ManagedJDBCResourceProvider::stop);
	}

	@SuppressWarnings("unchecked")
	private Properties getJdbcProps(String pid, Map<String, Object> properties) throws ConfigurationException {

		Object object = properties.getOrDefault(JDBC_PROP_NAMES, JDBC_PROPERTIES);
		Collection<String> propnames;
		if (object instanceof String) {
			propnames = Arrays.asList(((String) object).split(","));
		} else if (object instanceof String[]) {
			propnames = Arrays.asList((String[]) object);
		} else if (object instanceof Collection) {
			propnames = (Collection<String>) object;
		} else {
			LOG.error("The configuration {} contained an invalid list of JDBC property names", pid, object);
			throw new ConfigurationException(JDBC_PROP_NAMES,
					"The jdbc property names must be a String+ or comma-separated String");
		}

		Properties p = new Properties();

		propnames.stream().filter(properties::containsKey)
				.forEach(s -> p.setProperty(s, String.valueOf(properties.get(s))));

		return p;
	}

	@Override
	public void deleted(String pid) {
		ofNullable(managedInstances.remove(pid))
			.ifPresent(ManagedJDBCResourceProvider::stop);
	}

	private static class ManagedJDBCResourceProvider
			implements ServiceTrackerCustomizer<DataSourceFactory, DataSourceFactory> {

		private final BundleContext context;
		private final String pid;
		private final Properties jdbcProperties;
		private final Map<String, Object> providerProperties;
		private final ServiceTracker<DataSourceFactory, DataSourceFactory> dsfTracker;

		private final AtomicReference<DataSourceFactory> activeDsf = new AtomicReference<>();
		private final AtomicReference<ServiceRegistration<JDBCConnectionProvider>> serviceReg = new AtomicReference<>();

		public ManagedJDBCResourceProvider(BundleContext context, String pid, Properties jdbcProperties,
				Map<String, Object> providerProperties) throws InvalidSyntaxException, ConfigurationException {
			this.context = context;
			this.pid = pid;
			this.jdbcProperties = jdbcProperties;
			this.providerProperties = providerProperties;

			String targetFilter = (String) providerProperties.get(DSF_TARGET_FILTER);
			if (targetFilter == null) {
				String driver = (String) providerProperties.get(OSGI_JDBC_DRIVER_CLASS);
				if (driver == null) {
					LOG.error("The configuration {} must specify a target filter or a JDBC driver class", pid);
					throw new ConfigurationException(OSGI_JDBC_DRIVER_CLASS,
							"The configuration must specify either a target filter or a JDBC driver class");
				}
				targetFilter = "(" + OSGI_JDBC_DRIVER_CLASS + "=" + driver + ")";
			}

			targetFilter = "(&(" + OBJECTCLASS + "=" + DataSourceFactory.class.getName() + ")" + targetFilter + ")";

			this.dsfTracker = new ServiceTracker<>(context, context.createFilter(targetFilter), this);
		}

		public void start() {
			dsfTracker.open();
		}

		public void stop() {
			dsfTracker.close();
		}

		@Override
		public DataSourceFactory addingService(ServiceReference<DataSourceFactory> reference) {
			DataSourceFactory service = context.getService(reference);

			updateService(service);
			return service;
		}

		private void updateService(DataSourceFactory service) {
			boolean setDsf;
			synchronized (this) {
				setDsf = activeDsf.compareAndSet(null, service);
			}

			if (setDsf) {
				try {
					JDBCConnectionProvider provider = new JDBCConnectionProviderFactoryImpl().getProviderFor(service,
							jdbcProperties, providerProperties);
					ServiceRegistration<JDBCConnectionProvider> reg = context
							.registerService(JDBCConnectionProvider.class, provider, getServiceProperties());
					if (!serviceReg.compareAndSet(null, reg)) {
						throw new IllegalStateException("Unable to set the JDBC connection provider registration");
					}
				} catch (Exception e) {
					LOG.error("An error occurred when creating the connection provider for {}.", pid, e);
					activeDsf.compareAndSet(service, null);
				}
			}
		}

		private Dictionary<String, ?> getServiceProperties() {
			Hashtable<String, Object> props = new Hashtable<>();
			providerProperties.keySet().stream().filter(s -> !JDBC_PASSWORD.equals(s))
					.forEach(s -> props.put(s, providerProperties.get(s)));
			return props;
		}

		@Override
		public void modifiedService(ServiceReference<DataSourceFactory> reference, DataSourceFactory service) {
		}

		@Override
		public void removedService(ServiceReference<DataSourceFactory> reference, DataSourceFactory service) {
			boolean dsfLeft;
			ServiceRegistration<JDBCConnectionProvider> oldReg = null;
			synchronized (this) {
				dsfLeft = activeDsf.compareAndSet(service, null);
				if (dsfLeft) {
					oldReg = serviceReg.getAndSet(null);
				}
			}

			if (oldReg != null) {
				try {
					oldReg.unregister();
				} catch (IllegalStateException ise) {
					LOG.debug("An exception occurred when unregistering a service for {}", pid);
				}
			}

			if (dsfLeft) {
				DataSourceFactory newDSF = dsfTracker.getService();
				if (newDSF != null) {
					updateService(dsfTracker.getService());
				}
			}
		}
	}
}


text:
package org.eclipse.lsp4j;

/**
 * The diagnostic tags.
 *
 * Since 3.15.0
 */
public enum DiagnosticTag {

    /**
     * Unused or unnecessary code.
     *
     * Clients are allowed to render diagnostics with this tag faded out instead of having
     * an error squiggle.
     */
    Unnecessary(1),

    /**
     * Deprecated or obsolete code.
     *
     * Clients are allowed to rendered diagnostics with this tag strike through.
     */
    Deprecated(2);

    private final int value;

    DiagnosticTag(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static DiagnosticTag forValue(int value) {
        DiagnosticTag[] allValues = DiagnosticTag.values();
        if (value < 1 || value > allValues.length)
            throw new IllegalArgumentException("Illegal enum value: " + value);
        return allValues[value - 1];
    }
}


text:
/*===
JSON
false false true
===*/

var pd;

print(JSON[Symbol.toStringTag]);
pd = Object.getOwnPropertyDescriptor(JSON, Symbol.toStringTag);
print(pd.writable, pd.enumerable, pd.configurable);


text: * pip continues and 'succeeds' with no files when build fails


text:
namespace draco {

// Container class for storing draco::Texture objects in an indexed list.
class TextureLibrary {
 public:
  // Copies textures from the source library to this library. Order of the
  // copied textures is preserved.
  void Copy(const TextureLibrary &src);

  // Appends all textures from the source library to this library. All textures
  // are copied over.
  void Append(const TextureLibrary &src);

  // Removes all textures from the library.
  void Clear();

  // Pushes a new texture into the library. Returns an index of the newly
  // inserted texture.
  int PushTexture(std::unique_ptr<Texture> texture);

  size_t NumTextures() const { return textures_.size(); }

  Texture *GetTexture(int index) { return textures_[index].get(); }
  const Texture *GetTexture(int index) const { return textures_[index].get(); }

  // Returns a map from texture pointer to texture index for all textures.
  std::unordered_map<const Texture *, int> ComputeTextureToIndexMap() const;

  // Removes and returns a texture from the library. The returned texture can be
  // either used by the caller or ignored in which case it would be
  // automatically deleted.
  std::unique_ptr<Texture> RemoveTexture(int index);

 private:
  std::vector<std::unique_ptr<Texture>> textures_;
};

}  // namespace draco

#endif  // DRACO_TRANSCODER_SUPPORTED
#endif  // DRACO_TEXTURE_TEXTURE_LIBRARY_H_


text:
const React = require('react');
const TestUtils = require('react-addons-test-utils');

const Cell = require('../Cell.jsx');

describe('Cell', () => {
    it('renders', () => {
        const testProps = {
            className: 'customClassName',
            width: 2999,
            label: 'This is my label'
        };

        const el = TestUtils.renderIntoDocument(<Cell {...testProps} />);
        const domNode = el.getDOMNode();

        expect(el).toBeTruthy();
        expect(domNode.style.width).toEqual(testProps.width + 'px');
        expect(domNode.innerHTML).toEqual(testProps.label);
        expect(domNode.className).toContain(testProps.className);
    });
});


text:
use config::Config;
use utils::console::*;

use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use toml;

error_chain! {}

#[derive(Debug)]
pub struct Cache {
    cache_dir: String,
    filename: String,
}

impl Cache {
    pub fn new(config: &Config, module_name: &str, cache_name: &str) -> Self {
        let filename = format!("{}.{}.toml", module_name, cache_name);

        Cache { cache_dir: config.general.cache_dir.clone(), filename: filename }
    }

    pub fn load<T: Deserialize>(self) -> Result<T> {
        let mut path = PathBuf::from(&self.cache_dir);
        path.push(&self.filename);
        let mut file = File::open(path.into_os_string()).chain_err(|| "Could not open cache file.")?;
        let mut content = String::new();
        file.read_to_string(&mut content).chain_err(|| "Could not read config file.")?;

        let data: T = toml::from_str(&content).chain_err(|| "Could not parse cache file.")?;

        Ok(data)
    }

    pub fn write<T: Serialize>(&self, data: &T) -> Result<()> {
        self.check_or_create_cache_dir().chain_err(|| "Cache directory is unavailable")?;
        let toml = toml::to_string(&data).chain_err(|| "Failed to parse data to TOML")?;
        self.write_toml_file(&toml).chain_err(|| "Failed to write cache file")?;

        Ok(())
    }

    fn check_or_create_cache_dir(&self) -> Result<()> {
        let path = Path::new(&self.cache_dir);
        if !path.is_dir() {
            fs::create_dir_all(path).chain_err(|| "Could not create cache directory")?;
            verboseln(format!("Created cache directory '{}'", &self.cache_dir));
        }

        Ok(())
    }

    fn write_toml_file(&self, toml: &str) -> Result<()> {
        let mut path = PathBuf::from(&self.cache_dir);
        path.push(&self.filename);

        let mut file = File::create(path.into_os_string()).chain_err(|| "Failed to create cache file")?;
        file.write_all(&toml.as_bytes()).chain_err(|| "Failed to write data to cache file")?;

        Ok(())
    }
}


text:
import argparse
import warnings

import numpy

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import reporter
from chainer import training
from chainer.training import extensions

import data
import thin_stack

def linearize_tree(vocab, root, xp=numpy):
    # Left node indexes for all parent nodes
    lefts = []
    # Right node indexes for all parent nodes
    rights = []
    # Parent node indexes
    dests = []
    # All labels to predict for all parent nodes
    labels = []

    # All words of leaf nodes
    words = []
    # Leaf labels
    leaf_labels = []

    # Current leaf node index
    leaf_index = [0]

    def traverse_leaf(exp):
        if len(exp) == 2:
            label, leaf = exp
            if leaf not in vocab:
                vocab[leaf] = len(vocab)
            words.append(vocab[leaf])
            leaf_labels.append(int(label))
            leaf_index[0] += 1
        elif len(exp) == 3:
            _, left, right = exp
            traverse_leaf(left)
            traverse_leaf(right)

    traverse_leaf(root)

    # Current internal node index
    node_index = leaf_index
    leaf_index = [0]

    def traverse_node(exp):
        if len(exp) == 2:
            leaf_index[0] += 1
            return leaf_index[0] - 1
        elif len(exp) == 3:
            label, left, right = exp
            l = traverse_node(left)
            r = traverse_node(right)

            lefts.append(l)
            rights.append(r)
            dests.append(node_index[0])
            labels.append(int(label))

            node_index[0] += 1
            return node_index[0] - 1

    traverse_node(root)
    assert len(lefts) == len(words) - 1

    return {
        'lefts': xp.array(lefts, xp.int32),
        'rights': xp.array(rights, xp.int32),
        'dests': xp.array(dests, xp.int32),
        'words': xp.array(words, xp.int32),
        'labels': xp.array(labels, xp.int32),
        'leaf_labels': xp.array(leaf_labels, xp.int32),
    }

@chainer.dataset.converter()
def convert(batch, device):
    return tuple(
        [device.send(d['lefts']) for d in batch] +
        [device.send(d['rights']) for d in batch] +
        [device.send(d['dests']) for d in batch] +
        [device.send(d['labels']) for d in batch] +
        [device.send(d['words']) for d in batch] +
        [device.send(d['leaf_labels']) for d in batch]
    )

class ThinStackRecursiveNet(chainer.Chain):

    def __init__(self, n_vocab, n_units, n_label):
        super(ThinStackRecursiveNet, self).__init__(
            embed=L.EmbedID(n_vocab, n_units),
            l=L.Linear(n_units * 2, n_units),
            w=L.Linear(n_units, n_label))
        self.n_units = n_units

    def leaf(self, x):
        return self.embed(x)

    def node(self, left, right):
        return F.tanh(self.l(F.concat((left, right))))

    def label(self, v):
        return self.w(v)

    def forward(self, *inputs):
        batch = len(inputs) // 6
        lefts = inputs[0: batch]
        rights = inputs[batch: batch * 2]
        dests = inputs[batch * 2: batch * 3]
        labels = inputs[batch * 3: batch * 4]
        sequences = inputs[batch * 4: batch * 5]
        leaf_labels = inputs[batch * 5: batch * 6]

        inds = numpy.argsort([-len(l) for l in lefts])
        # Sort all arrays in descending order and transpose them
        lefts = F.transpose_sequence([lefts[i] for i in inds])
        rights = F.transpose_sequence([rights[i] for i in inds])
        dests = F.transpose_sequence([dests[i] for i in inds])
        labels = F.transpose_sequence([labels[i] for i in inds])
        sequences = F.transpose_sequence([sequences[i] for i in inds])
        leaf_labels = F.transpose_sequence(
            [leaf_labels[i] for i in inds])

        batch = len(inds)
        maxlen = len(sequences)

        loss = 0
        count = 0
        correct = 0

        dtype = chainer.get_dtype()
        stack = self.xp.zeros((batch, maxlen * 2, self.n_units), dtype)
        for i, (word, label) in enumerate(zip(sequences, leaf_labels)):
            batch = word.shape[0]
            es = self.leaf(word)
            ds = self.xp.full((batch,), i, self.xp.int32)
            y = self.label(es)
            loss += F.softmax_cross_entropy(y, label, normalize=False) * batch
            count += batch
            predict = self.xp.argmax(y.array, axis=1)
            correct += (predict == label.array).sum()

            stack = thin_stack.thin_stack_set(stack, ds, es)

        for left, right, dest, label in zip(lefts, rights, dests, labels):
            l, stack = thin_stack.thin_stack_get(stack, left)
            r, stack = thin_stack.thin_stack_get(stack, right)
            o = self.node(l, r)
            y = self.label(o)
            batch = l.shape[0]
            loss += F.softmax_cross_entropy(y, label, normalize=False) * batch
            count += batch
            predict = self.xp.argmax(y.array, axis=1)
            correct += (predict == label.array).sum()

            stack = thin_stack.thin_stack_set(stack, dest, o)

        loss /= count
        reporter.report({'loss': loss}, self)
        reporter.report({'total': count}, self)
        reporter.report({'correct': correct}, self)
        return loss

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--device', '-d', type=str, default='-1',
                        help='Device specifier. Either ChainerX device '
                        'specifier or an integer. If non-negative integer, '
                        'CuPy arrays with specified device id are used. If '
                        'negative integer, NumPy arrays are used')
    parser.add_argument('--epoch', '-e', default=400, type=int,
                        help='number of epochs to learn')
    parser.add_argument('--unit', '-u', default=30, type=int,
                        help='number of units')
    parser.add_argument('--batchsize', '-b', type=int, default=25,
                        help='learning minibatch size')
    parser.add_argument('--label', '-l', type=int, default=5,
                        help='number of labels')
    parser.add_argument('--epocheval', '-p', type=int, default=5,
                        help='number of epochs per evaluation')
    parser.add_argument('--test', dest='test', action='store_true')
    parser.set_defaults(test=False)
    group = parser.add_argument_group('deprecated arguments')
    group.add_argument('--gpu', '-g', dest='device',
                       type=int, nargs='?', const=0,
                       help='GPU ID (negative value indicates CPU)')
    args = parser.parse_args()

    if chainer.get_dtype() == numpy.float16:
        warnings.warn(
            'This example may cause NaN in FP16 mode.', RuntimeWarning)

    vocab = {}
    max_size = None
    train_trees = data.read_corpus('trees/train.txt', max_size)
    test_trees = data.read_corpus('trees/test.txt', max_size)

    device = chainer.get_device(args.device)
    device.use()
    xp = device.xp

    train_data = [linearize_tree(vocab, t, xp) for t in train_trees]
    train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize)
    test_data = [linearize_tree(vocab, t, xp) for t in test_trees]
    test_iter = chainer.iterators.SerialIterator(
        test_data, args.batchsize, repeat=False, shuffle=False)

    model = ThinStackRecursiveNet(len(vocab), args.unit, args.label)
    model.to_device(device)

    optimizer = chainer.optimizers.AdaGrad(0.1)
    optimizer.setup(model)

    updater = training.StandardUpdater(
        train_iter, optimizer, converter=convert, device=device)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'))
    trainer.extend(
        extensions.Evaluator(
            test_iter, model, converter=convert, device=device),
        trigger=(args.epocheval, 'epoch'))
    trainer.extend(extensions.LogReport())

    trainer.extend(extensions.MicroAverage(
        'main/correct', 'main/total', 'main/accuracy'))
    trainer.extend(extensions.MicroAverage(
        'validation/main/correct', 'validation/main/total',
        'validation/main/accuracy'))

    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))

    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.run()

if __name__ == '__main__':
    main()


text:
package org.opendaylight.opflex.genie.content.model.mownership;

import java.util.LinkedList;
import java.util.Map;

import org.opendaylight.opflex.genie.content.model.mclass.MClass;
import org.opendaylight.opflex.genie.content.model.module.Module;
import org.opendaylight.opflex.genie.engine.model.Cat;
import org.opendaylight.opflex.genie.engine.model.Item;
import org.opendaylight.opflex.modlan.report.Severity;

/**
 * Created by midvorki on 9/27/14.
 */
public class MClassRule extends MOwnershipRule
{
    public static final Cat MY_CAT = Cat.getCreate("mowner:class");

    public MClassRule(MOwner aInParent, String aInNameOrAll)
    {
        super(MY_CAT,aInParent,aInNameOrAll,DefinitionScope.OWNER);
    }

    public MClassRule(MModuleRule aInParent, String aInNameOrAll)
    {
        super(MY_CAT,aInParent,aInNameOrAll,DefinitionScope.MODULE);
    }

    public void getClasses(Map<String, MClass> aOut)
    {
        switch (getDefinitionScope())
        {
            case OWNER:

                {
                    if (isWildCard())
                    {
                        // THIS IS WILD CARD, ALL CLASSES ARE IN SCOPE
                        for (Item lIt : MClass.MY_CAT.getNodes().getItemsList())
                        {
                            aOut.put(lIt.getGID().getName(),(MClass) lIt);
                        }
                    }
                    else
                    {
                        // NOT A WILD CARD, FIND SPECIFIC CLASS
                        MClass lIt = MClass.get(getLID().getName());
                        if (null != lIt)
                        {
                            aOut.put(lIt.getGID().getName(), lIt);
                        }
                        else
                        {
                            Severity.DEATH.report("ownership rule class retrieval", "", "",
                                                  "no such class: " + getLID().getName());
                        }
                    }
                }
                break;

            case MODULE:

                {
                    MModuleRule lModuleRule = (MModuleRule) getParent();
                    if (lModuleRule.isWildCard())
                    {
                        // MODULE IS WILDCARD --> ANY MODULE, MATCH LOCAL NAMES OF THE CLASSES
                        if (isWildCard())
                        {
                            // ALL CLASSES IN THE UNIVERSE
                            for (Item lIt : MClass.MY_CAT.getNodes().getItemsList())
                            {
                                aOut.put(lIt.getGID().getName(),(MClass) lIt);
                            }
                        }
                        else
                        {
                            // MATCH LOCAL NAMES OF THE CLASSES IN THE UNIVERSE
                            for (Item lIt : MClass.MY_CAT.getNodes().getItemsList())
                            {
                                if (lIt.getLID().getName().equals(getLID().getName()))
                                {
                                    aOut.put(lIt.getGID().getName(), (MClass) lIt);
                                }
                            }
                        }
                    }
                    else
                    {
                        // MODULE IS SPECIFIED
                        Module lModule = Module.get(lModuleRule.getLID().getName());
                        if (null != lModule)
                        {
                            LinkedList<Item> lIts = new LinkedList<Item>();
                            lModule.getChildItems(MClass.MY_CAT, lIts);
                            for (Item lIt : lIts)
                            {
                                aOut.put(lIt.getGID().getName(),(MClass) lIt);
                            }
                        }
                        else
                        {
                            Severity.DEATH.report("ownership rule class retrieval", "", "",
                                                  "no such module: " + lModuleRule.getLID().getName());
                        }
                    }
                }
        }
    }
}


text:
/** @fileoverview
  * This tool executes an SQL select to PostgreSQL
  * database and returns resulted field list in JSON. */

var fs = require('fs');
var pg = require('pg.js');
var Promise = require('es6-promise').Promise;

/** @constructor
 * Deferred encapsulates a promise that gets fulfilled by outside code. */
var Deferred = function () {
    var self = this;

    this.promise = new Promise(function (resolve, reject) {
        self.resolve = resolve;
        self.reject = reject;
    });
};

/** Return a new function that calls fn making it see the desired scope
 * through its "this" variable.
 * @param {Object} scope Variable fn should see as "this".
 * @param {function()} fn Function to call. */
function bindToScope(scope, fn) {
    return function () {
        fn.apply(scope, arguments);
    };
}

/** @constructor
 * PostgreSQL database interface.
 * Simple wrapper to use promises with pg.js. */
var PgDatabase = function () {
    this.client = null;
};

/** @param {Object} client:
 * host, port, database, user and password. */
PgDatabase.prototype.connect = function (client) {
    var defer = new Deferred();

    this.client = client;
    defer.resolve();
    return (defer.promise);
};

/** Execute query without reading any results. */
PgDatabase.prototype.exec = function () {
    var query = this.client.query.apply(this.client, arguments);
    var defer = new Deferred();

    query.on('error', function (err) {
        defer.reject(err);
    });

    query.on('end', function (state) {
        defer.resolve(state);
    });

    return (defer.promise);
};

/** Send query to database and read a single result row. */
PgDatabase.prototype.queryResult = function () {
    var query = this.client.query.apply(this.client, arguments);
    var defer = new Deferred();
    var result = [];

    query.on('row', function (row) {
        result.push(row);
    });

    query.on('error', function (err) {
        defer.reject(err);
    });

    query.on('end', function (state) {
        if (!result) return (defer.reject('Not found'));
        defer.resolve(result);
    });

    return (defer.promise);
};

PgDatabase.prototype.begin = function () {
    return (this.exec('BEGIN TRANSACTION'));
};

PgDatabase.prototype.commit = function () {
    return (this.exec('COMMIT'));
};

PgDatabase.prototype.rollback = function () {
    return (this.exec('ROLLBACK'));
};

/** @constructor
 * FieldSelector executes slq select to
 * an SQL database. */
var FieldSelector = function () {
    this.db = null;
};

/** @param {string} client Name of JSON file with database address and credentials. */
FieldSelector.prototype.connect = function (client) {
    var defer = new Deferred();

    this.db = new PgDatabase();

    return (this.db.connect(client));
};
/**
 * Read parameter values from database
 * @param id config id
 * @returns {Promise}
 */
FieldSelector.prototype.readFields = function() {
    var self = this,
        sql = 'SELECT search_tag FROM sld_type ORDER BY id';
    console.log("readFields SQL: ", sql);
    return (self.db.queryResult(sql));
};

/** Roll back current transaction and close connection. */
FieldSelector.prototype.abort = function () {
    return (this.db.rollback());
};

/** Commit current transaction and close connection. */
FieldSelector.prototype.finish = function () {
    return (this.db.commit());
};

/** Top function, to execute sql statement
 */
exports.select_fields = function (client, cb) {

    console.log("in select_fields");

    var selector = new FieldSelector(),
        callback = cb;

    var connected = selector.connect(client);

    // Read parameter values
    var ready = connected.then(function () {
        return (selector.readFields());
    });

    ready.catch(function (err) {
        console.error(err);
    });

    ready.then(function(readyResult) {
        var i;
        var result = [];
        // Convert result to JSON object
        for (i=0; i<readyResult.length; i++) {
            result.push(JSON.parse('{"path":['+readyResult[i].search_tag+']}'));
        }
        console.log('Select fields success!');
        callback(false, result);
    });
};


text:
<?xml version="1.0" encoding="utf-8"?>
<addon addon_id="SV_XentagTending" title="SV: XenTag Tending sidebar widget" version_string="1.0.1" version_id="10001" url="" install_callback_class="" install_callback_method="" uninstall_callback_class="" uninstall_callback_method="">
  <admin_navigation/>
  <admin_permissions/>
  <admin_style_properties/>
  <admin_templates/>
  <admin_template_modifications/>
  <code_events/>
  <code_event_listeners>
    <listener event_id="load_class" execute_order="10" callback_class="SV_XentagTrending_Listener" callback_method="load_class" active="1" hint="XenForo_ControllerPublic_Forum" description="XenForo_ControllerPublic_Forum"/>
    <listener event_id="load_class" execute_order="10" callback_class="SV_XentagTrending_Listener" callback_method="load_class" active="1" hint="Tinhte_XenTag_Model_Tag" description="Tinhte_XenTag_Model_Tag"/>
  </code_event_listeners>
  <cron/>
  <email_templates/>
  <email_template_modifications/>
  <optiongroups/>
  <permissions>
    <permission_groups/>
    <permissions/>
    <interface_groups/>
  </permissions>
  <phrases>
    <phrase title="sv_xentagtrending_cloud" version_id="10000" version_string="1.0.0"><![CDATA[Trending Tag Cloud]]></phrase>
  </phrases>
  <route_prefixes/>


text:
using System;
using System.Text.Json.Serialization;

namespace LinqToTwitter
{
    public record ListResponseData
    {
        /// <summary>
        /// When created
        /// </summary>
        [JsonPropertyName("created_at")]
        public DateTime CreatedAt { get; set; }

        /// <summary>
        /// What this list is about
        /// </summary>
        [JsonPropertyName("description")]
        public string? Description { get; set; }

        /// <summary>
        /// Number of users following this list
        /// </summary>
        [JsonPropertyName("follower_count")]
        public int FollowerCount { get; set; }

        /// <summary>
        /// List ID
        /// </summary>
        [JsonPropertyName("id")]
        public string? ID { get; set; }

        /// <summary>
        /// Number of users are members in this list
        /// </summary>
        [JsonPropertyName("member_count")]
        public int MemberCount { get; set; }

        /// <summary>
        /// List Name
        /// </summary>
        [JsonPropertyName("name")]
        public string? Name { get; set; }

        /// <summary>
        /// ID of user who is the list owner
        /// </summary>
        [JsonPropertyName("owner_id")]
        public string? OwnerID { get; set; }

        /// <summary>
        /// Can only the owner see this list?
        /// </summary>
        [JsonPropertyName("private")]
        public bool Private { get; set; }

        //
        // The following properties are only returned for commands
        //

        [JsonPropertyName("updated")]
        public bool Updated { get; set; }

        [JsonPropertyName("deleted")]
        public bool Deleted { get; set; }

        [JsonPropertyName("is_member")]
        public bool IsMember { get; set; }

        [JsonPropertyName("following")]
        public bool Following { get; set; }

        [JsonPropertyName("pinned")]
        public bool Pinned { get; set; }
    }
}


text:
package com.facebook.buck.jvm.java;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.facebook.buck.cli.BuildTargetNodeToBuildRuleTransformer;
import com.facebook.buck.model.BuildTargetFactory;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.FakeBuildRuleParamsBuilder;
import com.facebook.buck.rules.FakeSourcePath;
import com.facebook.buck.rules.PathSourcePath;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.testutil.FakeProjectFilesystem;
import com.google.common.base.Optional;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PrebuiltJarTest {

  private static final Path PATH_TO_JUNIT_JAR = Paths.get("third-party/java/junit/junit-4.11.jar");

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  private PrebuiltJar junitJarRule;
  private FakeProjectFilesystem filesystem;

  @Before
  public void setUp() throws IOException {
    filesystem = new FakeProjectFilesystem(temp.newFolder());

    BuildRuleParams buildRuleParams =
        new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//lib:junit"))
            .setProjectFilesystem(filesystem)
            .build();

    junitJarRule = new PrebuiltJar(
        buildRuleParams,
        new SourcePathResolver(
            new BuildRuleResolver(TargetGraph.EMPTY, new BuildTargetNodeToBuildRuleTransformer())),
        new FakeSourcePath("abi.jar"),
        new PathSourcePath(filesystem, PATH_TO_JUNIT_JAR),
        Optional.<SourcePath>of(new FakeSourcePath("lib/junit-4.11-sources.jar")),
        /* gwtJar */ Optional.<SourcePath>absent(),
        Optional.of("http://junit-team.github.io/junit/javadoc/latest/"),
        /* mavenCoords */ Optional.<String>absent(),
        /* provided */ false);
  }

  @Test
  public void testGetJavaSrcsIsEmpty() {
    assertTrue(junitJarRule.getJavaSrcs().isEmpty());
  }

  @Test
  public void testGetAnnotationProcessingDataIsEmpty() {
    assertFalse(junitJarRule.getGeneratedSourcePath().isPresent());
  }
}


text:
Implementation of Bernard Chazelle's algorithm to Triangulate Polygon in Linear Time

Input polygon files are lists of ordered pairs, for example a square:

> -5, -5

> -5, 5

> 5, 5

> 5, -5

Output files have the keyword "diagonals" followed by an ordered list of indices after them:
> diagonals
> 0, 2

__Installation__

To use visulizations,

>  pip install pygame --user


text:
package com.google.android.gms.common.api;

import android.app.Activity;
import android.app.PendingIntent;
import android.os.Parcel;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
import com.google.android.gms.internal.ee;
import com.google.android.gms.internal.ee.a;

public final class Status
  implements Result, SafeParcelable
{
  public static final StatusCreator CREATOR = new StatusCreator();
  public static final Status nA = new Status(0, null, null);
  public static final Status nB = new Status(14, null, null);
  public static final Status nC = new Status(15, null, null);
  private final int kg;
  private final int mC;
  private final PendingIntent mPendingIntent;
  private final String nD;

  public Status(int paramInt)
  {
    this(1, paramInt, null, null);
  }

  Status(int paramInt1, int paramInt2, String paramString, PendingIntent paramPendingIntent)
  {
    this.kg = paramInt1;
    this.mC = paramInt2;
    this.nD = paramString;
    this.mPendingIntent = paramPendingIntent;
  }

  public Status(int paramInt, String paramString, PendingIntent paramPendingIntent)
  {
    this(1, paramInt, paramString, paramPendingIntent);
  }

  private String bh()
  {
    if (this.nD != null)
      return this.nD;
    return CommonStatusCodes.getStatusCodeString(this.mC);
  }

  final PendingIntent bs()
  {
    return this.mPendingIntent;
  }

  final String bt()
  {
    return this.nD;
  }

  @Deprecated
  public final ConnectionResult bu()
  {
    return new ConnectionResult(this.mC, this.mPendingIntent);
  }

  public final int describeContents()
  {
    return 0;
  }

  public final boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof Status))
      return false;
    Status localStatus = (Status)paramObject;
    return (this.kg == localStatus.kg) && (this.mC == localStatus.mC) && (ee.equal(this.nD, localStatus.nD)) && (ee.equal(this.mPendingIntent, localStatus.mPendingIntent));
  }

  public final PendingIntent getResolution()
  {
    return this.mPendingIntent;
  }

  public final Status getStatus()
  {
    return this;
  }

  public final int getStatusCode()
  {
    return this.mC;
  }

  final int getVersionCode()
  {
    return this.kg;
  }

  public final boolean hasResolution()
  {
    return this.mPendingIntent != null;
  }

  public final int hashCode()
  {
    Object[] arrayOfObject = new Object[4];
    arrayOfObject[0] = Integer.valueOf(this.kg);
    arrayOfObject[1] = Integer.valueOf(this.mC);
    arrayOfObject[2] = this.nD;
    arrayOfObject[3] = this.mPendingIntent;
    return ee.hashCode(arrayOfObject);
  }

  public final boolean isInterrupted()
  {
    return this.mC == 14;
  }

  public final boolean isSuccess()
  {
    return this.mC <= 0;
  }

  public final void startResolutionForResult(Activity paramActivity, int paramInt)
  {
    if (!hasResolution())
      return;
    paramActivity.startIntentSenderForResult(this.mPendingIntent.getIntentSender(), paramInt, null, 0, 0, 0);
  }

  public final String toString()
  {
    return ee.e(this).a("statusCode", bh()).a("resolution", this.mPendingIntent).toString();
  }

  public final void writeToParcel(Parcel paramParcel, int paramInt)
  {
    StatusCreator.a(this, paramParcel, paramInt);
  }
}

/* Location:           /Users/vikas/Documents/Mhacks_Real_app/classes-dex2jar.jar
 * Qualified Name:     com.google.android.gms.common.api.Status
 * JD-Core Version:    0.6.2
 */


text:
  Todos
  About

<button class="dialog-open-button" (click)="openDialog()">Open dialog</button>

<router-outlet></router-outlet>


text:
/**
 * "First, solve the problem. Then, write the code. -John Johnson"
 * "Or use Vangav M"
 * www.vangav.com
 * */

/**
 * MIT License
 *
 * Copyright (c) 2016 Vangav
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 * */

/**
 * Community
 * Facebook Group: Vangav Open Source - Backend
 *   fb.com/groups/575834775932682/
 * Facebook Page: Vangav
 *   fb.com/vangav.f
 *
 * Third party communities for Vangav Backend
 *   - play framework
 *   - cassandra
 *   - datastax
 *
 * Tag your question online (e.g.: stack overflow, etc ...) with
 *   #vangav_backend
 *   to easier find questions/answers online
 * */

package com.vangav.backend.dispatcher.worker.worker_generator;

import java.util.ArrayList;

import com.vangav.backend.content.formatting.JavaFormatterInl;
import com.vangav.backend.files.FileLoaderInl;
import com.vangav.backend.files.FileWriterInl;
import com.vangav.backend.system.CommandLineInl;

/**
 * @author mustapha
 * fb.com/mustapha.abdallah
 */
/**
 * BackendGenerator handles creating a new backend based on vangav backend
 *   (vos_backend aka vangav open source backend)
 */
public class WorkerGeneratorInl {

  //disable default instantiation
  private WorkerGeneratorInl () {}

  private static final String kWorkerProjectSuffix = "_worker";

  /**
   * generateBackend
   * 1- creates play java project
   * 2- adds vos_backend.jar to the project's lib
   * * if config json files are present for controllers and cassandra?
   * *3- generates controllers code and properties files
   * *4- generates cassandra client code and properties files
   * 5- adds default classes/properties
   * 6- generates scripts for run, compile, etc ...
   * @param projectName
   * @param javaPackageName
   * @param addNotifications
   * @param addCassandra
   * @throws Exception
   */
  public static void generateWorker (
    String projectName,
    final String javaPackageName,
    final boolean addNotifications,
    final boolean addCassandra) throws Exception {

    projectName += kWorkerProjectSuffix;

    // write project generation script and execute it
    generateNewPlayProject(
      projectName);

    // modify play generated content (add comments, suppress warnings, etc...)
    modifyPlayGeneratedContent(projectName);

    // add vangav m client
    addVangavMClient(projectName);

    // add default classes/properties that should be in all play projects
    addDefaultsToPlayProject(
      projectName,
      javaPackageName,
      addNotifications,
      addCassandra);

    // write run, compile, debug, clean and dist scripts
    addPlayProjectScripts(projectName);

    // compile project
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
        + "cd ../../"
        + projectName
        + "\n"
        + "../vos_backend/play-2.2.6/play compile &> /dev/null\n",
      "../../"
        + projectName
        + "/compile_project.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/compile_project.sh");

    CommandLineInl.executeCommand(
      "../../"
      + projectName
      + "/compile_project.sh");

    CommandLineInl.executeCommand(
      "rm ../../"
      + projectName
      + "/compile_project.sh");

    // finished
    System.out.println(
      "\nFinished creating worker project ["
      + projectName
      + "]\n");
  }

  private static final String kPlayProjectCreationScriptFormat =
    "#!/bin/bash\n"
    + "cd ../..\n"
    + "echo -e \"%s\\n2\\n\" | ./vos_backend/play-2.2.6/play new %s &> /dev/null\n"
    + "cd %s\n"
    + "mkdir lib\n"
    + "cp ../vos_backend/dist/vos_backend.jar ./lib\n";
  private static final String kMakeEclipseProject =
    "../vos_backend/play-2.2.6/play eclipse &> /dev/null\n";
  /**
   * generateNewPlayProject
   * creates a new play project
   * @param projectName
   * @throws Exception
   */
  private static void generateNewPlayProject (
    final String projectName) throws Exception {

    // write project generation script and execute it

    String playProjectCreationScript =
      String.format(
        kPlayProjectCreationScriptFormat,
        projectName,
        projectName,
        projectName);

    // make the new project eclipse compatible
    playProjectCreationScript += kMakeEclipseProject;

    System.out.println(
      "Creating worker project ["
      + projectName
      + "], it will take 1-3 minutes to complete ...");

    FileWriterInl.writeTextFile(
      playProjectCreationScript,
      "./_vangav_play_project_creation_script.sh",
      false);

    CommandLineInl.executeMultipleCommands(
      "chmod +x _vangav_play_project_creation_script.sh",
      "./_vangav_play_project_creation_script.sh",
      "rm _vangav_play_project_creation_script.sh");
  }

  /**
   * modifyPlayGeneratedContent
   * modify play generated content (add comments, suppress warnings, etc...)
   * @param projectName
   * @throws Exception
   */
  private static void modifyPlayGeneratedContent (
    final String projectName) throws Exception {

    // comment out the default route generated by play in conf/routes

    ArrayList<String> confRoutesLines =
      FileLoaderInl.loadTextFileLines(
        "../../"
        + projectName
        + "/conf/routes");

    String currLine;
    for (int i = 0; i < confRoutesLines.size(); i++) {

      currLine = confRoutesLines.get(i).trim();

      if (currLine.startsWith("GET") == true) {

        confRoutesLines.remove(i);
        confRoutesLines.add(i, "# " + currLine + "\n");
        confRoutesLines.add(i, "# Commented out route generated by play\n");

        break;
      }
    }

    FileWriterInl.writeTextFile(
      confRoutesLines,
      "../../"
        + projectName
        + "/conf/routes",
      false,
      false);

    // modifdy Application.java generated by play to emphasize that it's
    //   generated and not used

    ArrayList<String> applicationLines =
      FileLoaderInl.loadTextFileLines(
        "../../"
        + projectName
        + "/app/controllers/Application.java");

    for (int i = 0; i < applicationLines.size(); i ++) {

      if (applicationLines.get(i).startsWith("public") == true) {

        applicationLines.add(i,
          "/**\n"
          + " * This is the default controller generated by play\n"
          + " *   and is disabled in the conf/routes files\n"
          + " * Note: unless you know what you are doing, keep it here and\n"
          + " *         don't edit or use it\n"
          + " * */\n\n"

          + "@SuppressWarnings(\"unused\")\n");

        break;
      }
    }

    FileWriterInl.writeTextFile(
      applicationLines,
      "../../"
        + projectName
        + "/app/controllers/Application.java",
      false,
      false);
  }

  /**
   * addVangavMClient
   * adds vangav m client to newly generated project
   * @param projectName
   * @throws Exception
   */
  private static void addVangavMClient (
    final String projectName) throws Exception {

    FileWriterInl.mkdirs(
      "../../"
      + projectName
      + "/vangav_m/solutions",
      false);

    CommandLineInl.executeCommand(
      "cp assets/vangav_m_json_client.jar "
      + "../../"
      + projectName
      + "/vangav_m/vangav_m_json_client.jar");
  }

  /**
   * addDefaultsToPlayProject
   * add default classes/properties like Global.java, etc ...
   * @param projectName
   * @throws Exception
   */
  private static void addDefaultsToPlayProject (
    final String projectName,
    final String javaPackageName,
    final boolean addNotifications,
    final boolean addCassandra) throws Exception {

    // add default Global class

    ArrayList<String> defaultGlobalLines =
      FileLoaderInl.loadTextFileLines(
        "../src/com/vangav/backend/"
        + "dispatcher/worker/worker_generator/defaults/Global.java");

    for (int i = 0; i < defaultGlobalLines.size(); i ++) {

      if (defaultGlobalLines.get(i).startsWith("package") == true) {

        defaultGlobalLines.remove(i);

        break;
      }
    }

    if (addCassandra == true) {

      for (int i = 0; i < defaultGlobalLines.size(); i ++) {

        if (defaultGlobalLines.get(i).startsWith(
              "import com.vangav.backend.properties") == true) {

          defaultGlobalLines.add(
            i + 1,
            "import com.vangav.backend.cassandra.Cassandra;\n");
        } else if (defaultGlobalLines.get(i).contains(
                     "// cassandra connection") == true) {

          defaultGlobalLines.add(
            i + 1,
            "\n        Cassandra.connect();\n");
        } else if (defaultGlobalLines.get(i).contains(
                    "// e.g.: disconnect") == true) {

          defaultGlobalLines.add(
            i + 1,
            "      Cassandra.i().disconnect();\n");
        }
      }
    }

    FileWriterInl.writeTextFile(
      defaultGlobalLines,
      "../../"
        + projectName
        + "/app/Global.java",
      false,
      false);

    // add worker controller directory

    String workerControllerDirPath =
      JavaFormatterInl.getPathToPackage(
        "../../"
          + projectName,
        JavaFormatterInl.kPlaySrcDirName,
        javaPackageName,
        projectName,
        "controllers",
        "worker");

    FileWriterInl.mkdirs(
      workerControllerDirPath,
      false);

    String workerControllerPackageName =
      JavaFormatterInl.getPackageName(
        javaPackageName,
        projectName,
        "controllers",
        "worker");

    // add controller class

    ArrayList<String> controllerLines =
      FileLoaderInl.loadTextFileLines(
        "../src/com/vangav/backend/"
        + "dispatcher/worker/worker_generator/defaults/ControllerWorker.java");

    for (int i = 0; i < controllerLines.size(); i ++) {

      if (controllerLines.get(i).startsWith("package") == true) {

        controllerLines.remove(i);
        controllerLines.add(
          i,
          "package "
            + workerControllerPackageName
            + ";\n");

        break;
      }
    }

    FileWriterInl.writeTextFile(
      controllerLines,
      workerControllerDirPath
        + "/ControllerWorker.java",
      false,
      false);

    // add handler class

    ArrayList<String> handlerLines =
      FileLoaderInl.loadTextFileLines(
        "../src/com/vangav/backend/"
        + "dispatcher/worker/worker_generator/defaults/HandlerWorker.java");

    for (int i = 0; i < handlerLines.size(); i ++) {

      if (handlerLines.get(i).startsWith("package") == true) {

        handlerLines.remove(i);
        handlerLines.add(
          i,
          "package "
            + workerControllerPackageName
            + ";\n");

        break;
      }
    }

    FileWriterInl.writeTextFile(
      handlerLines,
      workerControllerDirPath
        + "/HandlerWorker.java",
      false,
      false);

    // modify routes

    ArrayList<String> confRoutesLines =
      FileLoaderInl.loadTextFileLines(
        "../../"
        + projectName
        + "/conf/routes");

    confRoutesLines.add(
      "\n\n"
      + "# Worker route\n"
      + "POST /Worker       "
      + workerControllerPackageName
      + ".ControllerWorker.postWorker()\n");

    FileWriterInl.writeTextFile(
      confRoutesLines,
      "../../"
        + projectName
        + "/conf/routes",
      false,
      false);

    // copy lib files
    if (addCassandra == true) {

      CommandLineInl.executeCommand(
        "cp ../lib/cassandra-driver-core-2.1.4.jar "
        + "../../"
        + projectName
        + "/lib/cassandra-driver-core-2.1.4.jar");

      CommandLineInl.executeCommand(
        "cp ../lib/metrics-core-3.0.2.jar "
        + "../../"
        + projectName
        + "/lib/metrics-core-3.0.2.jar");
    }

    // copy properties files

    FileWriterInl.mkdirs(
      "../../"
        + projectName
        + "/conf/prop",
      false);

    CommandLineInl.executeCommand(
      "cp ../prop/thread_pool_properties.prop ../../"
      + projectName
      + "/conf/prop/");

    if (addNotifications == true) {

      CommandLineInl.executeCommand(
        "cp ../prop/android_notification_properties.prop ../../"
        + projectName
        + "/conf/prop/");
      CommandLineInl.executeCommand(
        "cp ../prop/apple_notification_properties.prop ../../"
        + projectName
        + "/conf/prop/");
    }

    if (addCassandra == true) {

      CommandLineInl.executeCommand(
        "cp ../prop/cassandra_properties.prop ../../"
        + projectName
        + "/conf/prop/");
    }
  }

  /**
   * addPlayProjectScripts
   * write run, compile, debug, clean and dist scripts
   * @param projectName
   * @throws Exception
   */
  private static void addPlayProjectScripts (
    final String projectName) throws Exception {

    // write run, compile, debug, clean and dist scripts

    // run
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
        + "# used to run the service in dev mode on port 8000 or arg port\n"
        + "#   with live compiling on code change/save\n"
        + "#   for production, use dist.sh\n\n"

        + "port=8000\n\n"

        + "if [ \"$1\" -ne 0 ]\n"
        + "then\n"
        + "  port=\"$1\"\n"
        + "fi\n\n"

        + "echo \"using port $port\"\n\n"

        + "../vos_backend/play-2.2.6/play \"run $port\"\n",
      "../../"
        + projectName
        + "/_run.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_run.sh");

    // compile
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
        + "# used to compile the service\n"
        + "../vos_backend/play-2.2.6/play compile\n",
      "../../"
        + projectName
        + "/_compile.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_compile.sh");

    // debug
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
        + "# used to run the service in debug mode\n\n"
        + "../vos_backend/play-2.2.6/play debug\n",
      "../../"
        + projectName
        + "/_debug.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_debug.sh");

    // clean
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
          + "# used to clean the project's build\n\n"
        + "../vos_backend/play-2.2.6/play clean\n",
      "../../"
        + projectName
        + "/_clean.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_clean.sh");

    // dist
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
          + "# used to generate the production version\n# "
          + projectName + "-1.0-SNAPSHOT.zip\n"
          + "# @target/universal/ \n\n"
        + "../vos_backend/play-2.2.6/play dist\n",
      "../../"
        + projectName
        + "/_dist.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_dist.sh");

    // eclipsify
    FileWriterInl.writeTextFile(
      "#!/bin/bash\n\n"
          + "# used to make the project eclipse-ready\n"
          + "  # (useful when cloning the project from a repo,\n"
          + "  # or when entering N for making the project\n"
          + "  #   eclipse-ready on generation)\n\n"
        + "../vos_backend/play-2.2.6/play eclipse\n",
      "../../"
        + projectName
        + "/_eclipsify.sh",
      false);
    CommandLineInl.executeCommand(
      "chmod +x ../../"
      + projectName
      + "/_eclipsify.sh");
  }
}


text:
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using IdentityModel.Client;
using SFA.DAS.Authentication;
using SFA.DAS.EAS.Domain.Configuration;
using SFA.DAS.EmployerUsers.WebClientComponents;

namespace SFA.DAS.EAS.Web.Authentication
{
    public class OwinAuthenticationService : IAuthenticationService
    {
        private readonly EmployerApprenticeshipsServiceConfiguration _configuration;
        private readonly HttpContextBase _httpContext;

        public OwinAuthenticationService(EmployerApprenticeshipsServiceConfiguration configuration, HttpContextBase httpContext)
        {
            _configuration = configuration;
            _httpContext = httpContext;
        }

        public string GetClaimValue(string key)
        {
            var claimIdentity = ((ClaimsIdentity)HttpContext.Current.User.Identity).Claims.FirstOrDefault(c => c.Type == key);
            return claimIdentity == null ? "" : claimIdentity.Value;
        }

        public bool HasClaim(string type, string value)
        {
            return ((ClaimsIdentity)_httpContext.User.Identity).HasClaim(type, value);
        }

        public bool IsUserAuthenticated()
        {
            return HttpContext.Current.GetOwinContext().Authentication.User.Identity.IsAuthenticated;
        }

        public void SignOutUser()
        {
            var owinContext = HttpContext.Current.GetOwinContext();
            var authenticationManager = owinContext.Authentication;

            authenticationManager.SignOut("Cookies");
        }

        public bool TryGetClaimValue(string key, out string value)
        {
            var identity = _httpContext.User.Identity as ClaimsIdentity;
            var claim = identity?.Claims.FirstOrDefault(c => c.Type == key);

            value = claim?.Value;

            return value != null;
        }

        public async Task UpdateClaims()
        {
            var constants = new Constants(_configuration.Identity);
            var userInfoEndpoint = constants.UserInfoEndpoint();
            var accessToken = GetClaimValue("access_token");
            var userInfoClient = new UserInfoClient(new Uri(userInfoEndpoint), accessToken);
            var userInfo = await userInfoClient.GetAsync();
            var identity = (ClaimsIdentity)HttpContext.Current.User.Identity;

            foreach (var claim in userInfo.Claims.ToList())
            {
                if (claim.Item1.Equals(DasClaimTypes.Email))
                {
                    var emailClaim = identity.Claims.FirstOrDefault(c => c.Type == "email");
                    var emailClaim2 = identity.Claims.FirstOrDefault(c => c.Type == DasClaimTypes.Email);

                    identity.RemoveClaim(emailClaim);
                    identity.RemoveClaim(emailClaim2);
                    identity.AddClaim(new Claim("email", claim.Item2));
                    identity.AddClaim(new Claim(DasClaimTypes.Email, claim.Item2));
                }

                if (claim.Item1.Equals(DasClaimTypes.RequiresVerification))
                {
                    var requiresValidationClaim = identity.Claims.FirstOrDefault(c => c.Type == DasClaimTypes.RequiresVerification);

                    if (requiresValidationClaim != null)
                    {
                        identity.RemoveClaim(requiresValidationClaim);
                    }

                    identity.AddClaim(new Claim(DasClaimTypes.RequiresVerification, claim.Item2));
                }
            }
        }
    }
}