text:
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: sreeni@google.com (Sreeni Viswanadha)

package org.javacc.parser;

import static org.javacc.parser.JavaCCGlobals.cu_from_insertion_point_2;
import static org.javacc.parser.JavaCCGlobals.cu_name;
import static org.javacc.parser.JavaCCGlobals.cu_to_insertion_point_2;
import static org.javacc.parser.JavaCCGlobals.getFileExtension;
import static org.javacc.parser.JavaCCGlobals.jj2index;
import static org.javacc.parser.JavaCCGlobals.jjtreeGenerated;
import static org.javacc.parser.JavaCCGlobals.lookaheadNeeded;
import static org.javacc.parser.JavaCCGlobals.maskVals;
import static org.javacc.parser.JavaCCGlobals.maskindex;
import static org.javacc.parser.JavaCCGlobals.tokenCount;
import static org.javacc.parser.JavaCCGlobals.toolName;
import static org.javacc.parser.JavaCCGlobals.toolNames;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Generate the parser.
 */
public class ParseGenCPP extends ParseGen {

public void start() throws MetaParseException {

    Token t = null;

    if (JavaCCErrors.get_error_count() != 0) throw new MetaParseException();

    List<String> tn = new ArrayList<String>(toolNames);
    tn.add(toolName);
    switchToStaticsFile();

    switchToIncludeFile();

    //standard includes
    genCodeLine("#include \"JavaCC.h\"");
    genCodeLine("#include \"CharStream.h\"");
    genCodeLine("#include \"Token.h\"");
    genCodeLine("#include \"TokenManager.h\"");

    Object object = Options.objectValue(Options.USEROPTION__CPP_PARSER_INCLUDE);

    if (object instanceof String) {
    	String include = (String)object;
    	if (include.length() > 0) {
    		if (include.charAt(0) == '<')
    			genCodeLine("#include " + include);
    		else
    			genCodeLine("#include \"" + include + "\"");
    	}
    }

    genCodeLine("#include \"" + cu_name + "Constants.h\"");

    if (jjtreeGenerated) {
      genCodeLine("#include \"JJT" + cu_name + "State.h\"");
    }

    genCodeLine("#include \"ErrorHandler.h\"");

    if (jjtreeGenerated) {
      genCodeLine("#include \"" + cu_name + "Tree.h\"");
    }

    if (Options.stringValue(Options.USEROPTION__CPP_NAMESPACE).length() > 0) {
      genCodeLine("namespace " + Options.stringValue("NAMESPACE_OPEN"));
    }

    genCodeLine("  struct JJCalls {");
    genCodeLine("    int        gen;");
    genCodeLine("    int        arg;");
    genCodeLine("    JJCalls*   next;");
    genCodeLine("    Token*     first;");
    genCodeLine("    ~JJCalls() { if (next) delete next; }");
    genCodeLine("     JJCalls() { next = nullptr; arg = 0; gen = -1; first = nullptr; }");
    genCodeLine("  };");
    genCodeLine("");

    String superClass = Options.stringValue(Options.USEROPTION__PARSER_SUPER_CLASS);
    genClassStart("", cu_name, new String[]{},
                  superClass == null  ? new String[0] : new String[] {
                   "public " + superClass});
    switchToMainFile();
    if (cu_to_insertion_point_2.size() != 0) {
      printTokenSetup((cu_to_insertion_point_2.get(0)));
      for (Iterator<?> it = cu_to_insertion_point_2.iterator(); it.hasNext();) {
        t = (Token)it.next();
        printToken(t);
      }
    }

    switchToMainFile();
    /*genCodeLine("typedef class _LookaheadSuccess { } *LookaheadSuccess; // Dummy class");
    genCodeLine("  static LookaheadSuccess jj_ls = new _LookaheadSuccess();");*/

    genCodeLine("");
    genCodeLine("");

    new ParseEngine().build(this);

    switchToIncludeFile();
    genCodeLine("");
    genCodeLine("public: ");
    genCodeLine("  void setErrorHandler(ErrorHandler *eh) {");
    genCodeLine("    if (errorHandler) delete errorHandler;");
    genCodeLine("    errorHandler = eh;");
    genCodeLine("  }");
    genCodeLine("");
    genCodeLine("  TokenManager *token_source = nullptr;");
    genCodeLine("  CharStream   *jj_input_stream = nullptr;");
    genCodeLine("  /** Current token. */");
    genCodeLine("  Token        *token = nullptr;");
    genCodeLine("  /** Next token. */");
    genCodeLine("  Token        *jj_nt = nullptr;");
    genCodeLine("");
    genCodeLine("private: ");
    genCodeLine("  int           jj_ntk;");

    genCodeLine("  JJCalls       jj_2_rtns[" + (jj2index + 1) + "];");
    genCodeLine("  bool          jj_rescan;");
    genCodeLine("  int           jj_gc;");
    genCodeLine("  Token        *jj_scanpos, *jj_lastpos;");
    genCodeLine("  int           jj_la;");
    genCodeLine("  /** Whether we are looking ahead. */");
    genCodeLine("  bool          jj_lookingAhead;");
    genCodeLine("  bool          jj_semLA;");

    genCodeLine("  int           jj_gen;");
    genCodeLine("  int           jj_la1[" + (maskindex + 1) + "];");
    genCodeLine("  ErrorHandler *errorHandler = nullptr;");
    genCodeLine("");
    genCodeLine("protected: ");
    genCodeLine("  bool          hasError;");
    genCodeLine("");
    int tokenMaskSize = (tokenCount-1)/32 + 1;

    if (Options.getErrorReporting() && tokenMaskSize > 0) {
      switchToStaticsFile();
      for (int i = 0; i < tokenMaskSize; i++) {
        if (maskVals.size() > 0) {
          genCodeLine("  unsigned int jj_la1_" + i + "[] = {");
          for (Iterator<?> it = maskVals.iterator(); it.hasNext();) {
            int[] tokenMask = (int[])(it.next());
            genCode("0x" + Integer.toHexString(tokenMask[i]) + ",");
          }
          genCodeLine("};");
        }
      }
    }

    if (Options.getDepthLimit() > 0) {
      genCodeLine("  private: int jj_depth;");
      genCodeLine("  private: bool jj_depth_error;");
      genCodeLine("  friend class __jj_depth_inc;");
      genCodeLine("  class __jj_depth_inc {public:");
      genCodeLine("    " + cu_name + "* parent;");
      genCodeLine("    __jj_depth_inc(" + cu_name + "* p): parent(p) { parent->jj_depth++; };");
      genCodeLine("    ~__jj_depth_inc(){ parent->jj_depth--; }");
      genCodeLine("  };");
    }
    if (!Options.getStackLimit().equals("")) {
      genCodeLine("  public: size_t jj_stack_limit;");
      genCodeLine("  private: void* jj_stack_base;");
      genCodeLine("  private: bool jj_stack_error;");
    }

    genCodeLine("");

    genCodeLine("  /** Constructor with user supplied TokenManager. */");

    switchToIncludeFile(); // TEMP
    genCodeLine("  Token *head; ");
    genCodeLine("public: ");
    generateMethodDefHeader(" ", cu_name, cu_name + "(TokenManager *tokenManager)");
    if (superClass != null)
    {
      genCodeLine(" : " + superClass + "()");
    }
    genCodeLine("{");
    genCodeLine("    head = nullptr;");
    genCodeLine("    ReInit(tokenManager);");
    if (Options.getTokenManagerUsesParser())
    	genCodeLine("    tokenManager->setParser(this);");
    genCodeLine("}");

    switchToIncludeFile();
    genCodeLine("  virtual ~" + cu_name + "();");
    switchToMainFile();
    genCodeLine("" + cu_name + "::~" +cu_name + "()");
    genCodeLine("{");
    genCodeLine("  clear();");
    genCodeLine("}");
    generateMethodDefHeader("void", cu_name, "ReInit(TokenManager* tokenManager)");
    genCodeLine("{");
    genCodeLine("    clear();");
    genCodeLine("    errorHandler = new ErrorHandler();");
    genCodeLine("    hasError = false;");
    genCodeLine("    token_source = tokenManager;");
    genCodeLine("    head = token = new Token();");
    genCodeLine("    token->kind = 0;");
    genCodeLine("    token->next = nullptr;");
    genCodeLine("    jj_lookingAhead = false;");
    genCodeLine("    jj_rescan = false;");
    genCodeLine("    jj_done = false;");
    genCodeLine("    jj_scanpos = jj_lastpos = nullptr;");
    genCodeLine("    jj_gc = 0;");
    genCodeLine("    jj_kind = -1;");
    genCodeLine("    indent = 0;");
    genCodeLine("    trace = " + Options.getDebugParser() + ";");
    if (!Options.getStackLimit().equals("")) {
      genCodeLine("    jj_stack_limit = "+Options.getStackLimit()+";");
      genCodeLine("    jj_stack_error = jj_stack_check(true);");
    }

    if (Options.getCacheTokens()) {
      genCodeLine("    token->next = jj_nt = token_source->getNextToken();");
    } else {
      genCodeLine("    jj_ntk = -1;");
    }
    if (jjtreeGenerated) {
      genCodeLine("    jjtree.reset();");
    }
    if (Options.getDepthLimit() > 0) {
      genCodeLine("    jj_depth = 0;");
      genCodeLine("    jj_depth_error = false;");
    }
    if (Options.getErrorReporting()) {
      genCodeLine("    jj_gen = 0;");
      if (maskindex > 0) {
        genCodeLine("    for (int i = 0; i < " + maskindex + "; i++) jj_la1[i] = -1;");
      }
    }
    genCodeLine("  }");
    genCodeLine("");

    generateMethodDefHeader("void", cu_name, "clear()");
    genCodeLine("{");
    genCodeLine("  //Since token manager was generate from outside,");
    genCodeLine("  //parser should not take care of deleting");
    genCodeLine("  //if (token_source) delete token_source;");
    genCodeLine("  if (head) {");
    genCodeLine("    Token *next, *t = head;");
    genCodeLine("    while (t) {");
    genCodeLine("      next = t->next;");
    genCodeLine("      delete t;");
    genCodeLine("      t = next;");
    genCodeLine("    }");
    genCodeLine("  }");
    genCodeLine("  if (errorHandler) {");
    genCodeLine("    delete errorHandler, errorHandler = nullptr;");
    genCodeLine("  }");
    if (Options.getDepthLimit() > 0) {
      genCodeLine("  assert(jj_depth==0);");
    }
    genCodeLine("}");
    genCodeLine("");

    if (!Options.getStackLimit().equals("")) {
      genCodeLine("");
      switchToIncludeFile();
      genCodeLine(" virtual");
      switchToMainFile();
      generateMethodDefHeader("bool ", cu_name, "jj_stack_check(bool init)");
      genCodeLine("  {");
      genCodeLine("     if(init) {");
      genCodeLine("       jj_stack_base = nullptr;");
      genCodeLine("       return false;");
      genCodeLine("     } else {");
      genCodeLine("       volatile int q = 0;");
      genCodeLine("       if(!jj_stack_base) {");
      genCodeLine("         jj_stack_base = (void*)&q;");
      genCodeLine("         return false;");
      genCodeLine("       } else {");
      genCodeLine("         // Stack can grow in both directions, depending on arch");
      genCodeLine("         std::ptrdiff_t used = (char*)jj_stack_base-(char*)&q;");
      genCodeLine("         return (std::abs(used) > jj_stack_limit);");
      genCodeLine("       }");
      genCodeLine("     }");
      genCodeLine("  }");
    }

    generateMethodDefHeader("Token *", cu_name, "jj_consume_token(int kind)", "ParseException");
    genCodeLine("  {");
    if (!Options.getStackLimit().equals("")) {
      genCodeLine("    if(kind != -1 && (jj_stack_error || jj_stack_check(false))) {");
      genCodeLine("      if (!jj_stack_error) {");
      genCodeLine("        errorHandler->handleOtherError(\"Stack overflow while trying to parse\", this);");
      genCodeLine("        jj_stack_error=true;");
      genCodeLine("      }");
      genCodeLine("      return jj_consume_token(-1);");
      genCodeLine("    }");
    }
    if (Options.getCacheTokens()) {
      genCodeLine("    Token *oldToken = token;");
      genCodeLine("    if ((token = jj_nt)->next != nullptr) jj_nt = jj_nt->next;");
      genCodeLine("    else jj_nt = jj_nt->next = token_source->getNextToken();");
    } else {
      genCodeLine("    Token *oldToken;");
      genCodeLine("    if ((oldToken = token)->next != nullptr) token = token->next;");
      genCodeLine("    else token = token->next = token_source->getNextToken();");
      genCodeLine("    jj_ntk = -1;");
    }
    genCodeLine("    if (token->kind == kind) {");
    if (Options.getErrorReporting()) {
      genCodeLine("      jj_gen++;");
      if (jj2index != 0) {
        genCodeLine("      if (++jj_gc > 100) {");
        genCodeLine("        jj_gc = 0;");
        genCodeLine("        for (int i = 0; i < " + jj2index + "; i++) {");
        genCodeLine("          JJCalls *c = &jj_2_rtns[i];");
        genCodeLine("          while (c != nullptr) {");
        genCodeLine("            if (c->gen < jj_gen) c->first = nullptr;");
        genCodeLine("            c = c->next;");
        genCodeLine("          }");
        genCodeLine("        }");
        genCodeLine("      }");
      }
    }
    if (Options.getDebugParser()) {
      genCodeLine("      trace_token(token, \"\");");
    }
    genCodeLine("      return token;");
    genCodeLine("    }");
    if (Options.getCacheTokens()) {
      genCodeLine("    jj_nt = token;");
    }
    genCodeLine("    token = oldToken;");
    if (Options.getErrorReporting()) {
      genCodeLine("    jj_kind = kind;");
    }
    //genCodeLine("    throw generateParseException();");
    if (!Options.getStackLimit().equals("")) {
      genCodeLine("    if (!jj_stack_error) {");
    }
    genCodeLine("    JJString image = kind >= 0 ? tokenImage[kind] : tokenImage[0];");
    genCodeLine("    errorHandler->handleUnexpectedToken(kind, image.substr(1, image.size() - 2), getToken(1), this);");
    if (!Options.getStackLimit().equals("")) {
      genCodeLine("    }");
    }
    genCodeLine("    hasError = true;");
    genCodeLine("    return token;");
    genCodeLine("  }");
    genCodeLine("");

    if (jj2index != 0) {
      switchToMainFile();
      generateMethodDefHeader("bool ", cu_name, "jj_scan_token(int kind)");
      genCodeLine("{");
      if (!Options.getStackLimit().equals("")) {
        genCodeLine("    if(kind != -1 && (jj_stack_error || jj_stack_check(false))) {");
        genCodeLine("      if (!jj_stack_error) {");
        genCodeLine("        errorHandler->handleOtherError(\"Stack overflow while trying to parse\", this);");
        genCodeLine("        jj_stack_error=true;");
        genCodeLine("      }");
        genCodeLine("      return jj_consume_token(-1);");
        genCodeLine("    }");
      }
      genCodeLine("    if (jj_scanpos == jj_lastpos) {");
      genCodeLine("      jj_la--;");
      genCodeLine("      if (jj_scanpos->next == nullptr) {");
      genCodeLine("        jj_lastpos = jj_scanpos = jj_scanpos->next = token_source->getNextToken();");
      genCodeLine("      } else {");
      genCodeLine("        jj_lastpos = jj_scanpos = jj_scanpos->next;");
      genCodeLine("      }");
      genCodeLine("    } else {");
      genCodeLine("      jj_scanpos = jj_scanpos->next;");
      genCodeLine("    }");
      if (Options.getErrorReporting()) {
        genCodeLine("    if (jj_rescan) {");
        genCodeLine("      int i = 0; Token *tok = token;");
        genCodeLine("      while (tok != nullptr && tok != jj_scanpos) { i++; tok = tok->next; }");
        genCodeLine("      if (tok != nullptr) jj_add_error_token(kind, i);");
        if (Options.getDebugLookahead()) {
          genCodeLine("    } else {");
          genCodeLine("      trace_scan(jj_scanpos, kind);");
        }
        genCodeLine("    }");
      } else if (Options.getDebugLookahead()) {
        genCodeLine("    trace_scan(jj_scanpos, kind);");
      }
      genCodeLine("    if (jj_scanpos->kind != kind) return true;");
      //genCodeLine("    if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;");
      genCodeLine("    if (jj_la == 0 && jj_scanpos == jj_lastpos) { return jj_done = true; }");
      genCodeLine("    return false;");
      genCodeLine("  }");
      genCodeLine("");
    }
    genCodeLine("");
    genCodeLine("/** Get the next Token. */");
    generateMethodDefHeader("Token *", cu_name, "getNextToken()");
    genCodeLine("{");
    if (Options.getCacheTokens()) {
      genCodeLine("    if ((token = jj_nt)->next != nullptr) jj_nt = jj_nt->next;");
      genCodeLine("    else jj_nt = jj_nt->next = token_source->getNextToken();");
    } else {
      genCodeLine("    if (token->next != nullptr) token = token->next;");
      genCodeLine("    else token = token->next = token_source->getNextToken();");
      genCodeLine("    jj_ntk = -1;");
    }
    if (Options.getErrorReporting()) {
      genCodeLine("    jj_gen++;");
    }
    if (Options.getDebugParser()) {
      genCodeLine("      trace_token(token, \" (in getNextToken)\");");
    }
    genCodeLine("    return token;");
    genCodeLine("  }");
    genCodeLine("");
    genCodeLine("/** Get the specific Token. */");
    generateMethodDefHeader("Token *", cu_name, "getToken(int index)");
    genCodeLine("{");
    if (lookaheadNeeded) {
      genCodeLine("    Token *t = jj_lookingAhead ? jj_scanpos : token;");
    } else {
      genCodeLine("    Token *t = token;");
    }
    genCodeLine("    for (int i = 0; i < index; i++) {");
    genCodeLine("      if (t->next != nullptr) t = t->next;");
    genCodeLine("      else t = t->next = token_source->getNextToken();");
    genCodeLine("    }");
    genCodeLine("    return t;");
    genCodeLine("  }");
    genCodeLine("");
    if (!Options.getCacheTokens()) {
      generateMethodDefHeader("int", cu_name, "jj_ntk_f()");
      genCodeLine("{");

      genCodeLine("    if ((jj_nt=token->next) == nullptr)");
      genCodeLine("      return (jj_ntk = (token->next=token_source->getNextToken())->kind);");
      genCodeLine("    else");
      genCodeLine("      return (jj_ntk = jj_nt->kind);");
      genCodeLine("  }");
      genCodeLine("");
    }

    switchToIncludeFile();
    genCodeLine("private:");
    genCodeLine("  int jj_kind;");
    if (Options.getErrorReporting()) {
      genCodeLine("  int **jj_expentries;");
      genCodeLine("  int *jj_expentry;");
      if (jj2index != 0) {
        switchToStaticsFile();
        // For now we don't support ERROR_REPORTING in the C++ version.
        //genCodeLine("  static int *jj_lasttokens = new int[100];");
        //genCodeLine("  static int jj_endpos;");
        genCodeLine("");

        generateMethodDefHeader("  void",  cu_name, "jj_add_error_token(int kind, int pos)");
        genCodeLine("  {");
        // For now we don't support ERROR_REPORTING in the C++ version.

        //genCodeLine("    if (pos >= 100) return;");
        //genCodeLine("    if (pos == jj_endpos + 1) {");
        //genCodeLine("      jj_lasttokens[jj_endpos++] = kind;");
        //genCodeLine("    } else if (jj_endpos != 0) {");
        //genCodeLine("      jj_expentry = new int[jj_endpos];");
        //genCodeLine("      for (int i = 0; i < jj_endpos; i++) {");
        //genCodeLine("        jj_expentry[i] = jj_lasttokens[i];");
        //genCodeLine("      }");
        //genCodeLine("      jj_entries_loop: for (java.util.Iterator it = jj_expentries.iterator(); it.hasNext();) {");
        //genCodeLine("        int[] oldentry = (int[])(it->next());");
        //genCodeLine("        if (oldentry.length == jj_expentry.length) {");
        //genCodeLine("          for (int i = 0; i < jj_expentry.length; i++) {");
        //genCodeLine("            if (oldentry[i] != jj_expentry[i]) {");
        //genCodeLine("              continue jj_entries_loop;");
        //genCodeLine("            }");
        //genCodeLine("          }");
        //genCodeLine("          jj_expentries.add(jj_expentry);");
        //genCodeLine("          break jj_entries_loop;");
        //genCodeLine("        }");
        //genCodeLine("      }");
        //genCodeLine("      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;");
        //genCodeLine("    }");
        genCodeLine("  }");
      }
      genCodeLine("");

      switchToIncludeFile();
      genCodeLine("protected:");
      genCodeLine("  /** Generate ParseException. */");
      generateMethodDefHeader("  virtual void ",  cu_name, "parseError()");
      genCodeLine("   {");
      if (Options.getErrorReporting()) {
        genCodeLine("      fprintf(stderr, \"Parse error at: %d:%d, after token: %s encountered: %s\\n\", token->beginLine, token->beginColumn, addUnicodeEscapes(token->image).c_str(), addUnicodeEscapes(getToken(1)->image).c_str());");
      }
      genCodeLine("   }");
      /*generateMethodDefHeader("ParseException",  cu_name, "generateParseException()");
      genCodeLine("   {");
      //genCodeLine("    jj_expentries.clear();");
      //genCodeLine("    bool[] la1tokens = new boolean[" + tokenCount + "];");
      //genCodeLine("    if (jj_kind >= 0) {");
      //genCodeLine("      la1tokens[jj_kind] = true;");
      //genCodeLine("      jj_kind = -1;");
      //genCodeLine("    }");
      //genCodeLine("    for (int i = 0; i < " + maskindex + "; i++) {");
      //genCodeLine("      if (jj_la1[i] == jj_gen) {");
      //genCodeLine("        for (int j = 0; j < 32; j++) {");
      //for (int i = 0; i < (tokenCount-1)/32 + 1; i++) {
        //genCodeLine("          if ((jj_la1_" + i + "[i] & (1<<j)) != 0) {");
        //genCode("            la1tokens[");
        //if (i != 0) {
          //genCode((32*i) + "+");
        //}
        //genCodeLine("j] = true;");
        //genCodeLine("          }");
      //}
      //genCodeLine("        }");
      //genCodeLine("      }");
      //genCodeLine("    }");
      //genCodeLine("    for (int i = 0; i < " + tokenCount + "; i++) {");
      //genCodeLine("      if (la1tokens[i]) {");
      //genCodeLine("        jj_expentry = new int[1];");
      //genCodeLine("        jj_expentry[0] = i;");
      //genCodeLine("        jj_expentries.add(jj_expentry);");
      //genCodeLine("      }");
      //genCodeLine("    }");
      //if (jj2index != 0) {
        //genCodeLine("    jj_endpos = 0;");
        //genCodeLine("    jj_rescan_token();");
        //genCodeLine("    jj_add_error_token(0, 0);");
      //}
      //genCodeLine("    int exptokseq[][1] = new int[1];");
      //genCodeLine("    for (int i = 0; i < jj_expentries.size(); i++) {");
      //if (!Options.getGenerateGenerics())
         //genCodeLine("      exptokseq[i] = (int[])jj_expentries.get(i);");
      //else
         //genCodeLine("      exptokseq[i] = jj_expentries.get(i);");
      //genCodeLine("    }");
      genCodeLine("    return new _ParseException();");//token, nullptr, tokenImage);");
      genCodeLine("  }"); */
    } else {
      genCodeLine("protected:");
      genCodeLine("  /** Generate ParseException. */");
      generateMethodDefHeader("virtual void ",  cu_name, "parseError()");
      genCodeLine("   {");
      if (Options.getErrorReporting()) {
        genCodeLine("      fprintf(stderr, \"Parse error at: %d:%d, after token: %s encountered: %s\\n\", token->beginLine, token->beginColumn, addUnicodeEscapes(token->image).c_str(), addUnicodeEscapes(getToken(1)->image).c_str());");
      }
      genCodeLine("   }");
      /*generateMethodDefHeader("ParseException",  cu_name, "generateParseException()");
      genCodeLine("   {");
      genCodeLine("    Token *errortok = token->next;");
      if (Options.getKeepLineColumn())
         genCodeLine("    int line = errortok.beginLine, column = errortok.beginColumn;");
      genCodeLine("    JJString mess = (errortok->kind == 0) ? tokenImage[0] : errortok->image;");
      if (Options.getKeepLineColumn())
         genCodeLine("    return new _ParseException();");// +
             //"\"Parse error at line \" + line + \", column \" + column + \".  " +
             //"Encountered: \" + mess);");
      else
         genCodeLine("    return new _ParseException();");//\"Parse error at <unknown location>.  " +
                 //"Encountered: \" + mess);");
      genCodeLine("  }");*/
    }
    genCodeLine("");

    switchToIncludeFile();
    genCodeLine("private:");
    genCodeLine("  int  indent;	// trace indentation");
    genCodeLine("  bool trace = " + Options.getDebugParser() + "; // trace enabled if true");
    genCodeLine("");
    genCodeLine("public:");
    generateMethodDefHeader("  bool",  cu_name, "trace_enabled()");
    genCodeLine("  {");
    genCodeLine("    return trace;");
    genCodeLine("  }");
    genCodeLine("");
   if (Options.getDebugParser()) {
      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "enable_tracing()");
      genCodeLine("{");
      genCodeLine("    trace = true;");
      genCodeLine("}");
      genCodeLine("");

      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "disable_tracing()");
      genCodeLine("{");
      genCodeLine("    trace = false;");
      genCodeLine("}");
      genCodeLine("");

      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "trace_call(const char *s)");
      genCodeLine("  {");
      genCodeLine("    if (trace_enabled()) {");
      genCodeLine("      for (int i = 0; i < indent; i++) { printf(\" \"); }");
      genCodeLine("      printf(\"Call:   %s\\n\", s);");
      genCodeLine("    }");
      genCodeLine("    indent = indent + 2;");
      genCodeLine("  }");
      genCodeLine("");

      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "trace_return(const char *s)");
      genCodeLine("  {");
      genCodeLine("    indent = indent - 2;");
      genCodeLine("    if (trace_enabled()) {");
      genCodeLine("      for (int i = 0; i < indent; i++) { printf(\" \"); }");
      genCodeLine("      printf(\"Return: %s\\n\", s);");
      genCodeLine("    }");
      genCodeLine("  }");
      genCodeLine("");

      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "trace_token(Token *t, const char *where)");
      genCodeLine("  {");
      genCodeLine("    if (trace_enabled()) {");
      genCodeLine("      for (int i = 0; i < indent; i++) { printf(\" \"); }");
      genCodeLine("      printf(\"Consumed token: <kind: %d(%s), \\\"%s\\\"\", t->kind, addUnicodeEscapes(tokenImage[t->kind]).c_str(), addUnicodeEscapes(t->image).c_str());");
      //genCodeLine("      if (t->kind != 0 && !tokenImage[t->kind].equals(\"\\\"\" + t->image + \"\\\"\")) {");
      //genCodeLine("        System.out.print(\": \\\"\" + t->image + \"\\\"\");");
      //genCodeLine("      }");
      genCodeLine("      printf(\" at line %d column %d> %s\\n\", t->beginLine, t->beginColumn, where);");
      genCodeLine("    }");
      genCodeLine("  }");
      genCodeLine("");

      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "trace_scan(Token *t1, int t2)");
      genCodeLine("  {");
      genCodeLine("    if (trace_enabled()) {");
      genCodeLine("      for (int i = 0; i < indent; i++) { printf(\" \"); }");
      genCodeLine("      printf(\"Visited token: <Kind: %d(%s), \\\"%s\\\"\", t1->kind, addUnicodeEscapes(tokenImage[t1->kind]).c_str(), addUnicodeEscapes(t1->image).c_str());");
      //genCodeLine("      if (t1->kind != 0 && !tokenImage[t1->kind].equals(\"\\\"\" + t1->image + \"\\\"\")) {");
      //genCodeLine("        System.out.print(\": \\\"\" + t1->image + \"\\\"\");");
      //genCodeLine("      }");
      genCodeLine("      printf(\" at line %d column %d>; Expected token: %s\\n\", t1->beginLine, t1->beginColumn, addUnicodeEscapes(tokenImage[t2]).c_str());");
      genCodeLine("    }");
      genCodeLine("  }");
      genCodeLine("");
    } else {
      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "enable_tracing()");
      genCodeLine("  {");
      genCodeLine("  }");
      switchToIncludeFile();
      generateMethodDefHeader("  void",  cu_name, "disable_tracing()");
      genCodeLine("  {");
      genCodeLine("  }");
      genCodeLine("");
    }

    if (jj2index != 0 && Options.getErrorReporting()) {
      generateMethodDefHeader("  void",  cu_name, "jj_rescan_token()");
      genCodeLine("{");
      genCodeLine("    jj_rescan = true;");
      genCodeLine("    for (int i = 0; i < " + jj2index + "; i++) {");
      //genCodeLine("    try {");
      genCodeLine("      JJCalls *p = &jj_2_rtns[i];");
      genCodeLine("      do {");
      genCodeLine("        if (p->gen > jj_gen) {");
      genCodeLine("          jj_la = p->arg; jj_lastpos = jj_scanpos = p->first;");
      genCodeLine("          switch (i) {");
      for (int i = 0; i < jj2index; i++) {
        genCodeLine("            case " + i + ": jj_3_" + (i+1) + "(); break;");
      }
      genCodeLine("          }");
      genCodeLine("        }");
      genCodeLine("        p = p->next;");
      genCodeLine("      } while (p != nullptr);");
      //genCodeLine("      } catch(LookaheadSuccess ls) { }");
      genCodeLine("    }");
      genCodeLine("    jj_rescan = false;");
      genCodeLine("  }");
      genCodeLine("");

      generateMethodDefHeader("  void",  cu_name, "jj_save(int index, int xla)");
      genCodeLine("{");
      genCodeLine("    JJCalls *p = &jj_2_rtns[index];");
      genCodeLine("    while (p->gen > jj_gen) {");
      genCodeLine("      if (p->next == nullptr) { p = p->next = new JJCalls(); break; }");
      genCodeLine("      p = p->next;");
      genCodeLine("    }");
      genCodeLine("    p->gen = jj_gen + xla - jj_la; p->first = token; p->arg = xla;");
      genCodeLine("  }");
      genCodeLine("");
    }

    if (cu_from_insertion_point_2.size() != 0) {
      printTokenSetup((cu_from_insertion_point_2.get(0))); this.ccol = 1;
      for (Iterator<?> it = cu_from_insertion_point_2.iterator(); it.hasNext();) {
        t = (Token)it.next();
        printToken(t);
      }
      printTrailingComments(t);
    }
    genCodeLine("");

    // in the include file close the class signature
    switchToIncludeFile();

    // copy other stuff
    Token t1 = JavaCCGlobals.otherLanguageDeclTokenBeg;
    Token t2 = JavaCCGlobals.otherLanguageDeclTokenEnd;
    while(t1 != t2) {
      printToken(t1);
      t1 = t1.next;
    }
    genCodeLine("\n");
    if (jjtreeGenerated) {
      genCodeLine("  JJT" + cu_name + "State jjtree;");
    }
    genCodeLine("private:");
    genCodeLine("  bool jj_done;");

    genCodeLine( "};");

    saveOutput(Options.getOutputDirectory() + File.separator + cu_name + getFileExtension(Options.getOutputLanguage()));
  }

   public static void reInit()
   {
      lookaheadNeeded = false;
   }

}


