text:
#ifndef __MIC__
#include "FileProcessing.h"
#include "common.h"
#include <zlib.h>
#undef __USE_BSD
#include <dirent.h>
#include <cassert>
#include <sstream>
#include "ErrorHandling.h"
#include <hdf5.h>
#include <hdf5_hl.h>

/*************************
read a bunch of raw matrix files
input: the file directory, the file type e.g. ".nii.gz"
output: an array of raw matrix structs, the number of subjects (reference
parameter)
**************************/
RawMatrix** ReadGzDirectory(const char* filepath, const char* filetype,
                            int& nSubs) {
  DIR* pDir;
  struct dirent* dp;
  char fullfilename[MAXFILENAMELENGTH];
  std::string strFilenames[MAXSUBJS];
  if ((pDir = opendir(filepath)) == NULL) {
    FATAL("invalid directory");
  }
  nSubs = 0;
  while ((dp = readdir(pDir)) != NULL) {
    if ((strstr(dp->d_name, filetype)) != NULL) {
      nSubs++;
    }
  }
  closedir(pDir);
  if ((pDir = opendir(filepath)) == NULL) {
    FATAL("invalid directory: " << filepath);
  }
  RawMatrix** r_matrices = new RawMatrix* [nSubs];
  int count = 0;
  while ((dp = readdir(pDir)) != NULL) {
    if ((strstr(dp->d_name, filetype)) != NULL) {
      sprintf(fullfilename, "%s", filepath);
      strcat(fullfilename, dp->d_name);
      strFilenames[count] = std::string(fullfilename);
      count++;
    }
  }
  sort(strFilenames, strFilenames + count);
  for (int i = 0; i < count; i++) {
    // cout<<"file "<<i<<": "<<strFilenames[i]<<endl; // output the file name to
    // know the file sequance
    // r_matrices[i] = ReadGzData(strFilenames[i], i);
    r_matrices[i] = ReadNiiGzData(strFilenames[i], i);
  }
  closedir(pDir);
  return r_matrices;
}

/*************************
read matrix data from gz files, the first eight bytes are two 32-bit ints,
indicating the row and col of the following matrix
input: the gz file name, subject id
output: the raw matrix data
**************************/
RawMatrix* ReadGzData(std::string fileStr, int sid) {
  const char* file = fileStr.c_str();
  gzFile fp = gzopen(file, "rb");
  // cout<<sid+1<<": "<<file<<endl;
  if (fp == NULL) {
    FATAL("file not found: " << file);
  }
  RawMatrix* r_matrix = new RawMatrix();
  r_matrix->sid = sid;
  int row, col;
  gzread(fp, &row, sizeof(int));
  gzread(fp, &col, sizeof(int));
  size_t startPos = fileStr.find_last_of('/');
  size_t endPos = fileStr.find_first_of('.', startPos);
  std::string sname = fileStr.substr(
      startPos + 1, endPos - startPos - 1);  // assuming that the subject name
                                             // doesn't contain '.'
  r_matrix->sname = sname;
  r_matrix->row = row;
  r_matrix->col = col;
  r_matrix->matrix = new float[row * col];
  uint16* data = new uint16[row * col];
  gzread(fp, data, row * col * sizeof(uint16));
#pragma omp parallel for
  for (int i = 0; i < row * col; i++)  // not efficient
  {
    r_matrix->matrix[i] = (double)data[i];
  }
  gzclose(fp);
  delete[] data;
  return r_matrix;
}

/*************************
read matrix data from nii.gz files
input: the gz file name, subject id
output: the raw matrix data
**************************/
RawMatrix* ReadNiiGzData(std::string fileStr, int sid) {
  const char* file = fileStr.c_str();
  nifti_image* nim;
  nim = nifti_image_read(file, 1);
  if (nim == NULL) {
    FATAL("file not found: " << file);
  }
  assert(nim);
  /*cout<<"fname: "<<nim->fname<<endl;
  cout<<"iname: "<<nim->iname<<endl;
  cout<<"nifti_type: "<<nim->nifti_type<<endl;
  cout<<"dx: "<<nim->dx<<endl;
  cout<<"dy: "<<nim->dy<<endl;
  cout<<"dz: "<<nim->dz<<endl;
  cout<<"byteorder: "<<nim->byteorder<<endl;
  FATAL("just debugging");*/
  RawMatrix* r_matrix = new RawMatrix();
  size_t startPos = fileStr.find_last_of('/');
  size_t endPos = fileStr.find_first_of('.', startPos);
  std::string sname = fileStr.substr(
      startPos + 1, endPos - startPos - 1);  // assuming that the subject name
                                             // doesn't contain '.'
  r_matrix->sname = sname;
  r_matrix->sid = sid;
  r_matrix->row = nim->nx * nim->ny * nim->nz;
  r_matrix->col = nim->nt;
  r_matrix->nx = nim->nx;
  r_matrix->ny = nim->ny;
  r_matrix->nz = nim->nz;
  //std::cout << "DEBUG nim->datatype = " << nim->datatype << std::endl;
  //std::cout << "DEBUG nim->nx = " << nim->nx << std::endl;
  //std::cout << "DEBUG nim->ny = " << nim->ny << std::endl;
  //std::cout << "DEBUG nim->nz = " << nim->nz << std::endl;
  //std::cout << "DEBUG r_matrix->row = " << r_matrix->row << std::endl;
  //std::cout << "DEBUG r_matrix->col = " << r_matrix->col << std::endl;
  r_matrix->matrix = new float[r_matrix->row * r_matrix->col];
  short* data_short = NULL;
  unsigned short* data_ushort = NULL;
  int* data_int = NULL;
  float* data_float = NULL;
  double* data_double = NULL;
  switch (nim->datatype)  // now only get one type
  {
    case DT_SIGNED_SHORT:
      data_short = (short*)nim->data;
      break;
    case DT_UINT16:
      data_ushort = (unsigned short*)nim->data;
      break;
    case DT_SIGNED_INT:
      data_int = (int*)nim->data;
      break;
    case DT_FLOAT:
      data_float = (float*)nim->data;
      break;
    case DT_FLOAT64:
      data_double = (double*)nim->data;
      break;
    default:
      FATAL("wrong data type of data file! " << nim->datatype);
  }
#pragma omp parallel for
  // the follow if statements don't harm the performance, it's no significant
  // benifit to put the follow ifs to the previous switch
  for (int i = 0; i < r_matrix->row * r_matrix->col; i++) {
    int t1 = i / r_matrix->row;
    int t2 = i % r_matrix->row;
    if (data_short != NULL) {
      r_matrix->matrix[t2 * r_matrix->col + t1] =
          (float)data_short[i];  // transpose, because data is time (row) *
                                 // voxel (column), r_matrix wants voxel (row) *
                                 // time (column)
    }
    if (data_ushort != NULL) {
      r_matrix->matrix[t2 * r_matrix->col + t1] =
          (float)data_ushort[i];  // transpose, because data is time (row) *
                                  // voxel (column), r_matrix wants voxel (row)
                                  // * time (column)
    }
    if (data_int != NULL) {
      r_matrix->matrix[t2 * r_matrix->col + t1] =
          (float)data_int[i];  // transpose, because data is time (row) * voxel
                               // (column), r_matrix wants voxel (row) * time
                               // (column)
    }
    if (data_float != NULL) {
      r_matrix->matrix[t2 * r_matrix->col + t1] =
          (float)data_float[i];  // transpose, because data is time (row) *
                                 // voxel (column), r_matrix wants voxel (row) *
                                 // time (column)
    }
    if (data_double != NULL) {
      r_matrix->matrix[t2 * r_matrix->col + t1] =
          (float)data_double[i];  // transpose, because data is time (row) *
                                  // voxel (column), r_matrix wants voxel (row)
                                  // * time (column)
    }
  }
  nifti_image_free(nim);
  return r_matrix;
}

/************************************************
Generate masked matrices using the mask file
input: the raw data array, the number of subjects, the mask file, bool value to determine if delete the raw data
output: a masked data array
*************************************************/
RawMatrix** GetMaskedMatrices(RawMatrix** r_matrices, int nSubs,
                              const char* maskFile, bool deleteData) {
  RawMatrix** masked_matrices = new RawMatrix* [nSubs];
  nifti_image* nim = nifti_image_read(maskFile, 1);
  int i;
  if (nim == NULL) {
    FATAL("file not found: ");
  }
  assert(nim);
  int* data_int = NULL;
  short* data_short = NULL;
  unsigned char* data_uchar = NULL;
  float* data_float = NULL;
  double* data_double = NULL;
  // cout<<nim->nx<<" "<<nim->ny<<" "<<nim->nz<<endl; FATAL("debug");
  switch (nim->datatype)  // now only get one type
  {
    case DT_SIGNED_INT:
      data_int = (int*)nim->data;
      break;
    case DT_SIGNED_SHORT:
      data_short = (short*)nim->data;
      break;
    case DT_UNSIGNED_CHAR:
      data_uchar = (unsigned char*)nim->data;
      break;
    case DT_FLOAT32:
      data_float = (float*)nim->data;
      break;
    case DT_FLOAT64:
      data_double = (double*)nim->data;
      break;
    default:
      FATAL("wrong data type of mask file!");
  }
#pragma omp parallel for
  for (i = 0; i < nSubs; i++)  // get only the masked voxels
  {
    RawMatrix* masked_matrix = new RawMatrix();
    masked_matrix->sid = r_matrices[i]->sid;
    masked_matrix->row = r_matrices[i]->row;
    masked_matrix->col = r_matrices[i]->col;
    masked_matrix->nx = r_matrices[i]->nx;
    masked_matrix->ny = r_matrices[i]->ny;
    masked_matrix->nz = r_matrices[i]->nz;
    int row = r_matrices[i]->row;
    int col = r_matrices[i]->col;
    masked_matrix->matrix = new float[row * col];
    float* src_mat = r_matrices[i]->matrix;
    float* dest_mat = masked_matrix->matrix;
    int count = 0;
    for (int j = 0; j < row; j++) {
      if (data_int != NULL && data_int[j]) {
        memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
               col * sizeof(float));
        count++;
      }
      if (data_short != NULL && data_short[j]) {
        memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
               col * sizeof(float));
        count++;
      }
      if (data_uchar != NULL && data_uchar[j]) {
        memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
               col * sizeof(float));
        count++;
      }
      if (data_float != NULL && data_float[j] >= 1 - TINYNUM) {
        memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
               col * sizeof(float));
        count++;
      }
      if (data_double != NULL && data_double[j] >= 1 - TINYNUM) {
        memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
               col * sizeof(float));
        count++;
      }
    }
    masked_matrix->row = count;  // update the row information
    masked_matrices[i] = masked_matrix;
    if (deleteData) {
      delete [] src_mat; // DEBUG tlw added brackets since deleting an array 2/23/2016
    }
  }
  nifti_image_free(nim);
  return masked_matrices;
}