text:
package com.google.copybara.exception;

/**
 * A special case of {@link ValidationException} when the error is likely to be a user error related
 * to access to the repo (access denied, not found, etc.)
 */
public class AccessValidationException extends ValidationException {

  public AccessValidationException(String message) {
    super(message);

  }

  public AccessValidationException(String message, Throwable cause) {
    super(message, cause);
  }
}

redpajama_set_name: RedPajamaGithub


text:
package com.dremio.exec.store.parquet;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.dremio.BaseTestQuery;
import com.dremio.exec.planner.physical.PlannerSettings;

/**
 * DRILL-4704
 */
public class TestFixedLenDecimal extends BaseTestQuery {

  private static final String DATAFILE = "cp.\"parquet/fixedlenDecimal.parquet\"";

  // enable decimal data type
  @BeforeClass
  public static void enableDecimalDataType() throws Exception {
    runSQL(String.format("alter system set \"%s\" = true", PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY));
  }

  @AfterClass
  public static void disableDecimalDataType() throws Exception {
    runSQL(String.format("alter system set \"%s\" = false", PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY));
  }

  @Test
  public void testNullCount() throws Exception {
    String query = String.format("select count(*) as c from %s where department_id is null", DATAFILE);
    testBuilder()
      .sqlQuery(query)
      .unOrdered()
      .baselineColumns("c")
      .baselineValues(1L)
      .build()
      .run();
  }