/************************************************
Get a masked matrix using the mask file
input: the raw data matrix, the mask file
output: a masked matrix
*************************************************/
RawMatrix* GetMaskedMatrix(RawMatrix* r_matrix, const char* maskFile) {
  RawMatrix* masked_matrix = new RawMatrix;
  nifti_image* nim = nifti_image_read(maskFile, 1);
  int j;
  if (nim == NULL) {
    FATAL("file not found: " << maskFile);
  }
  assert(nim);  // quiet static analyzer
  int* data_int = NULL;
  short* data_short = NULL;
  unsigned char* data_uchar = NULL;
  float* data_float = NULL;
  double* data_double = NULL;
  // cout<<nim->nx<<" "<<nim->ny<<" "<<nim->nz<<endl; FATAL("debug");
  switch (nim->datatype)  // now only get one type
  {
    case DT_SIGNED_INT:
      data_int = (int*)nim->data;
      break;
    case DT_SIGNED_SHORT:
      data_short = (short*)nim->data;
      break;
    case DT_UNSIGNED_CHAR:
      data_uchar = (unsigned char*)nim->data;
      break;
    case DT_FLOAT32:
      data_float = (float*)nim->data;
      break;
    case DT_FLOAT64:
      data_double = (double*)nim->data;
      break;
    default:
      FATAL("wrong data type of mask file!");
  }
  masked_matrix->sid = r_matrix->sid;
  masked_matrix->row = r_matrix->row;
  masked_matrix->col = r_matrix->col;
  masked_matrix->nx = r_matrix->nx;
  masked_matrix->ny = r_matrix->ny;
  masked_matrix->nz = r_matrix->nz;
  int row = r_matrix->row;
  int col = r_matrix->col;
  masked_matrix->matrix = new float[row * col];
  float* src_mat = r_matrix->matrix;
  float* dest_mat = masked_matrix->matrix;
  int count = 0;
  for (j = 0; j < row; j++) {
    if (data_int != NULL && data_int[j]) {
      memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
             col * sizeof(float));
      count++;
    }
    if (data_short != NULL && data_short[j]) {
      memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
             col * sizeof(float));
      count++;
    }
    if (data_uchar != NULL && data_uchar[j]) {
      memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
             col * sizeof(float));
      count++;
    }
    if (data_float != NULL && data_float[j] >= 1 - TINYNUM) {
      memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
             col * sizeof(float));
      count++;
    }
    if (data_double != NULL && data_double[j] >= 1 - TINYNUM) {
      memcpy(&(dest_mat[count * col]), &(src_mat[j * col]),
             col * sizeof(float));
      count++;
    }
    masked_matrix->row = count;  // update the row information
  }
  nifti_image_free(nim);
  return masked_matrix;
}

/************************************************
Generate masked matrices using the mask file
input: the number of subjects, the raw data matrices, the mask file, the masked matrices
output: write in the masked matrices, delete the raw data matrices
*************************************************/
void GenerateMaskedMatrices(int nSubs, RawMatrix** r_matrices1, RawMatrix** r_matrices2,
                            const char* mask_file1, const char* mask_file2,
                            RawMatrix*** p_masked_matrices1, RawMatrix*** p_masked_matrices2) {
  float t = 0.0f;
  struct timeval start, end;
  gettimeofday(&start, 0);
  RawMatrix** masked_matrices1 = NULL;
  RawMatrix** masked_matrices2 = NULL;
  if (mask_file1 == NULL) {
    masked_matrices1 = r_matrices1;
  }
  else
  {
    if (r_matrices2 == r_matrices1) {
      masked_matrices1 = GetMaskedMatrices(r_matrices1, nSubs, mask_file1, false);
    }
    else {
      // after GetMaskedMatrices, the data stored in r_matrices have been deleted
      masked_matrices1 = GetMaskedMatrices(r_matrices1, nSubs, mask_file1, true);
    }
  }
  if (mask_file2 == NULL) {
    masked_matrices2 = r_matrices2;
  }
  else {
    // This is a special-case optimization (not required)
    if (r_matrices1 == r_matrices2 && strcmp(mask_file1, mask_file2) == 0) {
      masked_matrices2 = masked_matrices1;
      // DEBUG tlw added since it's now safe to delete r_matrices
      for (int i = 0; i < nSubs; i++) {
        delete [] r_matrices1[i]->matrix;
      }
    }
    else {
      // after GetMaskedMatrices, the data stored in r_matrices have been deleted
      masked_matrices2 = GetMaskedMatrices(r_matrices2, nSubs, mask_file2, true);
    }
  }
  gettimeofday(&end, 0);
  t += end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) * 0.000001;
  std::cout << "masked matrices generating done! " << "takes " << t << " s"<< std::endl;
  std::cout << "#voxels for mask1: " << masked_matrices1[0]->row
       << " #voxels for mask2: " << masked_matrices2[0]->row << std::endl;
  *p_masked_matrices1 = masked_matrices1;
  *p_masked_matrices2 = masked_matrices2;
  return;
}

/************************************************
Generate masked point location using the mask file
input: the raw point location, the number of voxels after masking, the mask file
output: a masked point location array
*************************************************/
VoxelXYZ* GetMaskedPts(VoxelXYZ* pts, int nMaskedVoxels, const char* maskFile) {
  VoxelXYZ* masked_pts = new VoxelXYZ[nMaskedVoxels];
  nifti_image* nim = nifti_image_read(maskFile, 1);
  int i;
  if (nim == NULL) {
    FATAL("file not found: " << maskFile);
  }
  assert(nim);  // quiet static analyzer
  int* data_int = NULL;
  short* data_short = NULL;
  unsigned char* data_uchar = NULL;
  float* data_float = NULL;
  double* data_double = NULL;
  switch (nim->datatype)  // now only get one type
  {
    case DT_SIGNED_INT:
      data_int = (int*)nim->data;
      break;
    case DT_SIGNED_SHORT:
      data_short = (short*)nim->data;
      break;
    case DT_UNSIGNED_CHAR:
      data_uchar = (unsigned char*)nim->data;
      break;
    case DT_FLOAT32:
      data_float = (float*)nim->data;
      break;
    case DT_FLOAT64:
      data_double = (double*)nim->data;
      break;
    default:
      FATAL("wrong data type of mask file!");
  }
  int nTotalVoxels = nim->nx * nim->ny * nim->nz;
  int count = 0;
  for (i = 0; i < nTotalVoxels; i++)  // get only the masked voxels
  {
    if (data_int != NULL && data_int[i]) {
      masked_pts[count].x = pts[i].x;
      masked_pts[count].y = pts[i].y;
      masked_pts[count].z = pts[i].z;
      count++;
    }
    if (data_short != NULL && data_short[i]) {
      masked_pts[count].x = pts[i].x;
      masked_pts[count].y = pts[i].y;
      masked_pts[count].z = pts[i].z;
      count++;
    }
    if (data_uchar != NULL && data_uchar[i]) {
      masked_pts[count].x = pts[i].x;
      masked_pts[count].y = pts[i].y;
      masked_pts[count].z = pts[i].z;
      count++;
    }
    if (data_float != NULL && data_float[i] >= 1 - TINYNUM) {
      masked_pts[count].x = pts[i].x;
      masked_pts[count].y = pts[i].y;
      masked_pts[count].z = pts[i].z;
      count++;
    }
    if (data_double != NULL && data_double[i] >= 1 - TINYNUM) {
      masked_pts[count].x = pts[i].x;
      masked_pts[count].y = pts[i].y;
      masked_pts[count].z = pts[i].z;
      count++;
    }
  }
  nifti_image_free(nim);
  return masked_pts;
}

/******************************
Generate trial information for same trials across subjects
input: the number of subjects, the shift number, the number of trials, the block
information file
output: the trial data structure array, the number of trials
*******************************/
Trial* GenRegularTrials(int nSubs, int nShift, int& nTrials, const char* file) {
  std::ifstream ifile(file);
  if (!ifile) {
    FATAL("no block file found!");
  }
  int nPerSubs = -1;
  ifile >> nPerSubs;
  Trial* trials = new Trial[nSubs * nPerSubs];  // 12 trials per subject
  int trial_labels[nPerSubs];  // = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
  int scs[nPerSubs];  // = {4, 24, 44, 64, 84, 104, 124, 144, 164, 184, 204,
                      // 224};
  int ecs[nPerSubs];  // = {15, 35, 55, 75, 95, 115, 135, 155, 175, 195, 215,
                      // 235};
  // int trial_labels[6] = {0, 1, 0, 1, 0, 1};
  // int scs[6] = {4, 24, 84, 104, 164, 184};
  // int ecs[6] = {15, 35, 95, 115, 175, 195};
  int i, j;
  for (i = 0; i < nPerSubs; i++) {
    ifile >> trial_labels[i] >> scs[i] >> ecs[i];
  }
  ifile.close();
  for (i = 0; i < nSubs; i++) {
    for (j = 0; j < nPerSubs; j++) {
      int index = i * nPerSubs + j;
      trials[index].sid = i;
      trials[index].label = trial_labels[j];
      trials[index].sc = scs[j] + nShift;
      trials[index].ec = ecs[j] + nShift;
      trials[index].tid_withinsubj = j;
      if (i < nSubs / 2) {
        trials[index].tid = j;
      } else {
        if (j % 2 == 0) {
          trials[index].tid = j + 1;  // bds:nPerSubs must be even?
        } else {
          trials[index].tid = j - 1;
        }
      }
    }
  }
  nTrials = nSubs * nPerSubs;
  return trials;
}

/******************************
Generate block information from a block directory, one-to-one mapping to the
data file.
The block files are in txt extension
input: the number of subjects, the shift number, the number of trials, the block
information directory
output: the trial data structure array, the number of trials
*******************************/
Trial* GenBlocksFromDir(int nSubs, int nShift, int& nTrials,
                        RawMatrix** r_matrices, const char* dir) {
  DIR* pDir;
  // cout << dir << endl;
  if ((pDir = opendir(dir)) == NULL) {
    FATAL("invalid block information directory");
  }
  closedir(pDir);
  std::string dirStr = std::string(dir);
  if (dirStr[dirStr.length() - 1] != '/') {
    dirStr += '/';
  }
  int i, j;
  nTrials = 0;
  Trial* trials = new Trial[nSubs * MAXTRIALPERSUBJ];
  int index = 0;
  for (i = 0; i < nSubs; i++) {
    std::string blockFileStr = r_matrices[i]->sname + ".txt";
    blockFileStr = dirStr + blockFileStr;
    std::ifstream ifile(blockFileStr.c_str());
    if (!ifile) {
      FATAL("no block file found!");
    }
    int nPerSubs = -1;
    ifile >> nPerSubs;
    int trial_labels[nPerSubs];
    int scs[nPerSubs];
    int ecs[nPerSubs];
    for (j = 0; j < nPerSubs; j++) {
      ifile >> trial_labels[j] >> scs[j] >> ecs[j];
      trials[index].sid = i;
      trials[index].label = trial_labels[j];
      trials[index].sc = scs[j] + nShift;
      trials[index].ec = ecs[j] + nShift;
      trials[index].tid_withinsubj = j;
      trials[index].tid = j;
      index++;
    }
    ifile.close();
    nTrials += nPerSubs;
  }
  return trials;
}

/***************************
read the location information, from a binary file, the file has row (#voxels)
and column (#TRs) in the beginning, followed by every voxel's 3D location
input: the file name
output: the location struct
****************************/
VoxelXYZ* ReadLocInfo(const char* file) {
  int row, col;
  FILE* fp = fopen(file, "rb");
  fread((void*)&row, sizeof(int), 1, fp);
  fread((void*)&col, sizeof(int), 1, fp);
  VoxelXYZ* pts = new VoxelXYZ[row];
  fread((void*)pts, sizeof(VoxelXYZ), row, fp);
  fclose(fp);
  // one time adjustment
  /*int i;
  for (i=0; i<row; i++)
  {
    pts[i].x -= 32;
    pts[i].y -= 32;
    pts[i].z -= 17;
  }*/
  return pts;
}

/***************************
generrate the initial location information following the nifti format, using the
nx ny nz information getting from the data nifti files
input: an arbitrary raw matrix, assuming that all subjects have the same format
output: the location struct
****************************/
VoxelXYZ* ReadLocInfoFromNii(RawMatrix* r_matrix) {
  int row = r_matrix->row;
  int nx = r_matrix->nx;
  int ny = r_matrix->ny;
  int nz = r_matrix->nz;
  VoxelXYZ* pts = new VoxelXYZ[row];
  for (int z = 0; z < nz; z++)
    for (int y = 0; y < ny; y++)
      for (int x = 0; x < nx; x++) {
        pts[z * ny * nx + y * nx + x].x = x;
        pts[z * ny * nx + y * nx + x].y = y;
        pts[z * ny * nx + y * nx + x].z = z;
      }
  return pts;
}

/*******************************
read the RT matrices information
input: the file name
output: the RT matrix array, the number of matrices (in the inputting parameter)
********************************/
double** ReadRTMatrices(const char* file, int& nSubs) {
  int nBlocksPerSub = -1;
  FILE* fp = fopen(file, "rb");
  fread((void*)&nSubs, sizeof(int), 1, fp);
  fread((void*)&nBlocksPerSub, sizeof(int), 1, fp);
  double** rtMatrices = new double* [nSubs];
  int i;
  for (i = 0; i < nSubs; i++) {
    double* rtMatrix = new double[nBlocksPerSub * nBlocksPerSub];
    fread((void*)rtMatrix, sizeof(double), nBlocksPerSub * nBlocksPerSub, fp);
    rtMatrices[i] = rtMatrix;
  }
  return rtMatrices;
}

/*******************************
write data to a compressed nifti file
input: the nifti data file name (with or without extension), the sample nifti
file to refer to, the data, the data type(defined in nifti1.h)
output: write the data to the file
********************************/
void WriteNiiGzData(const char* outputFile, const char* refFile, void* data,
                    int dataType) {
  if (refFile==NULL) {
    std::cout<<"No NIfTI file to refer, do not write out NIfTI file"<<std::endl;
    return;
  }
  nifti_image* nim =
      nifti_image_read(refFile, 1);  // 1 means reading the data as well
  if (nim == NULL) {
    FATAL("sample file not found: " << refFile);
  }
  assert(nim);  // quiet static analyzer
  nifti_image* nim2 = nifti_copy_nim_info(nim);
  char* newFileName = nifti_makeimgname(
      (char*)outputFile, nim->nifti_type, 0,
      1);  // 3rd argument: 0 means overwrite the existing file, 1 means
           // returning error if the file exists; 4th argument: 0 means not
           // compressed, 1 means compressed
  delete nim2->fname;  // delete the old filename to avoid memory leak
  nim2->fname = newFileName;
  nim2->datatype = dataType;
  nim2->nbyper = getSizeByDataType(dataType);
  nim2->data = data;
  nifti_image_write(nim2);
  nifti_image_free(nim);
  nifti_image_free(nim2);
  return;
}

/*******************************
write 4D data to a compressed nifti file
input: the nifti data file name (with or without extension), the sample nifti
file to refer to, the data, the data type(defined in nifti1.h), the time (4th)
dimension
output: write the data to the file
********************************/
void Write4DNiiGzData(const char* outputFile, const char* refFile, void* data,
                      int dataType, int nt) {
  if (refFile==NULL) {
    std::cout<<"No NIfTI file to refer, do not write out 4D NIfTI file"<<std::endl;
    return;
  }
  nifti_image* nim =
      nifti_image_read(refFile, 1);  // 1 means reading the data as well
  if (nim == NULL) {
    FATAL("sample file not found: " << refFile);
  }
  assert(nim);  // quiet static analyzer
  nifti_image* nim2 = nifti_copy_nim_info(nim);
  char* newFileName = nifti_makeimgname(
      (char*)outputFile, nim->nifti_type, 0,
      1);  // 3rd argument: 0 means overwrite the existing file, 1 means
           // returning error if the file exists; 4th argument: 0 means not
           // compressed, 1 means compressed
  delete nim2->fname;  // delete the old filename to avoid memory leak
  nim2->fname = newFileName;
  nim2->datatype = dataType;
  nim2->nbyper = getSizeByDataType(dataType);
  nim2->nt = nt;
  nim2->dim[0] = 4;
  nim2->dim[4] = nt;
  nim2->nvox = nim2->nx * nim2->ny * nim2->nz * nim2->nt;
  nim2->data = data;
  nifti_image_write(nim2);
  nifti_image_free(nim);
  nifti_image_free(nim2);
  return;
}

/*******************************
generate data to be written to a nifti file based on a mask file, the input data
is specifically from VoxelScore struct
input: the mask nifti file name, the data, the length of the data array, the
data type(defined in nifti1.h, DT_SIGNED_INT indicates to generate voxel id
file, DT_FLOAT indicates to generate voxel score file)
output: the data that is ready to be written to a nifti file
********************************/
void* GenerateNiiDataFromMask(const char* maskFile, VoxelScore* scores,
                              int length, int dataType) {
  nifti_image* nim =
      nifti_image_read(maskFile, 1);  // 1 means reading the data as well
  if (nim == NULL) {
    FATAL("mask file not found: " << maskFile << " in GenerateNiiDataFromMask");
  }
  int* data_int = NULL;
  short* data_short = NULL;
  unsigned char* data_uchar = NULL;
  float* data_float = NULL;
  double* data_double = NULL;
  switch (nim->datatype) {
    case DT_SIGNED_INT:
      data_int = (int*)nim->data;
      break;
    case DT_SIGNED_SHORT:
      data_short = (short*)nim->data;
      break;
    case DT_UNSIGNED_CHAR:
      data_uchar = (unsigned char*)nim->data;
      break;
    case DT_FLOAT:
      data_float = (float*)nim->data;
      break;
    case DT_FLOAT64:
      data_double = (double*)nim->data;
      break;
    default:
      FATAL("wrong data type of mask file!"
            << " in GenerateNiiDataFromMask");
  }
  int nVoxels = nim->nx * nim->ny * nim->nz;
  void* returnData = NULL;
  int* returnData_int = NULL;
  float* returnData_float = NULL;
  if (dataType == DT_SIGNED_INT) {
    returnData_int = new int[nVoxels];
    memset(returnData_int, 0, nVoxels * sizeof(int));
    returnData = (void*)returnData_int;
  } else if (dataType == DT_FLOAT) {
    returnData_float = new float[nVoxels];
    memset(returnData_float, 0, nVoxels * sizeof(float));
    returnData = (void*)returnData_float;
  } else {
    FATAL("wrong data type of return data!"
          << " in GenerateNiiDataFromMask");
  }
  // generate data from scores
  void* maskedData = new int[length];  // int and float have the same data size
  if (dataType == DT_SIGNED_INT) {
#pragma omp parallel for
    for (int i = 0; i < length; i++) {
      ((int*)maskedData)[scores[i].vid] = i + 1;  // voxel list is 0-based
    }
  } else  // must be DT_FLOAT here
  {
#pragma omp parallel for
    for (int i = 0; i < length; i++) {
      ((float*)maskedData)[scores[i].vid] =
          scores[i].score;  // voxel list is 0-based
    }
  }
  // generate data done

  int count = 0;
  // because of the count variable, no OMP here
  for (int i = 0; i < nVoxels; i++) {
    if (data_int != NULL && data_int[i]) {
      if (count == length) {
        FATAL("number of scores is larger than number of masked voxels "
              << length << "!"
              << " in GenerateNiiDataFromMask");
      }
      if (dataType == DT_SIGNED_INT) {
        returnData_int[i] = ((int*)maskedData)[count];
      } else  // must be DT_FLOAT here
      {
        returnData_float[i] = ((float*)maskedData)[count];
      }
      count++;
    }
    if (data_short != NULL && data_short[i]) {
      if (count == length) {
        FATAL("number of scores is larger than number of masked voxels "
              << length << "!"
              << " in GenerateNiiDataFromMask");
      }
      if (dataType == DT_SIGNED_INT) {
        returnData_int[i] = ((int*)maskedData)[count];
      } else  // must be DT_FLOAT here
      {
        returnData_float[i] = ((float*)maskedData)[count];
      }
      count++;
    }
    if (data_uchar != NULL && data_uchar[i]) {
      if (count == length) {
        FATAL("number of scores is larger than number of masked voxels "
              << length << "!"
              << " in GenerateNiiDataFromMask");
      }
      if (dataType == DT_SIGNED_INT) {
        returnData_int[i] = ((int*)maskedData)[count];
      } else  // must be DT_FLOAT here
      {
        returnData_float[i] = ((float*)maskedData)[count];
      }
      count++;
    }
    if (data_float != NULL && data_float[i] >= 1 - TINYNUM) {
      if (count == length) {
        FATAL("number of scores is larger than number of masked voxels "
              << length << "!"
              << " in GenerateNiiDataFromMask");
      }
      if (dataType == DT_SIGNED_INT) {
        returnData_int[i] = ((int*)maskedData)[count];
      } else  // must be DT_FLOAT here
      {
        returnData_float[i] = ((float*)maskedData)[count];
      }
      count++;
    }
    if (data_double != NULL && data_double[i] >= 1 - TINYNUM) {
      if (count == length) {
        FATAL("number of scores is larger than number of masked voxels "
              << length << "!"
              << " in GenerateNiiDataFromMask");
      }
      if (dataType == DT_SIGNED_INT) {
        returnData_int[i] = ((int*)maskedData)[count];
      } else  // must be DT_FLOAT here
      {
        assert(returnData_float);
        returnData_float[i] = ((float*)maskedData)[count];
      }
      count++;
    }
  }
  nifti_image_free(nim);
  return returnData;
}

/*************************************
take a nifti datatype as input
return its size in bytes to feed nbyter of nifti_image
**************************************/
inline int getSizeByDataType(int datatype) {
  int nbyter = -1;
  switch (datatype) {
    case 2:        // DT_UNSIGNED_CHAR, DT_UINT8
    case 256:      // DT_INT8
      nbyter = 1;  // 1 byte
      break;
    case 4:        // DT_SIGNED_SHORT, DT_INT16
    case 512:      // DT_UINT16
      nbyter = 2;  // 2 bytes
      break;
    case 8:        // DT_SIGNED_INT, DT_INT32
    case 768:      // DT_UINT32
    case 16:       //  DT_FLOAT, DT_FLOAT32
      nbyter = 4;  // 4 bytes
      break;
    case 64:  // DT_DOUBLE, DT_FLOAT64
      nbyter = 8;
      break;
    default:
      FATAL("wrong data type of nifti file in getSizeByDataType!");
  }
  return nbyter;
}

/* get filename extension starting at first '.' */
const char* GetFilenameExtension(const char* filename) {
  const char* dot = strrchr(filename, '.');
  if (!dot || dot == filename) return "";
  return dot + 1;
}

/* read all bytes from a text file into a buffer, which it allocates
    caller should delete buffer when done
 */
char* getTextBytes(const char* fcma_file, long* num_bytes) {
  FILE* f = fopen(fcma_file, "rb");
  fseek(f, 0, SEEK_END);

  long len = ftell(f);
  fseek(f, 0, SEEK_SET);

  *num_bytes = len + 1;
  char* data = new char[*num_bytes];
  fread(data, 1, len, f);
  fclose(f);

  return data;
}

/*******************************
 Read key:value or key=value" pairs from a file, skipping '#' comments.
   - (Caller should allocate "char* keys_and_values[length]"  or "new
 char*[length]")
 input: the filename and array of pointers to hold the values (preceded by max
 array length).
        Keys will be the even indices (0,2,4..) Values the odd indices
 (1,3,5...)
 output: the number of elements (pairs*2-- the keys plus the values)
 ********************************/
int ReadConfigFile(const char* fcma_file, const int& length,
                   char** keys_and_values) {

  const char* lsep = "\n\r";
  const char* sep = ":=";
  char* line, *word, *brkt, *brkw;
  long num_bytes;

  char* text = getTextBytes(fcma_file, &num_bytes);

  int count = 0;
  for (line = strtok_r(text, lsep, &brkt); line;
       line = strtok_r(NULL, lsep, &brkt)) {

    if (line[0] == '#') continue;

    for (word = strtok_r(line, sep, &brkw); word;
         word = strtok_r(NULL, sep, &brkw)) {

      size_t wlen = strlen(word);
      keys_and_values[count] = new char[wlen + 1];
      // printf("%d: got label %s (len %lu)\n",count,word,strlen(word));
      assert(count < length);
      memmove(keys_and_values[count], word, wlen + 1);
      count++;
    }
  }
  delete[] text;
  return count;
}