  @Test
  public void testNotNullCount() throws Exception {
    String query = String.format("select count(*) as c from %s where department_id is not null", DATAFILE);
    testBuilder()
      .sqlQuery(query)
      .unOrdered()
      .baselineColumns("c")
      .baselineValues(106L)
      .build()
      .run();
  }

  @Test
  public void testSimpleQueryWithCast() throws Exception {
    String query = String.format("select cast(department_id as bigint) as c from %s where cast(employee_id as decimal) = 170", DATAFILE);
    testBuilder()
      .sqlQuery(query)
      .unOrdered()
      .baselineColumns("c")
      .baselineValues(80L)
      .build()
      .run();
  }

  @Test
  public void testSimpleQueryDrill4704Fix() throws Exception {
    String query = String.format("select cast(department_id as bigint) as c from %s where employee_id = 170", DATAFILE);
    testBuilder()
      .sqlQuery(query)
      .unOrdered()
      .baselineColumns("c")
      .baselineValues(80L)
      .build()
      .run();
  }

  @Test
  public void testHash32FunctionWithSeed() throws Exception {
    try (AutoCloseable option = withOption(PlannerSettings.ENABLE_REDUCE_PROJECT, false)) {
      String query = String.format("select hash32(department_id, hash32(employee_id)) hash1, hash32(employee_id) hash2 from %s where department_id is null", DATAFILE);
      testBuilder()
        .sqlQuery(query)
        .unOrdered()
        .baselineColumns("hash1", "hash2")
        .baselineValues(1216062191, 1216062191)
        .go();
    }

  }

  @Test
  public void testHash64FunctionWithSeed() throws Exception {
    try (AutoCloseable option = withOption(PlannerSettings.ENABLE_REDUCE_PROJECT, false)) {
      String query = String.format("select hash64(department_id, hash64(employee_id)) hash1, hash64(employee_id) hash2 from %s where department_id is null", DATAFILE);
      testBuilder()
        .sqlQuery(query)
        .unOrdered()
        .baselineColumns("hash1", "hash2")
        .baselineValues(6356026919477319845L, 6356026919477319845L)
        .go();
    }

  }

}


text:
<?xml version="1.0"?>
<!--

-->
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Default</title>
    <media>
        <preview_image>media/test_default.jpg</preview_image>
    </media>
</theme>


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/description" />

    <EditText
        android:id="@+id/tfDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    </LinearLayout>

    <include layout = "@layout/for_indexz" />

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:measureWithLargestChild="true" >

        <include layout = "@layout/for_dialog_apply_ok_close" />

    </LinearLayout>

</LinearLayout>


text:
<!DOCTYPE html>

    <title>Blog</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.css" rel="stylesheet" type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css" rel="stylesheet" type="text/css">
    <link href="../styles/main.css" rel="stylesheet" type="text/css">

Tim Laing

                Home
                Projects
                Blog
                About

Time management

Time management can be a problem area for me as it can for many others. I have found that I am most effective in the morning and try and get most of my work done then. There was a bunch of interesting ideas and blogs in the time management section that I enjoyed reading.

 I enjoyed the idea of starting to create habits by just doing small things and letting these become the start of something bigger. The Pomodoro technique of working in small measurement time incriments will be a helpful way of doing this. I am really interested in keeping this up.

                 <iframe src="//embeds.vice.com/?playerId=YjMwNmI4YjU2MGM5ZWRjMzRmMjljMjc5&aid=munchies.vice.com/fuel-kron-gracie&vid=JzcTE5ODE6V1leGjpkZrgJpUyCHacyYr&embedCode=JzcTE5ODE6V1leGjpkZrgJpUyCHacyYr&cust_params=embdom%3Dhttps%3A%2F%2Fmunchies.vice.com%2Fen%2Fvideos%2Ffuel-kron-gracie%26topic%3D%26aid%3Dfuel-kron-gracie%26auth%3Dkron-gracie%26keywords%3Ddiet%2Cfuel%2Cjiu+jitsu%2Ckron+gracie%2CMMA%2Csports%2Cvegetarian%26country%3Den%26contentId%3DJzcTE5ODE6V1leGjpkZrgJpUyCHacyYr&ad_rule=1&description_url=https://munchies.vice.com/en/videos/fuel-kron-gracie&autoplay=1&share_url=https://munchies.vice.com/en/videos/fuel-kron-gracie" width="960px" height="563px" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>

 I have tried meditation and mindfulness before and I am keen to give it a go again. I find that as I get older it certainly resonates with me more. When I was younger I tried it and it didn't work at all.


text:
"""
The :mod:`websockets.protocol` module handles WebSocket control and data
frames as specified in `sections 4 to 8 of RFC 6455`_.

.. _sections 4 to 8 of RFC 6455: http://tools.ietf.org/html/rfc6455#section-4
"""

__all__ = ['WebSocketCommonProtocol']

import asyncio
import asyncio.queues
import codecs
import collections
import logging
import random
import struct

from .exceptions import InvalidState, PayloadTooBig, WebSocketProtocolError
from .framing import *
from .handshake import *

logger = logging.getLogger(__name__)

# A WebSocket connection goes through the following four states, in order:

CONNECTING, OPEN, CLOSING, CLOSED = range(4)

# In order to ensure consistency, the code always checks the current value of
# WebSocketCommonProtocol.state before assigning a new value and never yields
# between the check and the assignment.