/* get filename prefix, without extension */
/* Caller must free returned string */
static char* GetFilenamePrefixAndFreeResult(const char* mystr) {
    char *retstr;
    char *lastdot;
    if (mystr == NULL)
        return NULL;
    if ((retstr = (char*)malloc (strlen (mystr) + 1)) == NULL)
        return NULL;
    strcpy (retstr, mystr);
    lastdot = strrchr (retstr, '.');
    if (lastdot != NULL)
        *lastdot = '\0';
    return retstr;
}

/*******************************
 write 4D data to an HDF5 file
 input: the matrix dimensions and float matrix, and outputfile name
 output: none, writes the data to HDF5 file
 ********************************/
void WriteCorrMatToHDF5(int row1, int row2, float* corrMat, const char* outputfile) {
    hid_t       file_id;
    hsize_t     dims[2];

    dims[0] = row1;
    dims[1] = row2;

    char* prefix = GetFilenamePrefixAndFreeResult(outputfile);
    char h5out[MAXFILENAMELENGTH];
    sprintf(h5out,"%s.h5", prefix);
    free(prefix);

    /* create a HDF5 file */
    file_id = H5Fcreate (h5out, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /* create and write an integer type dataset named dset */
    H5LTmake_dataset(file_id, "/dset", 2, dims, H5T_NATIVE_FLOAT, corrMat);

    /* close file */
    H5Fclose (file_id);
}

#endif


text:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Dnfeitosa.Enums")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Diego Feitosa")]
[assembly: AssemblyProduct("Dnfeitosa.Enums")]
[assembly: AssemblyCopyright("Copyright ©  2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d076c92b-cd87-4da1-961c-2c76279da104")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.1.0")]
[assembly: AssemblyFileVersion("1.7.1.0")]

// Framework-specif projects
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.ASP.NET.MVC")]
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.ASP.NET.MVC2")]
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.NHibernate2")]

// Test projects
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Tests")]
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.ASP.NET.MVC.Test")]
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.ASP.NET.MVC2.Test")]
[assembly: InternalsVisibleTo("Dnfeitosa.Enums.Frameworks.NHibernate2.Test")]

redpajama_set_name: RedPajamaGithub


text:
package Generics.element;

/**
 * Created by Defias on 2020/07.
 * Description:
 */
public class StringAddress {
    private String s;
    public StringAddress(String s) { this.s = s; }
    public String toString() {
        return super.toString() + " " + s;
    }
}


text:
"use strict";
var React = require("react");
var ReactDOM = require("react-dom");
var helloworld_1 = require("./helloworld");
ReactDOM.render(React.createElement(helloworld_1.HelloWorld, null), document.getElementById("content"));
//# sourceMappingURL=app.js.map


text:
var should = require('chai').should();
var Promise = require('bluebird');

describe('post', function(){
  var Hexo = require('../../../lib/hexo');
  var hexo = new Hexo(__dirname, {silent: true});
  var Post = hexo.model('Post');
  var generator = Promise.method(require('../../../lib/plugins/generator/post').bind(hexo));

  hexo.config.permalink = ':title/';

  before(function(){
    return hexo.init();
  });

  it('default layout', function(){
    return Post.insert({
      source: 'foo',
      slug: 'bar'
    }).then(function(post){
      return generator(hexo.locals).then(function(data){
        data.should.eql([
          {
            path: 'bar/',
            layout: ['post', 'page', 'index'],
            data: post
          }
        ]);
      }).then(function(){
        return post.remove();
      });
    });
  });

  it('custom layout', function(){
    return Post.insert({
      source: 'foo',
      slug: 'bar',
      layout: 'photo'
    }).then(function(post){
      return generator(hexo.locals).then(function(data){
        data[0].layout.should.eql(['photo', 'post', 'page', 'index']);
      }).then(function(){
        return post.remove();
      });
    });
  });

  it('layout disabled', function(){
    return Post.insert({
      source: 'foo',
      slug: 'bar',
      layout: false
    }).then(function(post){
      return generator(hexo.locals).then(function(data){
        should.not.exist(data[0].layout);
      }).then(function(){
        return post.remove();
      });
    });
  });

  it('prev/next post', function(){
    return Post.insert([
      {source: 'foo', slug: 'foo', date: 1e8},
      {source: 'bar', slug: 'bar', date: 1e8 + 1},
      {source: 'baz', slug: 'baz', date: 1e8 - 1}
    ]).then(function(posts){
      return generator(hexo.locals).then(function(data){
        should.not.exist(data[0].data.prev);
        data[0].data.next._id.should.eql(posts[0]._id);

        data[1].data.prev._id.should.eql(posts[1]._id);
        data[1].data.next._id.should.eql(posts[2]._id);

        data[2].data.prev._id.should.eql(posts[0]._id);
        should.not.exist(data[2].data.next);
      }).thenReturn(posts);
    }).map(function(post){
      return post.remove();
    });
  });
});


text:
require 'fileutils'
require 'open-uri'
require 'openssl'
require 'time'

Signal.trap('INT') {
   $stderr.puts "\n\n  ! User aborted\n"
   exit 1
}

module AppleOSS
  TOP_DIR = 'appleoss' # files will be downloaded here
  ERASE = "\b" * 12

  OSX_VERSIONS_ENDPOINT = 'https://opensource.apple.com/'
  OSX_VERSIONS_REGEX = /[^0-9<]*([0-9][0-9.]*)/m
  OSX_VERSIONS_START_LINE = /product-name">OS X</
  OSX_VERSIONS_END_LINE = /product release-list/

  TARBALLS_ENDPOINT = 'https://opensource.apple.com/tarballs/'
  TARBALLS_START_LINE = /
/
  TARBALLS_END_LINE = /<\/table>/
  DIR_REGEX = /([^\/]*)\/<\/a>/
  PACKAGE_TARBALL_REGEX = /.*<\/a><\/td>/

  def self.fetch_uncached(url)
    $stderr.puts "Fetching #{ENDPOINT}"
    open(url).read
  end

  def self.fetch(url)
    (@fetched ||= {})[url] ||= self.fetch_uncached
  end

  def self.fetch_between(url, start_regex, stop_regex)
    started, stopped = false, false
    self.fetch(url).split("\n").map { |line|
      next if stopped
      if started
        if line =~ stop_regex
          stopped = true
          next
        end
        line
      else
        started = true if line =~ start_regex
      end
    }.compact.join("\n")
  end

  def self.check_file(fn)
    Zlib::GzipReader.open(fn).read rescue false
  end

  def self.relative_release_urls
    Hash[self.fetch_between(OSX_VERSIONS_ENDPOINT, OSX_VERSIONS_START_LINE, OSX_VERSIONS_END_LINE).scan(RELEASE_REGEX).map { |x, y| [y, x] }]
  end

  def self.absolute_release_urls
    Hash[self.relative_release_urls.map { |version, relative_url|
      [version, URI.join(OSX_VERSIONS_ENDPOINT, relative_url).to_s ]
    }]
  end

  def self.osx_versions
    self.relative_release_urls.keys
  end

  def self.release_index_for_version(version)
    self.fetch(self.absolute_release_urls[version])
  end

  def self.relative_tarball_urls_for_version(version)
    (@relative_tarball_urls_for_version ||= {})[version] ||= self.release_index_for_version(version).scan(TARBALL_REGEX).flatten
  end

  def self.tarball_filenames_for_version(version)
    (@tarball_filenames_for_version ||= {})[version] ||= self.relative_tarball_urls_for_version(version).map { |uri| File.basename(uri) }
  end

  def self.absolute_tarball_urls_for_version(version)
    (@absolute_tarball_urls_for_version ||= {})[version] ||= self.relative_tarball_urls_for_version(version).map { |relative_url|
      URI.join(ENDPOINT, relative_url).to_s
    }
  end

  def self.download_tarball(uri, fn)
    size = nil
    needs_erase = false
    content_length_proc = proc { |content_length|
      size = content_length
      $stderr.puts "  | Downloading #{size} byte(s) from #{uri}"
    }
    progress_proc = proc { |bytes_read_so_far|
      $stderr.print ERASE + "    %.2f %" % percent = 100.0 * bytes_read_so_far / size
      needs_erase = true
    }
    options = {
         content_length_proc: content_length_proc,
               progress_proc: progress_proc
    }
    options['If-Modified-Since'] = File.stat(fn).mtime.httpdate if File.exists? fn
    tries = 3
    begin
      open(uri, options) { |r| File.open(fn, 'wb') { |w| w.write(r.read) } }
      raise OpenURI::HTTPError.new "Bad download" unless self.check_file fn
    rescue OpenURI::HTTPError => e
      if needs_erase
        $stderr.print ERASE
        needs_erase = false
      end
      if e.io.status[0].to_i == 304 && self.check_file(fn)
        $stderr.puts "  | Already downloaded #{uri}"
        return
      end
      options.delete 'If-Modified-Since'
      $stderr.puts "  | Retrying #{uri}"
      retry if (tries -= 1) > 0
      raise
    end
    if needs_erase
      $stderr.print ERASE
      needs_erase = false
    end
  end

# ---- package downloader

  def self.relative_package_dir_urls
    Hash[self.fetch(TARBALLS_ENDPOINT).scan(DIR_REGEX).map { |dir, package| [package, dir] }]
  end

  def self.absolute_package_dir_urls
    Hash[self.relative_package_dir_urls.map { |package, relative_url|
      [package, URI.join(ENDPOINT, relative_url).to_s]
    }]
  end

  def self.package_index_for_package(package)
    (@package_indicies ||= {})[package] ||= self.package_index_for_package_uncached(package)
  end

  def self.relative_tarball_urls_for_package(package)
    (@relative_tarball_urls_for_package ||= {})[package] ||= self.package_index_for_package(package).scan(PACKAGE_TARBALL_REGEX).flatten
  end

 def self.tarball_filenames_for_package(package)
    (@tarball_filenames_for_version ||= {})[package] ||= self.relative_tarball_urls_for_package(package).map { |uri| File.basename(uri) }
  end

  def self.absolute_tarball_urls_for_package(package)
    (@absolute_tarball_urls_for_package ||= {})[package] ||= self.relative_tarball_urls_for_package(package).map { |relative_url|
      URI.join(TARBALLS_ENDPOINT, self.relative_package_dir_urls[package], relative_url).to_s
    }
  end

  def self.download_package(package)
    $stderr.puts "Downloading source tarballs for package #{package}"

    tarball_urls = self.absolute_tarball_urls_for_package(package)
    filenames = self.tarball_filenames_for_package(package)
    FileUtils.mkdir_p(TOP_DIR)
    Dir.chdir(TOP_DIR) do
      FileUtils.mkdir_p(package)
      Dir.chdir(package) do
        tarball_urls.zip(filenames).each { |uri, fn| self.download_tarball(uri, fn) }
      end
    end
  end

  def self.download_packages_all
    self.packages.each { |package| self.download_package(package) }
  end

# ---- osx downloader

  def self.download_osx_version(version)
    $stderr.puts "Downloading source tarballs for osx #{version}"

    tarball_urls = self.absolute_tarball_urls_for_version(version)
    filenames = self.tarball_filenames_for_version(version)
    FileUtils.mkdir_p(TOP_DIR)
    Dir.chdir(TOP_DIR) do
      FileUtils.mkdir_p(version)
      Dir.chdir(version) do
        tarball_urls.zip(filenames).each { |uri, fn| self.download_tarball(uri, fn) }
      end
    end
  end

  def self.download_osx_all
    self.osx_versions.each { |osx_version| self.download_osx_version(osx_version) }
  end
end

if ARGV.empty?
  AppleOSS.download_packages_all
  AppleOSS.download_osx_all
else
  if ARGV.delete('-l') || ARGV.delete('--list-osx-versions')
    puts AppleOSS.osx_versions
    exit
  end
  if ARGV.delete('-p') || ARGV.delete('--list-packages')
    puts AppleOSS.packages
    exit
  end
  # check input
  ARGV.map { |arg|
    unless AppleOSS.packages.include?(arg) || AppleOSS.versions.include?(arg)
      fail "Unknown package or osx version #{package}"
    end
  }
  ARGV.map { |arg|
    if AppleOSS.version.include?(arg)
      AppleOSS.download_osx_version(arg)
    else
      AppleOSS.download_package(arg)
    end
  }
end


text:
var express = require('express');
var bodyParser = require('body-parser');

var server = module.exports = express();

server.set('views', __dirname + '/views'); 	// Views file folder
server.engine('html', require('ejs').renderFile);
server.set('view engine', 'ejs');

server.use(express.static(__dirname + '/static')); 			// Static folder

// Routes
server.get('/', function (req, res) {
	res.send('Home!');
});

server.get('/text', function (req, res) {
	res.send('text page');
});

server.get('/redirect', function (req, res) {
	res.redirect('/');
});

server.get('/json', function (req, res) {
	var results = {"Name":"Name"};
	res.json(results);
});

server.get('/index.html', function (req, res) {
	res.render('pages/index');
});


text:
from tokenizer import split_tokens, extract_tokens

class ObjectClass:

    def __init__(self, s):
        token_defaults = {
                'NAME':(()),
                'DESC': '',
                'OBSOLETE': 0,
                'SUP':(()),
                'STRUCTURAL':None,
                'AUXILIARY':None,
                'ABSTRACT':None,
                'MUST':(()),
                'MAY':(),
                'X-ORIGIN': '',
                'X-RDBM-IGNORE':('',),
              }

        l = split_tokens(s)
        self.oid = l[1]
        self.tokens = extract_tokens(l, token_defaults)

        self.kind = 0

        if self.tokens['ABSTRACT']!=None:
          self.kind = 1
        elif self.tokens['AUXILIARY']!=None:
          self.kind = 2
        if self.kind==0 and not self.tokens['SUP'] and self.oid!='2.5.6.0':
          self.tokens['SUP'] = ('top',)

    def key_attr(self, key, value, quoted=0):
        if type(value) == type(()):
            value = value[0]
        if value:
          if quoted:
            return " %s '%s'" % (key, value.replace("'","\\'"))
          else:
            return " %s %s" % (key, value)
        else:
          return ""

    def key_list(self, key, values, sep=' ', quoted=0):
        if not values:
          return ''
        if quoted:
          quoted_values = [ "'%s'" % value.replace("'","\\'") for value in values ]
        else:
          quoted_values = values
        if len(values)==1:
          return ' %s %s' % (key,quoted_values[0])
        else:
          return ' %s ( %s )' % (key,sep.join(quoted_values))

    def getstr(self):
        result = [str(self.oid)]
        result.append(self.key_list('NAME', self.tokens['NAME'], quoted=1))
        result.append(self.key_attr('DESC', self.tokens['DESC'], quoted=1))
        result.append(self.key_list('SUP', self.tokens['SUP'], sep=' $ '))
        result.append({0:'',1:' OBSOLETE'}[self.tokens['OBSOLETE']])
        result.append({0:' STRUCTURAL',1:' ABSTRACT',2:' AUXILIARY'}[self.kind])
        result.append(self.key_list('MUST', self.tokens['MUST'], sep=' $ '))
        result.append(self.key_list('MAY', self.tokens['MAY'], sep=' $ '))
        result.append(self.key_attr('X-ORIGIN', self.tokens['X-ORIGIN'], quoted=1))

        return u'( %s )' % ''.join(result)

AttributeUsage = {
  'userApplication':0,
  'userApplications':0,
  'directoryOperation':1,
  'distributedOperation':2,
  'dSAOperation':3,
}

class AttributeType:

    def __init__(self, s=''):
        token_defaults = {
            'NAME':(()),
            'DESC':(None,),
            'OBSOLETE':None,
            'SUP':(()),
            'EQUALITY':(None,),
            'ORDERING':(None,),
            'SUBSTR':(None,),
            'SYNTAX':(None,),
            'SINGLE-VALUE':None,
            'COLLECTIVE':None,
            'NO-USER-MODIFICATION':None,
            'USAGE':('userApplications',),
            'X-ORIGIN':(None,),
            'X-ORDERED':(None,),
            'X-RDBM-ADD': (None,),
          }

        if s:

            l = split_tokens(s)
            self.oid = l[1]
            self.tokens = extract_tokens(l, token_defaults)

            try:
              syntax = self.tokens['SYNTAX'][0]
            except IndexError:
              self.syntax = None
              self.syntax_len = None
            else:
              if syntax is None:
                self.syntax = None
                self.syntax_len = None
              else:
                try:
                  self.syntax,syntax_len = self.tokens['SYNTAX'][0].split("{")
                except ValueError:
                  self.syntax = self.tokens['SYNTAX'][0]
                  self.syntax_len = None
                  for i in l:
                    if i.startswith("{") and i.endswith("}"):
                      self.syntax_len=int(i[1:-1])
                else:
                  self.syntax_len = int(syntax_len[:-1])

    def key_attr(self, key, value, quoted=0):
        if type(value) == type(()):
            value = value[0]
        if value:
          if quoted:
            return " %s '%s'" % (key, value.replace("'","\\'"))
          else:
            return " %s %s" % (key, value)
        else:
          return ""

    def key_list(self, key, values, sep=' ', quoted=0):
        if not values:
          return ''
        if quoted:
          quoted_values = [ "'%s'" % value.replace("'","\\'") for value in values ]
        else:
          quoted_values = values
        if len(values)==1:
          return ' %s %s' % (key,quoted_values[0])
        else:
          return ' %s ( %s )' % (key,sep.join(quoted_values))

    def getstr(self):
        result = [str(self.oid)]
        result.append(self.key_list('NAME', self.tokens['NAME'], quoted=1))
        result.append(self.key_attr('DESC', self.tokens['DESC'], quoted=1))
        result.append(self.key_list('SUP', self.tokens['SUP'], sep=' $ '))
        result.append({0:'',1:' OBSOLETE'}[self.tokens['OBSOLETE']!=None])
        result.append(self.key_attr('EQUALITY', self.tokens['EQUALITY']))
        result.append(self.key_attr('ORDERING', self.tokens['ORDERING']))
        result.append(self.key_attr('SUBSTR', self.tokens['SUBSTR']))
        result.append(self.key_attr('SYNTAX', self.tokens['SYNTAX']))
        if self.syntax_len!=None:
          result.append(('{%d}' % (self.syntax_len))*(self.syntax_len>0))
        result.append({0:'',1:' SINGLE-VALUE'}[self.tokens['SINGLE-VALUE']!=None])
        result.append({0:'',1:' COLLECTIVE'}[self.tokens['COLLECTIVE']!=None])
        result.append({0:'',1:' NO-USER-MODIFICATION'}[self.tokens['NO-USER-MODIFICATION']!=None])
        result.append(
          {
            0:"",
            1:" USAGE directoryOperation",
            2:" USAGE distributedOperation",
            3:" USAGE dSAOperation",
          }[AttributeUsage.get(self.tokens['USAGE'][0],0)]
        )
        result.append(self.key_attr('X-ORIGIN', self.tokens['X-ORIGIN'], quoted=1))
        result.append(self.key_attr('X-ORDERED', self.tokens['X-ORDERED'], quoted=1))
        return '( %s )' % ''.join(result)


text:
unless ["production", "staging"].include? ENV['RACK_ENV']
  # dotenv allows us to store configuration variables in a .env file
  # This makes it easy to keep our secret configuration variables
  # outside of our public repo. HOORAY!
  require 'dotenv'
  Dotenv.load(".env.#{ENV['RACK_ENV']}")
  # https://github.com/bkeepers/dotenv#sinatra-or-plain-ol-ruby
end


text:
<?php
class childrenDailyGoalsController extends BaseController{

	function get(){

	}
	function save(){

	}
	function update(){

	}
	function delete(){

	}
}
?>


text:
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _createSvgIcon = _interopRequireDefault(require("./utils/createSvgIcon"));

var _jsxRuntime = require("react/jsx-runtime");

var _default = (0, _createSvgIcon.default)( /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
  d: "M0 15h2V9H0v6zm3 2h2V7H3v10zm19-8v6h2V9h-2zm-3 8h2V7h-2v10zM18 3H6v18h12V3zm-2 16H8V5h8v14z"
}), 'VibrationSharp');