class WebSocketCommonProtocol(asyncio.StreamReaderProtocol):
    """
    This class implements common parts of the WebSocket protocol.

    It assumes that the WebSocket connection is established. The handshake is
    managed in subclasses such as
    :class:`~websockets.server.WebSocketServerProtocol` and
    :class:`~websockets.client.WebSocketClientProtocol`.

    It runs a task that stores incoming data frames in a queue and deals with
    control frames automatically. It sends outgoing data frames and performs
    the closing handshake.

    The ``host``, ``port`` and ``secure`` parameters are simply stored as
    attributes for handlers that need them.

    The ``timeout`` parameter defines the maximum wait time in seconds for
    completing the closing handshake and, only on the client side, for
    terminating the TCP connection. :meth:`close()` will complete in at most
    this time on the server side and twice this time on the client side.

    The ``max_size`` parameter enforces the maximum size for incoming messages
    in bytes. The default value is 1MB. ``None`` disables the limit. If a
    message larger than the maximum size is received, :meth:`recv()` will
    return ``None`` and the connection will be closed with status code 1009.

    Once the handshake is complete, request and response HTTP headers are
    available:

    * as a MIME :class:`~email.message.Message` in the :attr:`request_headers`
      and :attr:`response_headers` attributes
    * as an iterable of (name, value) pairs in the :attr:`raw_request_headers`
      and :attr:`raw_response_headers` attributes

    If a subprotocol was negotiated, it's available in the :attr:`subprotocol`
    attribute.

    Once the connection is closed, the status code is available in the
    :attr:`close_code` attribute and the reason in :attr:`close_reason`.
    """

    # There are only two differences between the client-side and the server-
    # side behavior: masking the payload and closing the underlying TCP
    # connection. This class implements the server-side behavior by default.
    # To get the client-side behavior, set is_client = True.

    is_client = False
    state = OPEN

    def __init__(self, *,
                 host=None, port=None, secure=None,
                 timeout=10, max_size=2 ** 20, loop=None):
        self.host = host
        self.port = port
        self.secure = secure

        self.timeout = timeout
        self.max_size = max_size
        # Store a reference to loop to avoid relying on self._loop, a private
        # attribute of StreamReaderProtocol, inherited from FlowControlMixin.
        self.loop = loop

        stream_reader = asyncio.StreamReader(loop=loop)
        super().__init__(stream_reader, self.client_connected, loop)

        self.reader = None
        self.writer = None

        self.request_headers = None
        self.raw_request_headers = None
        self.response_headers = None
        self.raw_response_headers = None

        self.subprotocol = None

        # Code and reason must be set when the closing handshake completes.
        self.close_code = None
        self.close_reason = ''

        # Futures tracking steps in the connection's lifecycle.
        # Set to True when the opening handshake has completed properly.
        self.opening_handshake = asyncio.Future(loop=loop)
        # Set to True when the closing handshake has completed properly and to
        # False when the connection terminates abnormally.
        self.closing_handshake = asyncio.Future(loop=loop)
        # Set to None when the connection state becomes CLOSED.
        self.connection_closed = asyncio.Future(loop=loop)

        # Queue of received messages.
        self.messages = asyncio.queues.Queue(loop=loop)

        # Mapping of ping IDs to waiters, in chronological order.
        self.pings = collections.OrderedDict()

        # Task managing the connection, initalized in self.client_connected.
        self.worker = None

        # In a subclass implementing the opening handshake, the state will be
        # CONNECTING at this point.
        if self.state == OPEN:
            self.opening_handshake.set_result(True)

    @property
    def state_name(self):
        return ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'][self.state]

    # Public API

    @property
    def local_address(self):
        """
        Local address of the connection.

        The address is a ``(host, port)`` tuple or ``None`` if the connection
        hasn't been established yet.
        """
        if self.writer is None:
            return None
        return self.writer.get_extra_info('sockname')

    @property
    def remote_address(self):
        """
        Remote address of the connection.

        The address is a ``(host, port)`` tuple or ``None`` if the connection
        hasn't been established yet.
        """
        if self.writer is None:
            return None
        return self.writer.get_extra_info('peername')

    @property
    def open(self):
        """
        This property is ``True`` when the connection is usable.

        It may be used to handle disconnections gracefully.
        """
        return self.state == OPEN

    @asyncio.coroutine
    def close(self, code=1000, reason=''):
        """
        This coroutine performs the closing handshake.

        It waits for the other end to complete the handshake. It doesn't do
        anything once the connection is closed.

        It's usually safe to wrap this coroutine in :func:`~asyncio.async`
        since errors during connection termination aren't particularly useful.

        ``code`` must be an :class:`int` and ``reason`` a :class:`str`.
        """
        if self.state == OPEN:
            # 7.1.2. Start the WebSocket Closing Handshake
            # 7.1.3. The WebSocket Closing Handshake is Started
            frame_data = serialize_close(code, reason)
            yield from self.write_frame(OP_CLOSE, frame_data)

        # If the connection doesn't terminate within the timeout, break out of
        # the worker loop.
        try:
            yield from asyncio.wait_for(
                self.worker, self.timeout, loop=self.loop)
        except asyncio.TimeoutError:
            self.worker.cancel()

        # The worker should terminate quickly once it has been cancelled.
        yield from self.worker

    @asyncio.coroutine
    def recv(self):
        """
        This coroutine receives the next message.

        It returns a :class:`str` for a text frame and :class:`bytes` for a
        binary frame.

        When the end of the message stream is reached, or when a protocol
        error occurs, :meth:`recv` returns ``None``, indicating that the
        connection is closed.
        """
        # Return any available message
        try:
            return self.messages.get_nowait()
        except asyncio.queues.QueueEmpty:
            pass

        # Wait for a message until the connection is closed
        next_message = asyncio.async(self.messages.get(), loop=self.loop)
        try:
            done, pending = yield from asyncio.wait(
                [next_message, self.worker],
                loop=self.loop, return_when=asyncio.FIRST_COMPLETED)
        except asyncio.CancelledError:
            # Handle the Task.cancel()
            next_message.cancel()
            raise

        if next_message in done:
            return next_message.result()
        else:
            next_message.cancel()

    @asyncio.coroutine
    def send(self, data):
        """
        This coroutine sends a message.

        It sends a :class:`str` as a text frame and :class:`bytes` as a binary
        frame.

        It raises a :exc:`TypeError` for other inputs and
        :exc:`~websockets.exceptions.InvalidState` once the connection is
        closed.
        """
        if isinstance(data, str):
            opcode = 1
            data = data.encode('utf-8')
        elif isinstance(data, bytes):
            opcode = 2
        else:
            raise TypeError("data must be bytes or str")
        yield from self.write_frame(opcode, data)

    @asyncio.coroutine
    def ping(self, data=None):
        """
        This coroutine sends a ping.

        It returns a :class:`~asyncio.Future` which will be completed when the
        corresponding pong is received and which you may ignore if you don't
        want to wait.

        A ping may serve as a keepalive.
        """
        # Protect against duplicates if a payload is explicitly set.
        if data in self.pings:
            raise ValueError("Already waiting for a pong with the same data")
        # Generate a unique random payload otherwise.
        while data is None or data in self.pings:
            data = struct.pack('!I', random.getrandbits(32))

        self.pings[data] = asyncio.Future(loop=self.loop)
        yield from self.write_frame(OP_PING, data)
        return self.pings[data]

    @asyncio.coroutine
    def pong(self, data=b''):
        """
        This coroutine sends a pong.

        An unsolicited pong may serve as a unidirectional heartbeat.
        """
        yield from self.write_frame(OP_PONG, data)

    # Private methods - no guarantees.

    @asyncio.coroutine
    def run(self):
        # This coroutine guarantees that the connection is closed at exit.
        yield from self.opening_handshake
        while not self.closing_handshake.done():
            try:
                msg = yield from self.read_message()
                if msg is None:
                    break
                self.messages.put_nowait(msg)
            except asyncio.CancelledError:
                break
            except WebSocketProtocolError:
                yield from self.fail_connection(1002)
            except asyncio.IncompleteReadError:
                yield from self.fail_connection(1006)
            except UnicodeDecodeError:
                yield from self.fail_connection(1007)
            except PayloadTooBig:
                yield from self.fail_connection(1009)
            except Exception:
                yield from self.fail_connection(1011)
                raise
        yield from self.close_connection()

    @asyncio.coroutine
    def read_message(self):
        # Reassemble fragmented messages.
        frame = yield from self.read_data_frame(max_size=self.max_size)
        if frame is None:
            return
        if frame.opcode == OP_TEXT:
            text = True
        elif frame.opcode == OP_BINARY:
            text = False
        else:   # frame.opcode == OP_CONT
            raise WebSocketProtocolError("Unexpected opcode")

        # Shortcut for the common case - no fragmentation
        if frame.fin:
            return frame.data.decode('utf-8') if text else frame.data

        # 5.4. Fragmentation
        chunks = []
        max_size = self.max_size
        if text:
            decoder = codecs.getincrementaldecoder('utf-8')(errors='strict')
            if max_size is None:
                def append(frame):
                    nonlocal chunks
                    chunks.append(decoder.decode(frame.data, frame.fin))
            else:
                def append(frame):
                    nonlocal chunks, max_size
                    chunks.append(decoder.decode(frame.data, frame.fin))
                    max_size -= len(frame.data)
        else:
            if max_size is None:
                def append(frame):
                    nonlocal chunks
                    chunks.append(frame.data)
            else:
                def append(frame):
                    nonlocal chunks, max_size
                    chunks.append(frame.data)
                    max_size -= len(frame.data)
        append(frame)

        while not frame.fin:
            frame = yield from self.read_data_frame(max_size=max_size)
            if frame is None:
                raise WebSocketProtocolError("Incomplete fragmented message")
            if frame.opcode != OP_CONT:
                raise WebSocketProtocolError("Unexpected opcode")
            append(frame)

        return ('' if text else b'').join(chunks)

    @asyncio.coroutine
    def read_data_frame(self, max_size):
        # Deal with control frames automatically and return next data frame.
        # 6.2. Receiving Data
        while True:
            frame = yield from self.read_frame(max_size)
            # 5.5. Control Frames
            if frame.opcode == OP_CLOSE:
                # Make sure the close frame is valid before echoing it.
                code, reason = parse_close(frame.data)
                if self.state == OPEN:
                    # 7.1.3. The WebSocket Closing Handshake is Started
                    yield from self.write_frame(OP_CLOSE, frame.data)
                if not self.closing_handshake.done():
                    self.close_code, self.close_reason = code, reason
                    self.closing_handshake.set_result(True)
                return
            elif frame.opcode == OP_PING:
                # Answer pings.
                yield from self.pong(frame.data)
            elif frame.opcode == OP_PONG:
                # Do not acknowledge pings on unsolicited pongs.
                if frame.data in self.pings:
                    # Acknowledge all pings up to the one matching this pong.
                    ping_id = None
                    while ping_id != frame.data:
                        ping_id, waiter = self.pings.popitem(0)
                        if not waiter.cancelled():
                            waiter.set_result(None)
            # 5.6. Data Frames
            else:
                return frame

    @asyncio.coroutine
    def read_frame(self, max_size):
        is_masked = not self.is_client
        frame = yield from read_frame(
            self.reader.readexactly, is_masked, max_size=max_size)
        side = 'client' if self.is_client else 'server'
        logger.debug("%s << %s", side, frame)
        return frame

    @asyncio.coroutine
    def write_frame(self, opcode, data=b''):
        # This may happen if a user attempts to write on a closed connection.
        if self.state != OPEN:
            raise InvalidState("Cannot write to a WebSocket "
                               "in the {} state".format(self.state_name))
        # Make sure no other frame will be sent after a close frame. Do this
        # before yielding control to avoid sending more than one close frame.
        if opcode == OP_CLOSE:
            self.state = CLOSING
        frame = Frame(True, opcode, data)
        side = 'client' if self.is_client else 'server'
        logger.debug("%s >> %s", side, frame)
        is_masked = self.is_client
        write_frame(frame, self.writer.write, is_masked)
        try:
            # Handle flow control automatically.
            yield from self.writer.drain()
        except ConnectionResetError:
            # Terminate the connection if the socket died.
            yield from self.fail_connection(1006)

    @asyncio.coroutine
    def close_connection(self, force=False):
        # 7.1.1. Close the WebSocket Connection
        if self.state == CLOSED:
            return

        # Defensive assertion for protocol compliance.
        if self.state != CLOSING and not force:             # pragma: no cover
            raise InvalidState("Cannot close a WebSocket connection "
                               "in the {} state".format(self.state_name))

        if self.is_client and not force:
            try:
                yield from asyncio.wait_for(
                    self.connection_closed, self.timeout, loop=self.loop)
            except (asyncio.CancelledError, asyncio.TimeoutError):
                pass

            if self.state == CLOSED:
                return

        # Attempt to terminate the TCP connection properly.
        # If the socket is already closed, this may crash.
        try:
            if self.writer.can_write_eof():
                self.writer.write_eof()
        except Exception:                                   # pragma: no cover
            pass

        self.writer.close()

        try:
            yield from asyncio.wait_for(
                self.connection_closed, self.timeout, loop=self.loop)
        except (asyncio.CancelledError, asyncio.TimeoutError):
            pass

    @asyncio.coroutine
    def fail_connection(self, code=1011, reason=''):
        # 7.1.7. Fail the WebSocket Connection
        logger.info("Failing the WebSocket connection: %d %s", code, reason)
        if self.state == OPEN:
            if code == 1006:
                # Don't send a close frame is the connection is broken. Set
                # the state to CLOSING to allow close_connection to proceed.
                self.state = CLOSING
            else:
                frame_data = serialize_close(code, reason)
                yield from self.write_frame(OP_CLOSE, frame_data)
        if not self.closing_handshake.done():
            self.close_code, self.close_reason = code, reason
            self.closing_handshake.set_result(False)
        yield from self.close_connection()

    # asyncio StreamReaderProtocol methods

    def client_connected(self, reader, writer):
        self.reader = reader
        self.writer = writer
        # Start the task that handles incoming messages.
        self.worker = asyncio.async(self.run(), loop=self.loop)

    def connection_lost(self, exc):
        # 7.1.4. The WebSocket Connection is Closed
        self.state = CLOSED
        if not self.closing_handshake.done():
            self.close_code, self.close_reason = 1006, ''
            self.closing_handshake.set_result(False)
        if not self.connection_closed.done():
            self.connection_closed.set_result(None)
        super().connection_lost(exc)


text:
"""
Created on Mon Feb 20 03:51:37 2017

@author: Aretas
"""

#creates a config file(s) for Vina; accepts enzyme PDB as input
#uses default settings
#accepts and xyz coordinate and xyz box size as input
#default search parameters work with all bornyl synthases except for 1n21
#Results folder -lo
#lo1 flag is for specifting folder directory with the ligand PDBQT library
#ligands must be in name_number format e.g. skeleton_12.pdbqt
#Please use with enyzme library as PWD! Otherwise the script won't work

import os
import re
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file',
    help='choose the PDBQT protein file', required=True)
parser.add_argument('-xc', '--x_center',
    help='choose x center coordinate', default=45)
parser.add_argument('-yc', '--y_center',
    help='choose the y center coordinate:', default=50)
parser.add_argument('-zc', '--z_center',
    help='choose the z center coordinate:', default=50)
parser.add_argument('-xs', '--x_size',
    help='choose the x size', default=20)
parser.add_argument('-ys', '--y_size',
    help='choose the y size', default=20)
parser.add_argument('-zs', '--z_size',
    help='choose the z size', default=16)
parser.add_argument('-lo', '--location',
    help='choose the folder to save the file to', required=True)
parser.add_argument('-lo1', '--location1',
    help='choose the folder of the ligand PDBQT library', default="Linear_structures/PDBQT_EMM")
parser.add_argument('-ch', '--chain',
    help='specify the enzyme chain (A/B) to perform MD with', default="A")

parser.add_argument('-m', '--num_modes',
    help='choose num_modes', default=15)
parser.add_argument('-e', '--exhaustiveness',
    help='choose exhaustiveness', default=15)
parser.add_argument('-s', '--seed',
    help='choose seed value', default=314159)

args = parser.parse_args()
x_center = float(args.x_center)
y_center = float(args.y_center)
z_center = float(args.z_center)
x_size = float(args.x_size)
y_size = float(args.y_size)
z_size = float(args.z_size)
location = args.location    #MD results folder
location1 = args.location1  #Ligand library location
chain = args.chain

file_name = str(args.file)
pdb_location = os.path.realpath(args.file)
MD_folder = os.path.realpath(args.location)
if not os.path.exists(MD_folder):
    os.makedirs(MD_folder)
ligand_location = os.path.realpath(args.location1)

#print (pdb_location, MD_folder, ligand_location)
m_obj = re.search(r'(.*)_NS(.*).pdbqt', args.file)
if m_obj:
    protein_name = m_obj.groups()[0]
    misc_name = m_obj.groups()[1]
    protein_name = protein_name + "_NS" + misc_name

folder_name = protein_name + "_{0}_final1".format(chain)
file_path = MD_folder + "/" + folder_name + "/"
file_path3 = MD_folder + "/"
file_path4 = ligand_location + "/"

if not os.path.exists(file_path):
    os.makedirs(file_path)

for filename in os.listdir(file_path4):
    scaffold_number = re.search(r'(.*)_(.*)_min.pdbqt',filename)
    if scaffold_number:
        scaffold_number_hit = scaffold_number.groups()[1]
        scaffold_name = scaffold_number.groups()[0]
        completeName = os.path.join(file_path,"config_vina_{0}_{2}_{1}.txt"
            .format(protein_name, scaffold_number_hit, chain))

        with open(completeName, "w") as file2:

            file2.write("receptor = {0}{1}.pdbqt\n".format(file_path3, protein_name))
            file2.write("ligand = {1}{2}_{0}_min.pdbqt\n".format(scaffold_number_hit, file_path4, scaffold_name))
            file2.write("\n")
            file2.write("out = {2}{0}_{3}_{4}_{1}_vina.pdbqt\n".format(
                protein_name,scaffold_number_hit,file_path, chain, scaffold_name))
            file2.write("\n")
            file2.write("center_x = {0}\n".format(x_center))
            file2.write("center_y = {0}\n".format(y_center))
            file2.write("center_z = {0}\n".format(z_center))
            file2.write("\n")
            file2.write("size_x = {0}\n".format(x_size))
            file2.write("size_y = {0}\n".format(y_size))
            file2.write("size_z = {0}\n".format(z_size))
            file2.write("\n")
            file2.write("exhaustiveness = {0}\n".format(args.exhaustiveness))
            file2.write("\n")
            file2.write("num_modes = {0}\n".format(args.num_modes))
            file2.write("\n")
            file2.write("seed = {0}\n".format(args.seed))

print("DONE")


text:
//=============================================================================
/**
 *  @file    Message_Queue_Test.cpp
 *
 *    This is:
 *    0) a test that ensures key ACE_Message_Queue features are
 *       working properly, including timeouts and priorities
 *    1) a simple test of the ACE_Message_Queue that illustrates how to
 *       use the forward and reverse iterators
 *    2) a simple performance measurement test for both single-threaded
 *       (null synch), thread-safe ACE_Message_Queues, and
 *       ACE_Message_Queue_Vx, which wraps VxWorks message queues
 *    3) a test/usage example of ACE_Message_Queue_Vx
 *    4) a test of the message counting in a message queue under load.
 *
 *  @author Irfan Pyarali <irfan@cs.wustl.edu>
 *  @author David L. Levine <levine@cs.wustl.edu>
 *  @author and Douglas C. Schmidt <schmidt@vanderbilt.edu>
 */
//=============================================================================

#include "test_config.h"
#include "ace/Atomic_Op.h"
#include "ace/Thread_Manager.h"
#include "ace/Message_Queue.h"
#include "ace/Message_Queue_NT.h"
#include "ace/Message_Queue_Vx.h"
#include "ace/Synch_Traits.h"
#include "ace/Null_Mutex.h"
#include "ace/Null_Condition.h"
#include "ace/High_Res_Timer.h"
#include "ace/Task.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/OS_NS_unistd.h"

const ACE_TCHAR usage[] = ACE_TEXT ("usage: Message_Queue_Test <number of messages>\n");

typedef ACE_Message_Queue<ACE_NULL_SYNCH> QUEUE;
typedef ACE_Message_Queue_Iterator<ACE_NULL_SYNCH> ITERATOR;
typedef ACE_Message_Queue_Reverse_Iterator<ACE_NULL_SYNCH> REVERSE_ITERATOR;

#if defined (ACE_HAS_WINCE)
static const int MESSAGE_FACTOR = 10000;
#else
static const int MESSAGE_FACTOR = 100000;
#endif
static const int MAX_MESSAGES = 10000;
static const int MAX_MESSAGE_SIZE = 32;
static const char test_message[] = "ACE_Message_Queue Test Message";

static int max_messages = MAX_MESSAGES;

// Dynamically allocate to avoid a static.
static ACE_High_Res_Timer *timer = 0;

#if defined (ACE_HAS_THREADS)
typedef ACE_Message_Queue<ACE_MT_SYNCH> SYNCH_QUEUE;

struct Queue_Wrapper
{
  // = TITLE
  //     Container for data passed to sender and receiver in
  //     performance test.
  //
  // = DESCRIPTION
  //     For use in multithreaded performance test.

  ACE_Message_Queue_Base *q_;
  // The message queue.

  ACE_Message_Block **send_block_;
  // Pointer to messages blocks for sender to send to reciever.

  Queue_Wrapper (void)
    : q_ (0), send_block_ (0)
  {
  }
  // Default constructor.
};

// For the message counting test, there are two tasks, producer and consumer.
// Each will spawn a number of threads, and the two tasks share a queue.
class Counting_Test_Producer : public ACE_Task<ACE_MT_SYNCH>
{
public:
  Counting_Test_Producer (ACE_Message_Queue<ACE_MT_SYNCH> *queue)
    : ACE_Task<ACE_MT_SYNCH> (0, queue), sequence_ (0), produced_ (0) {}
  virtual int svc (void);

  ACE_Atomic_Op<ACE_Thread_Mutex, long> sequence_;
  ACE_Atomic_Op<ACE_Thread_Mutex, long> produced_;
};

class Counting_Test_Consumer : public ACE_Task<ACE_MT_SYNCH>
{
public:
  Counting_Test_Consumer (ACE_Message_Queue<ACE_MT_SYNCH> *queue)
    : ACE_Task<ACE_MT_SYNCH> (0, queue), consumed_ (0) {}
  virtual int svc (void);

  ACE_Atomic_Op<ACE_Thread_Mutex, long> consumed_;
};

int
Counting_Test_Producer::svc (void)
{
  // Going to produce a lot of blocks. Since we don't necessarily want them
  // all consumed, there's no arrangement with the consumer to be sure that
  // the same number produced will be consumed; the test check will compare
  // the number produced, consumed, and remaining to be sure it ends up
  // correct.
  // Also, to be sure there's not just 1 producer and 1 consumer pinging
  // back and forth, make the producers randomly delay between blocks.
  ACE_OS::srand (static_cast<unsigned int> (ACE_OS::time ()));
  int multiple = ACE_OS::rand () % 10;
  int delay_ms = (ACE_OS::rand () % 10) / 2;
  // The delay usually causes the test to time out in the automated
  // regression testing. I just left it here in case it's needed someday.
  delay_ms = 0;
  long count = MESSAGE_FACTOR * (multiple ? multiple : 1);
  long produced = 0;
  // Some of the threads enqueue single blocks, others sequences.
  long lsequence = ++(this->sequence_);
  int seq = static_cast<int> (lsequence);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) Producer will enqueue %B blocks in seq of %d, ")
              ACE_TEXT ("%d msec delay\n"),
              (size_t)count,
              seq,
              delay_ms));

  ACE_Message_Block *first = 0, *prev = 0, *b = 0;
  ACE_Time_Value delay (0, delay_ms);
  ACE_Time_Value timeout (10);
  while (produced < count)
    {
      ACE_NEW_NORETURN (b, ACE_Message_Block (1));
      if (b == 0)
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Producer out of memory\n")));
          break;
        }
      first = b;
      prev = first;
      for (int s = 1; s < seq; ++s)
        {
          ACE_NEW_NORETURN (b, ACE_Message_Block (1));
          if (b == 0)
            break;
          prev->next (b);
          b->prev (prev);
          prev = b;
        }
      if (b == 0)
        {
          if (first != b)
            {
              while (first->next () != 0)
                {
                  b = first->next ();
                  first->release ();
                  first = b;
                }
              first->release ();
            }
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Producer out of memory\n")));
          break;
        }
      // To be sure we can keep going on slow or completed consumers, but not
      // delay excessively if the consumers have stopped, limit the time
      // spent waiting to 10 seconds.
      ACE_Time_Value block = ACE_OS::gettimeofday ();
      block += timeout;
      if (this->putq (first, &block) == -1)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) Producer cannot putq; giving up\n")));
          while (first->next () != 0)
            {
              b = first->next ();
              first->release ();
              first = b;
            }
          first->release ();
          break;
        }
      produced += seq;
      if (delay_ms)
        ACE_OS::sleep (delay);
    }
  this->produced_ += produced;
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Producer done\n")));
  return 0;
}

int
Counting_Test_Consumer::svc (void)
{
  // Consume lots of blocks and release them. To mimic a thread with work
  // to do, put a small random delay between dequeuing the blocks. Consume
  // a calculated number of blocks then stop; the test checker will determine
  // if the number consumed plus the number remaining is correct for the
  // number produced.
  unsigned int seed = static_cast<unsigned int> (ACE_OS::time ());

  int multiple = ACE_OS::rand_r (&seed) % 10;
  int delay_ms = ACE_OS::rand_r (&seed) % 10;
  // The delay usually causes the test to time out in the automated
  // regression testing. I just left it here in case it's needed someday.
  delay_ms = 0;
  long count = MESSAGE_FACTOR * (multiple ? multiple : 1);
  long consumed = 0;
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) Consumer will dequeue %B blocks, ")
              ACE_TEXT ("%d msec delay\n"),
              (size_t)count,
              delay_ms));
  ACE_Message_Block *b = 0;
  ACE_Time_Value delay (0, delay_ms);
  ACE_Time_Value timeout (2);
  while (consumed < count)
    {
      // To be sure we can wait in the case of an empty queue, but not
      // delay excessively if the producers have stopped, limit the time
      // spent waiting to 2 seconds.
      ACE_Time_Value block = ACE_OS::gettimeofday ();
      block += timeout;
      if (this->getq (b, &block) == -1)
        {
          if (errno == EWOULDBLOCK)
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Consumer timed out\n")));
          else
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%t) Consumer %p\n"),
                        ACE_TEXT ("getq")));
          break;
        }
      ++consumed;
      b->release ();
      if (delay_ms)
        ACE_OS::sleep (delay);
    }
  this->consumed_ += consumed;
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Consumer done\n")));
  return 0;
}

static int
counting_test (void)
{
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Starting counting test\n")));

  ACE_Message_Queue<ACE_MT_SYNCH> q (2 * 1024 * 1024);  // 2MB high water
  Counting_Test_Producer p (&q);
  Counting_Test_Consumer c (&q);
  // Activate consumers first; if the producers fail to start, consumers will
  // stop quicker.
  if (c.activate (THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, 5) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Consumers %p\n"),
                       ACE_TEXT ("activate")),
                      -1);
  if (p.activate (THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, 5) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Producers %p\n"),
                  ACE_TEXT ("activate")));
      c.wait ();
      return -1;
    }
  // Producers and consumers are both running; wait for them then
  // check the results.
  p.wait ();
  c.wait ();
  // This compare relies on the flush() method counting blocks as it
  // walks the chain releasing them, and doesn't rely on the count.
  int status = 0;
  long q_count = static_cast<long> (q.message_count ());
  long remaining = q.flush ();
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Queue message_count is %b; %b flushed\n"),
              (ssize_t)q_count,
              (ssize_t)remaining));
  if (q_count != remaining)
    {
      status = -1;
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("message_count and flushed should be equal!\n")));
    }
  long expected = p.produced_.value () - c.consumed_.value ();
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Produced %b, consumed %b; diff %b\n"),
              (ssize_t)p.produced_.value (),
              (ssize_t)c.consumed_.value (),
              (ssize_t)expected));
  if (expected != remaining)
    {
      status = -1;
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Producer-consumer diff is %b; should be %b\n"),
                  (ssize_t)expected,
                  (ssize_t)remaining));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Ending counting test\n")));
  return status;
}

#endif /* ACE_HAS_THREADS */