exports.default = _default;


text:
you are viewing a single comment's thread.

view the rest of the comments →


text:
'''
  Command to send dynamic filesystem information to Zagg
'''
# vim: expandtab:tabstop=4:shiftwidth=4
#
#   Copyright 2015 Red Hat Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#This is not a module, but pylint thinks it is.  This is a command.
#pylint: disable=invalid-name,import-error

import argparse
from openshift_tools.monitoring.metric_sender import MetricSender
from openshift_tools.monitoring import pminfo

def parse_args():
    """ parse the args from the cli """

    parser = argparse.ArgumentParser(description='Disk metric sender')
    parser.add_argument('-v', '--verbose', action='store_true', default=None, help='Verbose?')
    parser.add_argument('--debug', action='store_true', default=None, help='Debug?')

    return parser.parse_args()

def filter_out_docker_filesystems(metric_dict, filesystem_filter):
    """ Simple filter to elimate unnecessary characters in the key name """
    filtered_dict = {k.replace(filesystem_filter, ''):v
                     for (k, v) in metric_dict.iteritems()
                     if 'docker' not in k
                    }
    return filtered_dict

def main():
    """  Main function to run the check """

    args = parse_args()
    metric_sender = MetricSender(verbose=args.verbose, debug=args.debug)

    filesys_full_metric = ['filesys.full']
    filesys_inode_derived_metrics = {'filesys.inodes.pused' :
                                     'filesys.usedfiles / (filesys.usedfiles + filesys.freefiles) * 100'
                                    }

    discovery_key_fs = 'disc.filesys'
    item_prototype_macro_fs = '#OSO_FILESYS'
    item_prototype_key_full = 'disc.filesys.full'
    item_prototype_key_inode = 'disc.filesys.inodes.pused'

    # Get the disk space
    filesys_full_metrics = pminfo.get_metrics(filesys_full_metric)

    filtered_filesys_metrics = filter_out_docker_filesystems(filesys_full_metrics, 'filesys.full.')

    metric_sender.add_dynamic_metric(discovery_key_fs, item_prototype_macro_fs, filtered_filesys_metrics.keys())
    for filesys_name, filesys_full in filtered_filesys_metrics.iteritems():
        metric_sender.add_metric({'%s[%s]' % (item_prototype_key_full, filesys_name): filesys_full})

    # Get filesytem inode metrics
    filesys_inode_metrics = pminfo.get_metrics(derived_metrics=filesys_inode_derived_metrics)

    filtered_filesys_inode_metrics = filter_out_docker_filesystems(filesys_inode_metrics, 'filesys.inodes.pused.')
    for filesys_name, filesys_inodes in filtered_filesys_inode_metrics.iteritems():
        metric_sender.add_metric({'%s[%s]' % (item_prototype_key_inode, filesys_name): filesys_inodes})

    metric_sender.send_metrics()

if __name__ == '__main__':
    main()


text:
using System.Web;
using System.Web.Mvc;

namespace Epi.Cloud.DataConsistencyServices
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}


text:
#include "cpp_tokens.hpp"                  // global configuration

///////////////////////////////////////////////////////////////////////////////
//  Include Wave itself
#include <boost/wave.hpp>

///////////////////////////////////////////////////////////////////////////////
//  The following files contain the custom lexer type to use
#include "slex_token.hpp"
#include "slex_iterator.hpp"

///////////////////////////////////////////////////////////////////////////////
//  include lexer specifics, import lexer names
#if !defined(BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION)
#include "slex/cpp_slex_lexer.hpp"
#endif // !defined(BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION)

#include <iostream>

///////////////////////////////////////////////////////////////////////////////
//  import required names
using namespace boost::spirit::classic;

using std::string;
using std::getline;
using std::ifstream;
using std::cout;
using std::cerr;
using std::endl;
using std::ostream;

///////////////////////////////////////////////////////////////////////////////
//  main program
int
main(int argc, char *argv[])
{
    if (2 != argc) {
        cout << "Usage: cpp_tokens input_file" << endl;
        return 1;
    }

// read the file to analyse into a std::string
    ifstream infile(argv[1]);
    string teststr;
    if (infile.is_open()) {
        infile.unsetf(std::ios::skipws);
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
        // this is known to be very slow for large files on some systems
        copy (std::istream_iterator<char>(infile),
              std::istream_iterator<char>(),
              std::inserter(teststr, teststr.end()));
#else
        teststr = std::string(std::istreambuf_iterator<char>(infile.rdbuf()),
                              std::istreambuf_iterator<char>());
#endif
    }
    else {
        teststr = argv[1];
    }

//  The following typedef does the trick. It defines the context type to use,
//  which depends on the lexer type (provided by the second template
//  parameter). Our lexer type 'slex_iterator<>' depends on a custom token type
//  'slex_token<>'. Our custom token type differs from the original one provided
//  by the Wave library only by defining an additional operator<<(), which is
//  used to dump the token information carried by a given token (see loop
//  below).
    typedef boost::wave::cpplexer::slex_token<> token_type;
    typedef boost::wave::cpplexer::slex::slex_iterator<token_type> lexer_type;
    typedef boost::wave::context<std::string::iterator, lexer_type>
        context_type;

// The C++ preprocessor iterator shouldn't be constructed directly. It is to be
// generated through a boost::wave::context<> object. This object is
// additionally to be used to initialize and define different parameters of
// the actual preprocessing.
// The preprocessing of the input stream is done on the fly behind the scenes
// during iteration over the context_type::iterator_type stream.
    context_type ctx (teststr.begin(), teststr.end(), argv[1]);

    ctx.set_language(boost::wave::support_cpp0x);
    ctx.set_language(boost::wave::enable_preserve_comments(ctx.get_language()));
    ctx.set_language(boost::wave::enable_prefer_pp_numbers(ctx.get_language()));
    ctx.set_language(boost::wave::enable_emit_contnewlines(ctx.get_language()));

    context_type::iterator_type first = ctx.begin();
    context_type::iterator_type last = ctx.end();
    context_type::token_type current_token;

    try {
    //  Traverse over the tokens generated from the input and dump the token
    //  contents.
        while (first != last) {
        // retrieve next token
            current_token = *first;

        // output token info
            cout << "matched " << current_token << endl;
            ++first;
        }
    }
    catch (boost::wave::cpp_exception const& e) {
    // some preprocessing error
        cerr
            << e.file_name() << "(" << e.line_no() << "): "
            << e.description() << endl;
        return 2;
    }
    catch (std::exception const& e) {
    // use last recognized token to retrieve the error position
        cerr
            << current_token.get_position().get_file()
            << "(" << current_token.get_position().get_line() << "): "
            << "unexpected exception: " << e.what()
            << endl;
        return 3;
    }
    catch (...) {
    // use last recognized token to retrieve the error position
        cerr
            << current_token.get_position().get_file()
            << "(" << current_token.get_position().get_line() << "): "
            << "unexpected exception." << endl;
        return 4;
    }
    return 0;
}


text:
import OpenCL
using OpenCL: cl

const sum_kernel_src = "
   __kernel void sum(__global const float *a,
                     __global const float *b,
                     __global float *c)
    {
      int gid = get_global_id(0);
      c[gid] = a[gid] + b[gid];
    }
"
a = rand(Float32, 50_000)
b = rand(Float32, 50_000)

device, ctx, queue = cl.create_compute_context()

# create opencl buffer objects
# copies to the device initiated when the kernel function is called
a_buff = cl.Buffer(Float32, ctx, (:r, :copy), hostbuf=a)
b_buff = cl.Buffer(Float32, ctx, (:r, :copy), hostbuf=b)
c_buff = cl.Buffer(Float32, ctx, :w, length(a))

# build the program and construct a kernel object
p = cl.Program(ctx, source=sum_kernel_src) |> cl.build!
sum_kernel = cl.Kernel(p, "sum")

# call the kernel object with global size set to the size our arrays
sum_kernel[queue, size(a)](a_buff, b_buff, c_buff)

# perform a blocking read of the result from the device
r = cl.read(queue, c_buff)

# check to see if our result is what we expect!
if isapprox(norm(r - (a+b)), zero(Float32))
    info("Success!")
else
    error("Norm should be 0.0f")
end


text:
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;

namespace Org.BouncyCastle.Bcpg.OpenPgp
{
	/// <remarks>
	/// Often a PGP key ring file is made up of a succession of master/sub-key key rings.
	/// If you want to read an entire public key file in one hit this is the class for you.
	/// </remarks>
    public class PgpPublicKeyRingBundle
    {
        private readonly IDictionary pubRings;
        private readonly IList order;

		private PgpPublicKeyRingBundle(
            IDictionary	pubRings,
            IList       order)
        {
            this.pubRings = pubRings;
            this.order = order;
        }

		public PgpPublicKeyRingBundle(
            byte[] encoding)
            : this(new MemoryStream(encoding, false))
        {
        }

		/// Build a PgpPublicKeyRingBundle from the passed in input stream.
		/// <param name="inputStream">Input stream containing data.</param>
		/// <exception cref="IOException">If a problem parsing the stream occurs.</exception>
		/// <exception cref="PgpException">If an object is encountered which isn't a PgpPublicKeyRing.</exception>
		public PgpPublicKeyRingBundle(
            Stream inputStream)
			: this(new PgpObjectFactory(inputStream).AllPgpObjects())
		{
        }

		public PgpPublicKeyRingBundle(
            IEnumerable e)
        {
			this.pubRings = Platform.CreateHashtable();
			this.order = Platform.CreateArrayList();

			foreach (object obj in e)
            {
				PgpPublicKeyRing pgpPub = obj as PgpPublicKeyRing;

				if (pgpPub == null)
				{
					throw new PgpException(obj.GetType().FullName + " found where PgpPublicKeyRing expected");
				}

				long key = pgpPub.GetPublicKey().KeyId;
                pubRings.Add(key, pgpPub);
				order.Add(key);
            }
        }

		[Obsolete("Use 'Count' property instead")]
		public int Size
		{
			get { return order.Count; }
		}

		/// Return the number of key rings in this collection.
        public int Count
        {
			get { return order.Count; }
        }

		/// Allow enumeration of the public key rings making up this collection.
        public IEnumerable GetKeyRings()
        {
			return new EnumerableProxy(pubRings.Values);
        }

		/// Allow enumeration of the key rings associated with the passed in userId.
		/// <param name="userId">The user ID to be matched.</param>
		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
		public IEnumerable GetKeyRings(
			string userId)
		{
			return GetKeyRings(userId, false, false);
		}

		/// Allow enumeration of the key rings associated with the passed in userId.
		/// <param name="userId">The user ID to be matched.</param>
		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
        public IEnumerable GetKeyRings(
            string	userId,
            bool	matchPartial)
        {
			return GetKeyRings(userId, matchPartial, false);
        }

		/// Allow enumeration of the key rings associated with the passed in userId.
		/// <param name="userId">The user ID to be matched.</param>
		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
		/// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param>
		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
		public IEnumerable GetKeyRings(
			string	userId,
			bool	matchPartial,
			bool	ignoreCase)
		{
			IList rings = Platform.CreateArrayList();

			if (ignoreCase)
			{
                userId = Platform.ToLowerInvariant(userId);
			}

			foreach (PgpPublicKeyRing pubRing in GetKeyRings())
			{
				foreach (string nextUserID in pubRing.GetPublicKey().GetUserIds())
				{
					string next = nextUserID;
					if (ignoreCase)
					{
                        next = Platform.ToLowerInvariant(next);
                    }

					if (matchPartial)
					{
						if (next.IndexOf(userId) > -1)
						{
							rings.Add(pubRing);
						}
					}
					else
					{
						if (next.Equals(userId))
						{
							rings.Add(pubRing);
						}
					}
				}
			}

			return new EnumerableProxy(rings);
		}

		/// Return the PGP public key associated with the given key id.
		/// <param name="keyId">The ID of the public key to return.</param>
        public PgpPublicKey GetPublicKey(
            long keyId)
        {
            foreach (PgpPublicKeyRing pubRing in GetKeyRings())
            {
                PgpPublicKey pub = pubRing.GetPublicKey(keyId);

				if (pub != null)
                {
                    return pub;
                }
            }

			return null;
        }

		/// Return the public key ring which contains the key referred to by keyId
		/// <param name="keyId">key ID to match against</param>
        public PgpPublicKeyRing GetPublicKeyRing(
            long keyId)
        {
            if (pubRings.Contains(keyId))
            {
                return (PgpPublicKeyRing)pubRings[keyId];
            }

			foreach (PgpPublicKeyRing pubRing in GetKeyRings())
            {
                PgpPublicKey pub = pubRing.GetPublicKey(keyId);

                if (pub != null)
                {
                    return pubRing;
                }
            }

			return null;
        }

		///
		/// Return true if a key matching the passed in key ID is present, false otherwise.
		///
		/// <param name="keyID">key ID to look for.</param>
		public bool Contains(
			long keyID)
		{
			return GetPublicKey(keyID) != null;
		}

		public byte[] GetEncoded()
        {
            MemoryStream bOut = new MemoryStream();

			Encode(bOut);

			return bOut.ToArray();
        }

		public void Encode(
            Stream outStr)
        {
			BcpgOutputStream bcpgOut = BcpgOutputStream.Wrap(outStr);

			foreach (long key in order)
            {
                PgpPublicKeyRing sec = (PgpPublicKeyRing) pubRings[key];

				sec.Encode(bcpgOut);
            }
        }

		///
		/// Return a new bundle containing the contents of the passed in bundle and
		/// the passed in public key ring.
		///
		/// <param name="bundle">The <c>PgpPublicKeyRingBundle</c> the key ring is to be added to.</param>
		/// <param name="publicKeyRing">The key ring to be added.</param>
		/// <returns>A new <c>PgpPublicKeyRingBundle</c> merging the current one with the passed in key ring.</returns>
		/// <exception cref="ArgumentException">If the keyId for the passed in key ring is already present.</exception>
        public static PgpPublicKeyRingBundle AddPublicKeyRing(
            PgpPublicKeyRingBundle  bundle,
            PgpPublicKeyRing        publicKeyRing)
        {
            long key = publicKeyRing.GetPublicKey().KeyId;

			if (bundle.pubRings.Contains(key))
            {
                throw new ArgumentException("Bundle already contains a key with a keyId for the passed in ring.");
            }

			IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings);
            IList newOrder = Platform.CreateArrayList(bundle.order);

			newPubRings[key] = publicKeyRing;

			newOrder.Add(key);

			return new PgpPublicKeyRingBundle(newPubRings, newOrder);
        }