static int
iterator_test (void)
{
  const int ITERATIONS = 5;
  ACE_TCHAR buffer[ITERATIONS][BUFSIZ];
  // Use queue size from of 32 Kb (more if using wide-char), instead of the
  // default of 16 Kb (defined by ACE_Message_Queue_Base::DEFAULT_HWM),
  // so that the test runs on machines with 8Kb pagesizes.

  //  QUEUE queue (32 * 1024 * sizeof (ACE_TCHAR));
  QUEUE queue (sizeof(buffer));

  int i;

  for (i = 0; i < ITERATIONS; i++)
    {
      ACE_OS::snprintf (buffer[i], BUFSIZ, ACE_TEXT ("%d"), i + 1);

      ACE_Message_Block *entry = 0;
      ACE_NEW_RETURN (entry,
                      ACE_Message_Block ((char *) buffer[i],
                                         sizeof buffer[i]),
                      -1);

      if (queue.is_full ())
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("QUEUE:: the message queue is full on iteration %u!\n"),
                           i + 1),
                          -1);

      if (queue.enqueue (entry) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("QUEUE::enqueue\n")),
                          -1);
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nForward Iterations\n")));
  {
    ITERATOR iterator (queue);

    for (ACE_Message_Block *entry = 0;
         iterator.next (entry) != 0;
         iterator.advance ())
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s\n"),
                  entry->base ()));
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nReverse Iterations\n")));
  {
    REVERSE_ITERATOR iterator (queue);

    for (ACE_Message_Block *entry = 0;
         iterator.next (entry) != 0;
         iterator.advance ())
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s\n"),
                  entry->base ()));
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nForward Iterations\n")));
  {
    QUEUE::ITERATOR iterator (queue);

    for (ACE_Message_Block *entry = 0;
         iterator.next (entry) != 0;
         iterator.advance ())
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s\n"),
                  entry->base ()));
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nReverse Iterations\n")));
  {
    QUEUE::REVERSE_ITERATOR iterator (queue);

    for (ACE_Message_Block *entry = 0;
         iterator.next (entry) != 0;
         iterator.advance ())
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s\n"),
                  entry->base ()));
  }

  return 0;
}

#if defined (ACE_HAS_THREADS)

static int
chained_block_test (void)
{

  QUEUE q;
  const char * s = "123456789";      // Will be length 10 when copied to block
  const size_t slen = 10;
  const size_t num_blks = 10;
  ACE_Message_Block b[num_blks];
  size_t i;
  int status = 0;

  for (i = 0; i < num_blks; ++i)
    {
      b[i].init (slen);
      b[i].copy (s);
    }

  // Test enqueueing single and chained blocks and be sure they end up with
  // the proper enqueued block count and sizes. Then be sure they are dequeued
  // in the proper order.
  b[0].next (&b[1]);
  b[1].next (&b[2]);
  // b[3] and b[4] are unchained.
  b[5].next (&b[6]);
  b[6].next (&b[7]);
  b[7].next (&b[8]);
  // b[9] is unchained
  q.enqueue_tail (&b[3]);
  q.enqueue_tail (&b[4]);
  int num = q.enqueue_head (&b[0]);
  if (num != 5)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 5; has %d\n"),
                  num));
      status = -1;
    }
  num = q.enqueue_tail (&b[5]);
  if (num != 9)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 9; has %d\n"),
                  num));
      status = -1;
    }
  num = q.enqueue_tail (&b[9]);
  if (num != 10)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Chained enqueue expected 10; has %d\n"),
                  num));
      status = -1;
    }
  size_t msgs, bytes;
  msgs = q.message_count ();
  bytes = q.message_bytes ();
  if (msgs != 10 || bytes != 100)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Chained enqueue totals: %d msgs, %d bytes; ")
                  ACE_TEXT ("should be 10 msgs, 100 bytes\n"),
                  (int)msgs, (int)bytes));
      status = -1;
    }

  // Now see if we can dequeue them, checking the order.
  ACE_Time_Value nowait (ACE_OS::gettimeofday ());
  ACE_Message_Block *bp;
  int qstat;
  for (i = 0; i < num_blks; ++i)
    {
      qstat = q.dequeue_head (bp, &nowait);
      if (qstat == -1)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Checking chained blocks, pass %d: %p\n"),
                      (int)i, ACE_TEXT ("dequeue_head")));
          status = -1;
        }
      else if (bp != &b[i])
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Checking chained blocks, pass %d: ")
                      ACE_TEXT ("block out of order\n"),
                      (int)i));
          status = -1;
        }
    }

  if (status == 0)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Chained block test OK\n")));
  return status;
}

static int
single_thread_performance_test (int queue_type = 0)
{
  const char test_message[] =
    "ACE_Message_Queue Test Message";
  const ACE_TCHAR *message =
    ACE_TEXT ("ACE_Message_Queue<ACE_NULL_SYNCH>, single thread");
  int i = 0;

  // Create a message queue.
  ACE_Message_Queue_Base *msgq = 0;

  if (queue_type == 0)
    ACE_NEW_RETURN (msgq,
                    QUEUE,
                    -1);
#if defined (ACE_VXWORKS)
  else
    {
      ACE_NEW_RETURN (msgq,
                      ACE_Message_Queue_Vx (max_messages,
                                            MAX_MESSAGE_SIZE),
                      -1);
      message = "ACE_Message_Queue_Vx, single thread test";
    }
#elif defined (ACE_WIN32) && defined (ACE_HAS_WIN32_OVERLAPPED_IO)
  else
    {
      ACE_NEW_RETURN (msgq,
                      ACE_Message_Queue_NT,
                      -1);
      message = ACE_TEXT ("ACE_Message_Queue_NT, single thread test");
    }
#endif /* ACE_VXWORKS */

  // Create the messages.  Allocate off the heap in case messages
  // is large relative to the amount of stack space available.
  ACE_Message_Block **send_block = 0;
  ACE_NEW_RETURN (send_block,
                  ACE_Message_Block *[max_messages],
                  -1);

  for (i = 0; i < max_messages; ++i)
    ACE_NEW_RETURN (send_block[i],
                    ACE_Message_Block (test_message,
                                       MAX_MESSAGE_SIZE),
                    -1);

  ACE_Message_Block **receive_block_p = 0;
  ACE_NEW_RETURN (receive_block_p,
                  ACE_Message_Block *[max_messages],
                  -1);

#if defined (ACE_VXWORKS)
  // Set up blocks to receive the messages.  Allocate these off the
  // heap in case messages is large relative to the amount of
  // stack space available.
  ACE_Message_Block *receive_block = 0;
  ACE_NEW_RETURN (receive_block,
                  ACE_Message_Block[max_messages],
                  -1);

  for (i = 0; i < max_messages; ++i)
    {
      receive_block[i].init (MAX_MESSAGE_SIZE);

      // For VxWorks Message Queues, the receive block pointer must be
      // assigned.  It will be used by dequeue_head ().
      receive_block_p[i] = &receive_block[i];
    }
#endif /* ACE_VXWORKS */

  timer->start ();

  // Send/receive the messages.
  for (i = 0; i < max_messages; ++i)
    {
      if (msgq->enqueue_tail (send_block[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("enqueue")),
                          -1);

      if (msgq->dequeue_head (receive_block_p[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("dequeue_head")),
                          -1);
    }

  timer->stop ();

  ACE_Time_Value tv;
  timer->elapsed_time (tv);
  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("%s: %u messages took %u msec (%f msec/message)\n"),
              message,
              max_messages,
              tv.msec (),
              (double) tv.msec () / max_messages));
  timer->reset ();

  delete [] receive_block_p;
#if defined (ACE_VXWORKS)
  delete [] receive_block;
#endif /* ACE_VXWORKS */

  for (i = 0; i < max_messages; ++i)
    delete send_block[i];
  delete [] send_block;
  delete msgq;

  return 0;
}

static void *
receiver (void *arg)
{
  Queue_Wrapper *queue_wrapper =
    reinterpret_cast<Queue_Wrapper *> (arg);
  int i;

  ACE_Message_Block **receive_block_p = 0;
  ACE_NEW_RETURN (receive_block_p,
                  ACE_Message_Block *[max_messages],
                  (void *) -1);

#if defined (ACE_VXWORKS)
  // Set up blocks to receive the messages.  Allocate these off the
  // heap in case messages is large relative to the amount of stack
  // space available.
  ACE_Message_Block *receive_block;
  ACE_NEW_RETURN (receive_block,
                  ACE_Message_Block[max_messages],
                  (void *) -1);

  for (i = 0; i < max_messages; ++i)
    {
      receive_block[i].init (MAX_MESSAGE_SIZE);

      // For VxWorks Message Queues, the receive block pointer must be
      // assigned.  It will be used by <dequeue_head>.
      receive_block_p[i] = &receive_block[i];
    }
#endif /* ACE_VXWORKS */

  for (i = 0; i < max_messages; ++i)
    if (queue_wrapper->q_->dequeue_head (receive_block_p[i]) == -1)
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%p\n"),
                         ACE_TEXT ("dequeue_head")),
                        0);
  timer->stop ();

  delete [] receive_block_p;
#if defined (ACE_VXWORKS)
  delete [] receive_block;
#endif /* ACE_VXWORKS */

  return 0;
}

static void *
sender (void *arg)
{
  Queue_Wrapper *queue_wrapper =
    reinterpret_cast<Queue_Wrapper *> (arg);
  int i;

  timer->start ();

  // Send the messages.
  for (i = 0; i < max_messages; ++i)
    if (queue_wrapper->q_->
        enqueue_tail (queue_wrapper->send_block_[i]) == -1)
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%p\n"),
                         ACE_TEXT ("enqueue")),
                        0);
  return 0;
}

static
int
performance_test (int queue_type = 0)
{
  Queue_Wrapper queue_wrapper;
  const ACE_TCHAR *message =
    ACE_TEXT ("ACE_Message_Queue<ACE_SYNCH>");
  int i = 0;

  // Create the messages.  Allocate off the heap in case messages is
  // large relative to the amount of stack space available.  Allocate
  // it here instead of in the sender, so that we can delete it after
  // the _receiver_ is done.
  ACE_Message_Block **send_block = 0;
  ACE_NEW_RETURN (send_block,
                  ACE_Message_Block *[max_messages],
                  -1);

  for (i = 0; i < max_messages; ++i)
    ACE_NEW_RETURN (send_block[i],
                    ACE_Message_Block (test_message,
                                       MAX_MESSAGE_SIZE),
                    -1);

  queue_wrapper.send_block_ = send_block;

  if (queue_type == 0)
    ACE_NEW_RETURN (queue_wrapper.q_,
                    SYNCH_QUEUE,
                    -1);
#if defined (ACE_VXWORKS)
  else
    {
      ACE_NEW_RETURN (queue_wrapper.q_,
                      ACE_Message_Queue_Vx (max_messages,
                                            MAX_MESSAGE_SIZE),
                      -1);
      message = "ACE_Message_Queue_Vx";
    }
#elif defined (ACE_WIN32) && defined (ACE_HAS_WIN32_OVERLAPPED_IO)
  else
    {
      ACE_NEW_RETURN (queue_wrapper.q_,
                      ACE_Message_Queue_NT,
                      -1);
      message = ACE_TEXT ("ACE_Message_Queue_NT");
    }
#endif /* ACE_VXWORKS */

  if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) sender,
                                              &queue_wrapper,
                                              THR_BOUND) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("spawning sender thread")),
                      -1);

  if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) receiver,
                                              &queue_wrapper,
                                              THR_BOUND) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("spawning receiver thread")),
                      -1);

  ACE_Thread_Manager::instance ()->wait ();
  ACE_Time_Value tv;
  timer->elapsed_time (tv);
  ACE_DEBUG ((LM_INFO, ACE_TEXT ("%s: %u messages took %u msec (%f msec/message)\n"),
              message,
              max_messages,
              tv.msec (),
              (double) tv.msec () / max_messages));
  timer->reset ();

  delete queue_wrapper.q_;
  queue_wrapper.q_ = 0;

  for (i = 0; i < max_messages; ++i)
    delete send_block[i];
  delete [] send_block;

  return 0;
}