		///
		/// Return a new bundle containing the contents of the passed in bundle with
		/// the passed in public key ring removed.
		///
		/// <param name="bundle">The <c>PgpPublicKeyRingBundle</c> the key ring is to be removed from.</param>
		/// <param name="publicKeyRing">The key ring to be removed.</param>
		/// <returns>A new <c>PgpPublicKeyRingBundle</c> not containing the passed in key ring.</returns>
		/// <exception cref="ArgumentException">If the keyId for the passed in key ring is not present.</exception>
        public static PgpPublicKeyRingBundle RemovePublicKeyRing(
            PgpPublicKeyRingBundle	bundle,
            PgpPublicKeyRing		publicKeyRing)
        {
            long key = publicKeyRing.GetPublicKey().KeyId;

			if (!bundle.pubRings.Contains(key))
            {
                throw new ArgumentException("Bundle does not contain a key with a keyId for the passed in ring.");
            }

			IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings);
            IList newOrder = Platform.CreateArrayList(bundle.order);

			newPubRings.Remove(key);
			newOrder.Remove(key);

			return new PgpPublicKeyRingBundle(newPubRings, newOrder);
        }
    }
}


text:
package org.springframework.social.box.api.impl;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;

import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.social.ResourceNotFoundException;
import org.springframework.social.UncategorizedApiException;

/**
 *
 * @author Ioannis Nikolaou
 */
public class BoxErrorHandler404sTest {

    BoxErrorHandler boxErrorHandler = new BoxErrorHandler();
    String error = "{\"type\":\"error\", \"status\":404, \"help_url\":\"helpUrl\", \"message\":\"message\", \"request_id\":\"requestId\", \"code\":";

    @Test
    public void handleForbidden() {
        ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new ByteArrayInputStream((error + "\"not_found\"}").getBytes()),HttpStatus.NOT_FOUND);
        try {
            boxErrorHandler.handleError(clientHttpResponse);
        } catch (Exception e) {
            assertTrue(e instanceof ResourceNotFoundException);
        }
    }

    @Test
    public void handlePreviewCannotBeGenerated() {
        ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new ByteArrayInputStream((error + "\"preview_cannot_be_generated\"}").getBytes()),HttpStatus.NOT_FOUND);
        try {
            boxErrorHandler.handleError(clientHttpResponse);
        } catch (Exception e) {
            assertTrue(e instanceof ResourceNotFoundException);
        }
    }

    @Test
    public void handleTrashed() {
        ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new ByteArrayInputStream((error + "\"trashed\"}").getBytes()),HttpStatus.NOT_FOUND);
        try {
            boxErrorHandler.handleError(clientHttpResponse);
        } catch (Exception e) {
            assertTrue(e instanceof ResourceNotFoundException);
        }
    }

    @Test
    public void handleNotTrashed() {
        ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new ByteArrayInputStream((error + "\"not_trashed\"}").getBytes()),HttpStatus.NOT_FOUND);
        try {
            boxErrorHandler.handleError(clientHttpResponse);
        } catch (Exception e) {
            assertTrue(e instanceof ResourceNotFoundException);
        }
    }

    @Test
    public void handleUnknownStatus() {
        ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new ByteArrayInputStream((error + "\"unknown_status\"}").getBytes()),HttpStatus.NOT_FOUND);
        try {
            boxErrorHandler.handleError(clientHttpResponse);
        } catch (Exception e) {
            assertTrue(e instanceof UncategorizedApiException);
        }
    }
}


text:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Nest
{
	/// <summary>
	/// A set of analyzers aimed at analyzing specific language text.
	/// </summary>
	public class LanguageAnalyzer : AnalyzerBase
	{
		public LanguageAnalyzer(Language language)
		{
			language.ThrowIfNull("language");
			var name = Enum.GetName(typeof (Language), language);
			if (name == null)
				language.ThrowIfNull("language");

			var langName = name.ToLowerInvariant();
			Type = langName;
		}

		/// <summary>
		/// A list of stopword to initialize the stop filter with. Defaults to the english stop words.
		/// </summary>
		[JsonProperty("stopwords")]
		public IEnumerable<string> StopWords { get; set; }

		[JsonProperty("stem_exclusion")]
		public IEnumerable<string> StemExclusionList { get; set; }

		/// <summary>
		/// A path (either relative to config location, or absolute) to a stopwords file configuration.
		/// </summary>
		[JsonProperty("stopwords_path")]
		public string StopwordsPath { get; set; }
	}
}


text:
/**
 *
 */
package org.topicquests.topicmap.json.merge;

import java.util.*;

import org.topicquests.common.ResultPojo;
import org.topicquests.common.api.IResult;
import org.topicquests.common.api.ITopicQuestsOntology;
import org.topicquests.model.TicketPojo;
import org.topicquests.model.api.node.INode;
import org.topicquests.model.api.node.INodeModel;
import org.topicquests.model.api.ITicket;
import org.topicquests.persist.json.api.IJSONDocStoreModel;
import org.topicquests.topicmap.json.model.api.IJSONTopicDataProvider;
import org.topicquests.topicmap.json.model.api.ISameLabelListener;
import org.topicquests.topicmap.json.model.JSONTopicmapEnvironment;

/**
 * @author park
 *
 */
public class SameLabelMergeHandler {
	private JSONTopicmapEnvironment environment;
	private IJSONTopicDataProvider database;
	private IJSONDocStoreModel jsonModel;
	private INodeModel nodeModel;
	private ITicket credentials;
	private Worker worker;
	private VirtualizerHandler virtualizerHandler;

	/**
	 *
	 */
	public SameLabelMergeHandler(JSONTopicmapEnvironment env) {
		environment = env;
		database = (IJSONTopicDataProvider)environment.getDataProvider();
		nodeModel = database.getNodeModel();
		credentials = new TicketPojo(ITopicQuestsOntology.SYSTEM_USER);
		virtualizerHandler = environment.getVirtualizerHandler();
		worker = new Worker();
	}

	public void studySameLabel(String nodeLocatorA, String nodeLocatorB, ISameLabelListener l) {
		worker.addHit(new WorkerObject(nodeLocatorA, nodeLocatorB, l));
	}

	public void shutDown() {
		if (worker != null)
			worker.shutDown();
	}
	class WorkerObject {
		public String locatorA;
		public String locatorB;
		public ISameLabelListener listener;

		public WorkerObject(String a, String b, ISameLabelListener l) {
			locatorA = a;
			locatorB = b;
			listener = l;
		}
	}
	class Worker extends Thread {
		private List<WorkerObject>hits;
		private boolean isRunning = true;

		public Worker() {
			hits = new ArrayList<WorkerObject>();
			this.start();
		}

		public void addHit(WorkerObject o) {
			synchronized(hits) {
				hits.add(o);
				hits.notify();
			}
		}
		public void shutDown() {
			synchronized(hits) {
				isRunning = false;
				hits.notify();
			}
		}
		public void run() {
			WorkerObject o = null;
			while(isRunning) {
				synchronized(hits) {
					if (hits.isEmpty()) {
						try {
							hits.wait();
						} catch (Exception e) {}
					}
					if (isRunning & !hits.isEmpty())
						o = hits.remove(0);
				}
				if (isRunning && o != null) {
					studySameLableNodes(o);
					o = null;
				}
			}
		}

	}

	/**
	 * Study two {@link INode} objects with same labels. If a new
	 * VirtualNode is created, return its locator in the
	 * output PLUS the two original locators space-separated in resultA
	 * @param nodeLocatorA
	 * @param nodeLocatorB
	 * @return
	 */
	public void studySameLableNodes(WorkerObject o) {
		String nodeLocatorA = o.locatorA;
		String nodeLocatorB = o.locatorA;
		String virtualNodeLocator = null;
		IResult result = new ResultPojo();
		IResult rA = database.getNode(nodeLocatorA, credentials);
		if (rA.hasError())
			result.addErrorString(rA.getErrorString());
		IResult rB = database.getNode(nodeLocatorB, credentials);
		if (rB.hasError())
			result.addErrorString(rB.getErrorString());
		INode nodeA = (INode)rA.getResultObject();
		INode nodeB = (INode)rB.getResultObject();
		String typeA, typeB;
		//sanity
		if (nodeA != null && nodeB != null) {
			IResult rC;
			//NOW, sort out if these are somehow the same subject
			typeA = nodeA.getNodeType();
			typeB = nodeB.getNodeType();
			if (typeA.equals(typeB)) {
				rC = studySameTypeNodes(nodeA, nodeB);
				if (rC.hasError())
					result.addErrorString(rC.getErrorString());
				virtualNodeLocator = (String)rC.getResultObject();
			} else {
				//TODO
			}

		}
		if (virtualNodeLocator != null)
			o.listener.acceptSameLabelResults(virtualNodeLocator, nodeLocatorA, nodeLocatorB, result.getErrorString());
	}

	//////////////////////////////////////
	//NOTES
	// If instanceOf OntologyClassType
	//   If memberOf same Ontology
	//   Then less likely
	//   Else...
	//
	//////////////////////////////////////
	/**
	 * Returns a virtual node locator if created
	 * @param nodeA
	 * @param nodeB
	 * @return
	 */
	IResult studySameTypeNodes(INode nodeA, INode nodeB) {
		IResult result = new ResultPojo();
		//TODO
		return result;
	}

}


text:
package com.github.dsaa.collections;

public class HashTable {

}


text: <main data-field="container" class="mainContainer"></main>


text:
  Toggle

          Toggle

          Turn On

          Turn Off

ExclusionGroup example

      1
      2
      3


text:
import sys
import time
import socket
import threading
import Queue
import ConfigParser
import logging as log

from connection import *
from connectionManager import *

log.basicConfig(level=log.DEBUG, stream=sys.stderr)

#Dynamically instantiate an instance of an Application-derived class from a module
#The module must provide an Instantiate() method
def InstantiateApplication(moduleName, *classArgs):
    module = __import__(moduleName)
    log.info("InstantiateApplication - Module: " + repr(module))
    classInstance = module.Instantiate(*classArgs)
    log.info( repr(classInstance) )
    return classInstance

#
#MAIN
#
if __name__ == "__main__":
    log.info('Loading configuration info')
    config = ConfigParser.ConfigParser()
    config.read('config.txt')
    port = config.getint('Server', 'Port')
    connectionQueueSize = config.getint('Server', 'ConnectionQueueSize')

    #Start applications
    log.info('Loading Applications...')
    applications = {}

    #Admin app
    adminApp = InstantiateApplication('AdminApplication', 'admin')
    applications['/'] = adminApp
    adminAppThread = threading.Thread(target=adminApp.Run).start()

    #Dynamically load applications specified in config.txt
    applicationList = config.items('Applications')
    for application in applicationList:
        appModuleName = application[1]
        appInstanceName = application[0]
        log.info('\tLoading instance of application %s as %s' % (appModuleName, appInstanceName))
        applicationInstance = InstantiateApplication(appModuleName, appInstanceName)
        applications['/' + appInstanceName] = applicationInstance
        applicationThread = threading.Thread(target=applicationInstance.Run).start()

    log.info("Applications Loaded:")
    log.info(repr(applications))

    #done with config
    del config

    log.info('Starting web socket server')
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serverSocket.bind( ('', port) )
    serverSocket.listen(connectionQueueSize)
    del port
    del connectionQueueSize

    #Start connection manager
    connectionManager = ConnectionManager()
    connectionManagerThread = threading.Thread(target=connectionManager.Run).start()

    #Accept clients
    try:
        while 1:
            log.info('Waiting to accept client connection...')
            clientSocket, clientAddress = serverSocket.accept()

            try:
                log.info('Got client connection from %s' % (repr(clientAddress)))
                connection = Connection(clientSocket, clientAddress)

                log.info('Client %s requested %s application' % (repr(clientAddress), connection.ApplicationPath))
                if connection.ApplicationPath in applications:
                    requestedApp = applications[connection.ApplicationPath]
                    log.info('Client %s requested app: %s ' % (repr(clientAddress), repr(requestedApp)))

                    if requestedApp.AddClient(connection) == True:
                        connectionManager.AddConnection(connection)
                    else:
                        connection.Close()
                        connection = None
                else:
                    log.info("Client %s requested an unknown Application. Closing connection." % repr(clientAddress))
                    connection.Close()
                    connection = None

            except Exception as ex:
                log.info('Execption occurred while attempting to establish client connection from %s.' % repr(clientAddress))
                log.info(repr(ex))

    except Exception as ex:
        log.info('Server encountered an unhandled exception.')
        log.info(repr(ex))

    log.info('Web socket server closing.')


text:
package nl.dtls.fairdatapoint.aoipmh;

import com.lyncode.xoai.dataprovider.filter.Scope;

/**
 *
 * @author Shamanou van Leeuwen
 * @Since 2016-07-02
 */
public final class ScopedFilter {
    private final Condition condition;
    private final Scope scope;

    public ScopedFilter(Condition condition, Scope scope) {
        this.condition = condition;
        this.scope = scope;
    }

    public Condition getCondition() {
        return condition;
    }

    public Scope getScope() {
        return scope;
    }
}


text:
#ifndef MLSL_H
#define MLSL_H

#include "nlopt.h"
#include "nlopt-util.h"

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data,
			   const double *lb, const double *ub, /* bounds */
			   double *x, /* in: initial guess, out: minimizer */
			   double *minf,
			   nlopt_stopping *stop,
                           nlopt_opt local_opt,
			   int Nsamples, /* #samples/iteration (0=default) */
                           int lds);

#ifdef __cplusplus
}  /* extern "C" */
#endif /* __cplusplus */

#endif


text:
package influent.server.rest;

import com.google.inject.name.Named;
import influent.idl.FL_Cluster;
import influent.idl.FL_DataAccess;
import influent.idl.FL_Entity;
import influent.idl.FL_EntitySearch;
import influent.idl.FL_LevelOfDetail;
import influent.idl.FL_OrderBy;
import influent.idl.FL_Property;
import influent.idl.FL_PropertyDescriptor;
import influent.idl.FL_PropertyDescriptors;
import influent.idl.FL_PropertyMatchDescriptor;
import influent.idl.FL_SearchResult;
import influent.idl.FL_SearchResults;
import influent.idlhelper.PropertyDescriptorHelper;
import influent.idlhelper.PropertyHelper;
import influent.server.configuration.ApplicationConfiguration;
import influent.server.data.PropertyMatchBuilder;
import influent.server.utilities.GuidValidator;
import influent.server.utilities.UISerializationHelper;
import influent.server.utilities.ValueFormatter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import oculus.aperture.common.JSONProperties;
import oculus.aperture.common.rest.ApertureServerResource;

import oculus.aperture.spi.common.Properties;
import org.apache.avro.AvroRemoteException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

public class EntitySearchResource extends ApertureServerResource{

	private final FL_EntitySearch _entitySearcher;
	private final FL_DataAccess _dataAccess;
	private static ApplicationConfiguration _applicationConfiguration;

	private static FL_PropertyDescriptors _searchDescriptors = null;

	private final static int DEFAULT_MAX_LIMIT = 50;

	private static final Logger s_logger = LoggerFactory.getLogger(EntitySearchResource.class);

	@Inject
	public EntitySearchResource(
		FL_EntitySearch entitySearcher,
		FL_DataAccess dataAccess,
		@Named("aperture.server.config") Properties config) {
		_entitySearcher = entitySearcher;
		_dataAccess = dataAccess;
		_applicationConfiguration = ApplicationConfiguration.getInstance(config);
	}

	private Map<String, List<Object>> groupEntitiesBy(String groupByKey, Map<String, Double> matchScores, List<FL_Entity> entities) {
		Map<String, List<Object>> groupedEntities = new LinkedHashMap<String, List<Object>>();

		final FL_PropertyDescriptor pd = PropertyDescriptorHelper.find(groupByKey, _searchDescriptors.getProperties());

		if (pd != null || (groupByKey.equals("MATCH") && matchScores.size() != 0)) {
			for (FL_Entity entity : entities) {

				FL_Property prop = PropertyHelper.getPropertyByKey(entity.getProperties(), groupByKey);
				String groupKey;

				if (pd != null) {
					groupKey = pd.getFriendlyText()+ ": " + ValueFormatter.format(prop);
				} else {
					groupKey = "Match: " + String.format("%.2f", matchScores.get(entity.getUid()));
				}

				List<Object> group = groupedEntities.get(groupKey);
				if (group == null) {
					group = new ArrayList<Object>();
					groupedEntities.put(groupKey, group);
				}

				group.add(entity);
			}
		} else {
			List<Object> group = groupedEntities.get("");

			if (group == null) {
				groupedEntities.put("", new ArrayList<Object>(entities));
			} else {
				group.addAll(entities);
			}
		}

		return groupedEntities;
	}

	private JSONArray serializeEntities(List<Object> entities) {
		JSONArray ja = new JSONArray();

		for (Object entity : entities) {
			JSONObject jo = serializeEntity(entity);
			if (jo != null) {  // skip over any objects that failed serialization
				ja.put(jo);
			}
		}
		return ja;
	}

	private JSONObject serializeEntity(Object entity) {
		String id = null;

		try {
			JSONObject jo = null;

			if (entity instanceof FL_Cluster) {
				FL_Cluster cluster = (FL_Cluster)entity;
				id = cluster.getUid();
				jo = UISerializationHelper.toUIJson(cluster);

				//adding in the entities so we don't have to do a separate call to get them all
				jo.put("memberEntities", UISerializationHelper.toUIJson(_dataAccess.getEntities(cluster.getMembers(), FL_LevelOfDetail.FULL)));
			} else {
				FL_Entity e = (FL_Entity)entity;
				id = e.getUid();
				jo = UISerializationHelper.toUIJson(e);
			}
			return jo;
		} catch (Exception e) {
			s_logger.error("Error serializing object with uid: " + id + ".   Omitting from results.");
			return null;
		}
	}

	@Post("json")
	public StringRepresentation search(String jsonData) throws ResourceException {
		Map<String, Double> matchScores = new HashMap<String, Double>();
		List<FL_Entity> entities = new ArrayList<FL_Entity>();

		try {
			JSONProperties request = new JSONProperties(jsonData);

			final String sessionId = request.getString("sessionId", null);
			if (!GuidValidator.validateGuidString(sessionId)) {
				throw new ResourceException(Status.CLIENT_ERROR_EXPECTATION_FAILED, "sessionId is not a valid UUID");
			}

			// Determine the number of results to return.
			int resultLimit = request.getInteger("limit", DEFAULT_MAX_LIMIT);
			if (resultLimit <= 0) {
				resultLimit = DEFAULT_MAX_LIMIT;
			}

			// Determine the start index.
			final int startIndex = request.getInteger("start", 0);

			//Determine level of detail, default to summary
			FL_LevelOfDetail levelOfDetail;
			try {
				levelOfDetail = FL_LevelOfDetail.valueOf(request.getString("levelOfDetail", "SUMMARY"));
			} catch (Exception e) {
				levelOfDetail = FL_LevelOfDetail.SUMMARY;
			}

			// Get the descriptors if we don't have them yet
			if (_searchDescriptors == null) {
				_searchDescriptors = _entitySearcher.getDescriptors();
			}

			// get all the search terms
			final PropertyMatchBuilder bld;
			String propMatchDesc = request.getString("descriptors", null);
			if (propMatchDesc != null) {
				bld = processDescriptors(new JSONObject(propMatchDesc));
			} else {
				bld = processSearchTerms(request.getString("query", "").trim());
			}

			final Map<String, List<FL_PropertyMatchDescriptor>> termMap = bld.getDescriptorMap();
			List<FL_OrderBy> orderBy = bld.getOrderBy();

			// return groups of one as singletons?
			final boolean ungroupSingletons = request.getBoolean("ungroupSingles", false);

			if (orderBy == null) {
				orderBy = _searchDescriptors.getOrderBy();
			}

			final String groupByKey = orderBy != null? orderBy.get(0).getPropertyKey() : null;

			// Execute the search
			FL_SearchResults sResponse = _entitySearcher.search(termMap, orderBy, (long)startIndex, (long)resultLimit, levelOfDetail);

			for (FL_SearchResult sResult : sResponse.getResults()) {
				FL_Entity entity = (FL_Entity)sResult.getResult();
				matchScores.put(entity.getUid(), sResult.getMatchScore());
				entities.add(entity);
			}

			normalizeMatchScores(matchScores);

			// search results response object
			JSONObject result = new JSONObject();

			// search result entities
			JSONArray resultItems = new JSONArray();

			// group entities if a grouping field was provided
			if (groupByKey != null && !groupByKey.equals("null")) {
				Map<String, List<Object>> groupedEntities = groupEntitiesBy(groupByKey, matchScores, entities);

				// add grouped entities to result items
				for (String key : groupedEntities.keySet()) {
					final List<Object> group = groupedEntities.get(key);

					JSONObject groupResult = new JSONObject();

					// don't provide a group key if ungroupSingletons and the group size is 1
					if (!ungroupSingletons || group.size() > 1) {
						groupResult.put("groupKey", key);
					}

					JSONArray groupMembers = serializeEntities(group);

					groupResult.put("items", groupMembers);

					resultItems.put(groupResult);
				}
			} else {
				// create a single anonymous group for all results
				JSONObject groupResult = new JSONObject();

				// under an empty group header
				groupResult.put("groupKey", "");

				List<Object> allResults = new ArrayList<Object>(entities.size());
				allResults.addAll(entities);

				groupResult.put("items", serializeEntities(allResults));

				resultItems.put(groupResult);
			}

			// Get the column header from the searcher
			FL_PropertyDescriptors columnHeaders = _entitySearcher.getKeyDescriptors(sResponse, orderBy);

			result.put("data", resultItems);
			result.put("matchScores", matchScores);
			result.put("totalResults", sResponse.getTotal());
			result.put("sessionId", sessionId);
			result.put("headers", UISerializationHelper.toUIJson(columnHeaders));
			result.put("detailLevel", sResponse.getLevelOfDetail());

			return new StringRepresentation(result.toString(),MediaType.APPLICATION_JSON);
		}
		catch (JSONException e) {
			throw new ResourceException(
				Status.CLIENT_ERROR_BAD_REQUEST,
				"Unable to create JSON object from supplied options string",
				e
			);
		} catch (AvroRemoteException e) {
			throw new ResourceException(
					Status.CLIENT_ERROR_BAD_REQUEST,
					"Exception during AVRO processing",
					e
				);
		}
	}

	private PropertyMatchBuilder processSearchTerms(String query) {
		//Extract a map of FL_PropertyMatchDescriptors by type from the query
		s_logger.info("Processing search terms from: " + query);
		final PropertyMatchBuilder terms = new PropertyMatchBuilder(query, _searchDescriptors, false, _applicationConfiguration.hasMultipleEntityTypes());

		return terms;
	}

	private PropertyMatchBuilder processDescriptors(JSONObject propertyMatchDescriptorsMap) throws JSONException {
		//Extract a map of FL_PropertyMatchDescriptors by type from the propertyMatchDescriptors
		s_logger.info("Processing search terms from: " + propertyMatchDescriptorsMap);
		final PropertyMatchBuilder terms = new PropertyMatchBuilder(propertyMatchDescriptorsMap, false, _applicationConfiguration.hasMultipleEntityTypes());

		return terms;
	}

	public static void normalizeMatchScores (Map<String, Double> matchScores) {
		double maxscore = 0;

		for (String entity : matchScores.keySet())
			if (matchScores.get(entity) > maxscore)
				maxscore = matchScores.get(entity);

		if (maxscore != 0)
			for (String entity : matchScores.keySet())
				matchScores.put(entity, matchScores.get(entity)/maxscore);
	}
}