// Ensure that the timedout dequeue_head() sets errno code properly.

static int
timeout_test (void)
{
  SYNCH_QUEUE mq;
  int status = 0;

  if (!mq.is_empty ())
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("New queue is not empty!\n")));
      status = 1;
    }
  else
    {
      ACE_Message_Block *b;
      ACE_Time_Value tv (ACE_OS::gettimeofday ());   // Now

      if (mq.dequeue_head (b, &tv) != -1)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Dequeued from empty queue!\n")));
          status = 1;
        }
      else if (errno != EWOULDBLOCK)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("Dequeue timeout should be EWOULDBLOCK, got")));
          status = 1;
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("Timed dequeue test: OK\n")));
          status = 0;     // All is well
        }
    }

  return status;
}
#endif /* ACE_HAS_THREADS */

// Check to make sure that dequeue_prio() respects FIFO ordering.
// @@ At some point, this function should be enhanced to do a more
// thorough check...

static int
prio_test (void)
{
  const char S1[] = "first";
  const char S2[] = "second";
  const int PRIORITY = 50;
  QUEUE mq;
  int status;

  ACE_Message_Block mb1 (S1, sizeof S1, PRIORITY);
  ACE_Message_Block mb2 (S2, sizeof S2, PRIORITY);

  mq.enqueue_prio (&mb1);
  mq.enqueue_prio (&mb2);

  ACE_Message_Block *mb1p = 0;
  ACE_Message_Block *mb2p = 0;

  mq.dequeue_prio (mb1p);
  mq.dequeue_prio (mb2p);

  ACE_DEBUG ((LM_DEBUG, "message 1 = %C\nmessage 2 = %C\n",
              mb1p->rd_ptr (),
              mb2p->rd_ptr ()));

  if (ACE_OS_String::strcmp (mb1p->rd_ptr (), S1) == 0
      && ACE_OS_String::strcmp (mb2p->rd_ptr (), S2) == 0)
    status = 0;
  else
    status = 1;

  return status;
}

static int
close_test (void)
{
  int status = 0;

  int flushed_messages;

  QUEUE mq1;
  flushed_messages = mq1.close ();

  if (flushed_messages != 0)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Closing queue should flush 0 messages, close() reports - %d\n"),
                  flushed_messages ));
      status = 1;
      return status;
    }

  // There was a bug that return previous queue state instead of
  // number of flushed messages. Thus, insert 2 messages != ACTIVATE
  // queue state
  ACE_Message_Block *pMB1;
  ACE_Message_Block *pMB2;
  ACE_NEW_NORETURN (pMB1, ACE_Message_Block (1));
  ACE_NEW_NORETURN (pMB2, ACE_Message_Block (1));
  QUEUE mq2;
  mq2.enqueue_head (pMB1);
  mq2.enqueue_head (pMB2);
  flushed_messages = mq2.close ();

  if (flushed_messages != 2)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Closing queue should flush 2 messages, close() reports - %d\n"),
                  flushed_messages ));
      status = 1;
      return status;
    }
  return status;
}

int
run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("Message_Queue_Test"));

  if (argc == 2)
    {
      if (!ACE_OS::strcmp (argv[1], ACE_TEXT ("-?")))
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%s/n"),
                      usage));
        }
      else
        {
          max_messages = ACE_OS::atoi (argv[1]);
        }
    }

  int status = prio_test ();

  // The iterator test occasionally causes a page fault or a hang on
  // VxWorks.
  if (status == 0)
    status = iterator_test ();

  ACE_NEW_RETURN (timer,
                  ACE_High_Res_Timer,
                  -1);

  if (status == 0)
    status = close_test ();

#if defined (ACE_HAS_THREADS)
  if (status == 0)
    status = timeout_test ();

  if (status == 0)
    status = chained_block_test ();

  if (status == 0)
    status = single_thread_performance_test ();

# if defined (ACE_VXWORKS) || defined (ACE_HAS_WIN32_OVERLAPPED_IO)
  // Test ACE_Message_Queue_Vx. or ACE_Message_Queue_NT
  if (status == 0)
    status = single_thread_performance_test (1);
# endif /* ACE_VXWORKS */

  if (status == 0)
    status = performance_test ();

# if defined (ACE_VXWORKS) || defined (ACE_HAS_WIN32_OVERLAPPED_IO)
  // Test ACE_Message_Queue_Vx or ACE_Message_Queue_NT
  if (status == 0)
    status = performance_test (1);
# endif /* ACE_VXWORKS */

  if (counting_test () != 0)
    status = -1;
#endif /* ACE_HAS_THREADS */

  if (status != 0)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("%p\n"),
                ACE_TEXT ("test failed")));
  delete timer;
  timer = 0;

  ACE_END_TEST;
  return status;
}


text:
require 'spec_helper'

describe Shop do

  let(:shop) { FactoryGirl.create(:shop) }

  context '#create' do

    it 'should create webhook via api' do
      stub = stub_request(:post, "test-store.myponty.com/api/webhooks.json")
      shop
      stub.should have_been_requested
    end

  end

end


text:
        Regression test for http://bugs.webkit.org/show_bug.cgi?id=15169
        Freeze on Google maps using the "Dig a hole" tool.

        This tests that the empty string property of a CSSStyleDeclaration is
        undefined and that retrieving it does not cause a hang.

FAIL (timed out or did not finish)


text:
require 'spec_helper'

describe Expressr::Renderer do
  describe '.get_renderer' do
    it 'returns a renderer' do
      described_class.get_renderer.should be_a(Expressr::Renderers::Base)
    end
  end

  it 'returns a Haml renderer' do
    Expressr::App.settings['view engine'] = 'haml'
    described_class.get_renderer.should be_a(Expressr::Renderers::Haml)
  end

  it 'returns a Slim renderer' do
    Expressr::App.settings['view engine'] = 'slim'
    described_class.get_renderer.should be_a(Expressr::Renderers::Slim)
  end
end


text:
package registry

import (
	"testing"

	"github.com/coreos/go-etcd/etcd"
	"github.com/mailgun/scroll/vulcan/middleware"
	. "gopkg.in/check.v1"
)

func TestLeaderRegistry(t *testing.T) {
	TestingT(t)
}

type LeaderSuite struct {
	client              *etcd.Client
	registry            *LeaderRegistry
	masterRegistration  *AppRegistration
	slaveRegistration   *AppRegistration
	handlerRegistration *HandlerRegistration
}

var _ = Suite(&LeaderSuite{})

func (s *LeaderSuite) SetUpSuite(c *C) {
	machines := []string{"http://127.0.0.1:4001"}
	s.client = etcd.NewClient(machines)
	s.registry = NewLeaderRegistry("customkey", "groupid", 15)
	s.masterRegistration = &AppRegistration{Name: "name", Host: "master", Port: 12345}
	s.slaveRegistration = &AppRegistration{Name: "name", Host: "slave", Port: 67890}
	s.handlerRegistration = &HandlerRegistration{
		Name:        "name",
		Host:        "host",
		Path:        "/path/to/server",
		Methods:     []string{"PUT"},
		Middlewares: []middleware.Middleware{middleware.Middleware{Type: "test", ID: "id", Spec: "hi"}},
	}
}

func (s *LeaderSuite) SetUpTest(c *C) {
	s.client.Delete("customkey", true)
}

func (s *LeaderSuite) TestRegisterAppCreatesBackend(c *C) {
	_ = s.registry.RegisterApp(s.masterRegistration)
	backend, err := s.client.Get("customkey/backends/name/backend", false, false)

	c.Assert(err, IsNil)
	c.Assert(backend.Node.Value, Equals, `{"Type":"http"}`)
	c.Assert(backend.Node.TTL, Equals, int64(0))
}

func (s *LeaderSuite) TestMasterServerRegistration(c *C) {
	_ = s.registry.RegisterApp(s.masterRegistration)

	server, err := s.client.Get("customkey/backends/name/servers/groupid", false, false)

	c.Assert(err, IsNil)
	c.Assert(server.Node.Value, Equals, `{"URL":"http://master:12345"}`)
	c.Assert(server.Node.TTL, Equals, int64(15))
}

func (s *LeaderSuite) TestSlaveServerRegistration(c *C) {
	master := NewLeaderRegistry("customkey", "groupid", 15)
	master.RegisterApp(s.masterRegistration)
	s.registry.RegisterApp(s.slaveRegistration)

	server, err := s.client.Get("customkey/backends/name/servers/groupid", false, false)

	c.Assert(err, IsNil)
	c.Assert(server.Node.Value, Equals, `{"URL":"http://master:12345"}`)
}

func (s *LeaderSuite) TestSlaveServerBecomesMaster(c *C) {
	// Create a master and slave.
	master := NewLeaderRegistry("customkey", "groupid", 15)
	master.RegisterApp(s.masterRegistration)
	s.registry.RegisterApp(s.slaveRegistration)

	// Remove the old master and re-register the slave.
	_, err := s.client.Delete("customkey/backends/name/servers/groupid", false)
	_ = s.registry.RegisterApp(s.slaveRegistration)
	_ = master.RegisterApp(s.masterRegistration)

	server, err := s.client.Get("customkey/backends/name/servers/groupid", false, false)

	c.Assert(err, IsNil)
	c.Assert(master.IsMaster, Equals, false)
	c.Assert(s.registry.IsMaster, Equals, true)
	c.Assert(server.Node.Value, Equals, `{"URL":"http://slave:67890"}`)
}

func (s *LeaderSuite) TestRegisterHandlerCreatesFrontend(c *C) {
	_ = s.registry.RegisterHandler(s.handlerRegistration)

	frontend, err := s.client.Get("customkey/frontends/host.put.path.to.server/frontend", false, false)

	c.Assert(err, IsNil)
	c.Assert(frontend.Node.Value, Matches, ".*path\\/to\\/server.*")
	c.Assert(frontend.Node.Value, Matches, ".*PUT.*")
	c.Assert(frontend.Node.TTL, Equals, int64(0))
}

func (s *LeaderSuite) TestRegisterHandlerCreatesMiddlewares(c *C) {
	_ = s.registry.RegisterHandler(s.handlerRegistration)

	frontend, err := s.client.Get("customkey/frontends/host.put.path.to.server/middlewares/id", false, false)

	c.Assert(err, IsNil)
	c.Assert(frontend.Node.Value, Matches, `{"Type":"test","Id":"id","Priority":0,"Middleware":"hi"}`)
	c.Assert(frontend.Node.TTL, Equals, int64(0))
}


text:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="assets/js/todo.js"></script>
</body>
</html>