text:
<?php
// Version: 1.0; Post

global $context;

$txt['post_reply'] = 'Post reply';
$txt['message_icon'] = 'Message icon';
$txt['post_in_board'] = 'Post in the board';
$txt['subject_not_filled'] = 'The subject field was not filled out. It is required.';
$txt['message_body_not_filled'] = 'The message body was not filled out. It is required.';
// Use numeric entities in the below string.
$txt['add_bbc'] = 'Add BBC tags';
$txt['bold'] = 'Bold';
$txt['italic'] = 'Italicized';
$txt['underline'] = 'Underline';
$txt['center'] = 'Centered';
$txt['hyperlink'] = 'Insert Hyperlink';
$txt['insert_email'] = 'Insert Email';
$txt['bbc_code'] = 'Insert Code';
// Escape any single quotes in here twice.. 'it\'s' -> 'it\\\'s'.
$txt['bbc_quote'] = 'Insert Quote';
$txt['bbc_spoiler'] = 'Insert Spoiler';
$txt['bbc_footnote'] = 'Insert Footnote';
$txt['list'] = 'Insert List';
$txt['list_unordered'] = 'Insert unordered list';
$txt['list_ordered'] = 'Insert ordered list';

$txt['change_color'] = 'Change color';
$txt['black'] = 'Black';
$txt['red'] = 'Red';
$txt['yellow'] = 'Yellow';
$txt['pink'] = 'Pink';
$txt['green'] = 'Green';
$txt['orange'] = 'Orange';
$txt['purple'] = 'Purple';
$txt['blue'] = 'Blue';
$txt['beige'] = 'Beige';
$txt['brown'] = 'Brown';
$txt['teal'] = 'Teal';
$txt['navy'] = 'Navy';
$txt['maroon'] = 'Maroon';
$txt['lime_green'] = 'Lime Green';
$txt['white'] = 'White';
$txt['disable_smileys'] = 'Disable smileys';
$txt['dont_use_smileys'] = 'Don\'t use smileys.';
// Escape any single quotes in here twice.. 'it\'s' -> 'it\\\'s'.
$txt['posted_on'] = 'Posted on';
$txt['standard'] = 'Standard';
$txt['thumbs_up'] = 'Thumb Up';
$txt['thumbs_down'] = 'Thumb Down';
$txt['excamation_point'] = 'Exclamation point';
$txt['question_mark'] = 'Question mark';
$txt['icon_poll'] = 'Poll';
$txt['lamp'] = 'Lamp';
$txt['add_smileys'] = 'Add smileys';
$txt['flash'] = 'Insert Flash';
$txt['ftp'] = 'Insert FTP Link';
$txt['image'] = 'Insert image';
$txt['table'] = 'Insert table';
$txt['table_td'] = 'Insert table column';
$txt['topic_notify_no'] = 'There are no topics with notification.';
$txt['marquee'] = 'Marquee';
$txt['teletype'] = 'Teletype';
$txt['strike'] = 'Strikethrough';
$txt['glow'] = 'Glow';
$txt['shadow'] = 'Shadow';
$txt['preformatted'] = 'Preformatted text';
$txt['left_align'] = 'Left align';
$txt['right_align'] = 'Right align';
$txt['superscript'] = 'Superscript';
$txt['subscript'] = 'Subscript';
$txt['table_tr'] = 'Insert table row';
$txt['horizontal_rule'] = 'Horizontal Rule';
$txt['font_size'] = 'Font size';
$txt['font_face'] = 'Font face';
$txt['toggle_view'] = 'Toggle view';
$txt['unformat_text'] = 'Remove formatting';

$txt['rich_edit_wont_work'] = 'Your browser does not support Rich Text editing.';
$txt['rich_edit_function_disabled'] = 'Your browser does not support this function.';

// Use numeric entities in the below five strings.
$txt['notifyUnsubscribe'] = 'Unsubscribe to this topic by clicking here';

$txt['lock_after_post'] = 'Lock after Post';
$txt['notify_replies'] = 'Notify me of replies.';
$txt['lock_topic'] = 'Lock this topic.';
$txt['shortcuts'] = 'shortcuts: alt+s submit/post or alt+p preview';
$txt['shortcuts_drafts'] = 'shortcuts: alt+s submit/post, alt+p preview or alt+d save draft';
$txt['shortcuts_firefox'] = 'shortcuts: shift+alt+s submit/post or shift+alt+p preview';
$txt['shortcuts_drafts_firefox'] = 'shortcuts: shift+alt+s submit/post, shift+alt+p preview or shift+alt+d save draft';
$txt['option'] = 'Option';
$txt['reset_votes'] = 'Reset vote count';
$txt['reset_votes_check'] = 'Check this if you want to reset all vote counts to 0.';
$txt['votes'] = 'votes';
$txt['attach'] = 'Attach';
$txt['clean_attach'] = 'Clear attachment';
$txt['attached'] = 'Attached';
$txt['allowed_types'] = 'Allowed file types';
$txt['cant_upload_type'] = 'You cannot upload that type of file. The only allowed extensions are %1$s.';
$txt['uncheck_unwatchd_attach'] = 'Uncheck the attachments you no longer want attached';
$txt['restricted_filename'] = 'That is a restricted filename. Please try a different filename.';
$txt['topic_locked_no_reply'] = 'Warning! This topic is currently/will be locked
Only admins and moderators can reply.';
$txt['awaiting_approval'] = 'Awaiting approval';
$txt['attachment_requires_approval'] = 'Note that any files attached will not be displayed until approved by a moderator.';
$txt['error_temp_attachments'] = 'There are attachments found, which you have attached before but not posted. These attachments are now attached to this post. If you do not want to include them in this post, you can remove them here.';
// Use numeric entities in the below string.
$txt['js_post_will_require_approval'] = 'Reminder: This post will not appear until approved by a moderator.';

$txt['enter_comment'] = 'Enter comment';
// Use numeric entities in the below two strings.
$txt['reported_post'] = 'Reported post';
$txt['reported_to_mod_by'] = 'by';
$txt['rtm10'] = 'Submit';
// Use numeric entities in the below four strings.
$txt['report_following_post'] = 'The following post, "%1$s" by';
$txt['reported_by'] = 'has been reported by';
$txt['board_moderate'] = 'on a board you moderate';
$txt['report_comment'] = 'The reporter has made the following comment';

$txt['attach_restrict_attachmentPostLimit'] = 'maximum total size %1$d KB';
$txt['attach_restrict_attachmentSizeLimit'] = 'maximum individual size %1$d KB';
$txt['attach_restrict_attachmentNumPerPostLimit'] = '%1$d per post';
$txt['attach_restrictions'] = 'Restrictions:';

$txt['post_additionalopt_attach'] = 'Attachments and other options';
$txt['post_additionalopt'] = 'Other options';
$txt['sticky_after'] = 'Sticky this topic.';
$txt['move_after2'] = 'Move this topic.';
$txt['back_to_topic'] = 'Return to this topic.';
$txt['approve_this_post'] = 'Approve this post';

$txt['retrieving_quote'] = 'Retrieving quote...';

$txt['post_visual_verification_label'] = 'Verification';
$txt['post_visual_verification_desc'] = 'Please enter the code in the image above to make this post.';

$txt['poll_options'] = 'Poll Options';
$txt['poll_run'] = 'Run the poll for';
$txt['poll_run_limit'] = '(Leave blank for no limit.)';
$txt['poll_results_visibility'] = 'Result visibility';
$txt['poll_results_anyone'] = 'Show the poll\'s results to anyone.';
$txt['poll_results_voted'] = 'Only show the results after someone has voted.';
$txt['poll_results_after'] = 'Only show the results after the poll has expired.';
$txt['poll_max_votes'] = 'Maximum votes per user';
$txt['poll_do_change_vote'] = 'Allow users to change vote';
$txt['poll_too_many_votes'] = 'You selected too many options.  For this poll, you may only select %1$s options.';
$txt['poll_add_option'] = 'Add Option';
$txt['poll_guest_vote'] = 'Allow guests to vote';

$txt['spellcheck_done'] = 'Spell checking complete.';
$txt['spellcheck_change_to'] = 'Change To:';
$txt['spellcheck_suggest'] = 'Suggestions:';
$txt['spellcheck_change'] = 'Change';
$txt['spellcheck_change_all'] = 'Change All';
$txt['spellcheck_ignore'] = 'Ignore';
$txt['spellcheck_ignore_all'] = 'Ignore All';

$txt['more_attachments'] = 'more attachments';
// Don't use entities in the below string.
$txt['more_attachments_error'] = 'Sorry, you aren\'t allowed to post any more attachments.';

$txt['more_smileys'] = 'more';
$txt['more_smileys_title'] = 'Additional smileys';
$txt['more_smileys_pick'] = 'Pick a smiley';
$txt['more_smileys_close_window'] = 'Close Window';

$txt['error_new_reply'] = 'Warning - while you were typing a new reply has been posted. You may wish to review your post.';
$txt['error_new_replies'] = 'Warning - while you were typing %1$d new replies have been posted. You may wish to review your post.';
$txt['error_new_reply_reading'] = 'Warning - while you were reading a new reply has been posted. You may wish to review your post.';
$txt['error_new_replies_reading'] = 'Warning - while you were reading %1$d new replies have been posted. You may wish to review your post.';

$txt['announce_this_topic'] = 'Send an announcement about this topic to the members:';
$txt['announce_title'] = 'Send an announcement';
$txt['announce_desc'] = 'This form allows you to send an announcement to the selected membergroups about this topic.';
$txt['announce_sending'] = 'Sending announcement of topic';
$txt['announce_done'] = 'done';
$txt['announce_continue'] = 'Continue';
$txt['announce_topic'] = 'Announce topic.';
$txt['announce_regular_members'] = 'Regular Members';

$txt['digest_subject_daily'] = 'Daily Digest';
$txt['digest_subject_weekly'] = 'Weekly Digest';
$txt['digest_intro_daily'] = 'Below is a summary of all activity in your subscribed boards and topics at %1$s today. To unsubscribe please visit the link below.';
$txt['digest_intro_weekly'] = 'Below is a summary of all activity in your subscribed boards and topics at %1$s this week. To unsubscribe please visit the link below.';
$txt['digest_new_topics'] = 'The following topics were started';
$txt['digest_new_topics_line'] = '"%1$s" in the %2$s board';
$txt['digest_new_replies'] = 'Replies have been made in the following topics';
$txt['digest_new_replies_one'] = '1 reply in "%1$s"';
$txt['digest_new_replies_many'] = '%1$d replies in "%2$s"';
$txt['digest_mod_actions'] = 'The following moderation actions have taken place';
$txt['digest_mod_act_sticky'] = '"%1$s" was stickied';
$txt['digest_mod_act_lock'] = '"%1$s" was locked';
$txt['digest_mod_act_unlock'] = '"%1$s" was unlocked';
$txt['digest_mod_act_remove'] = '"%1$s" was removed';
$txt['digest_mod_act_move'] = '"%1$s" was moved';
$txt['digest_mod_act_merge'] = '"%1$s" was merged';
$txt['digest_mod_act_split'] = '"%1$s" was split';

$txt['attach_error_title'] = 'Error uploading attachments.';
$txt['attach_warning'] = 'There was a problem during the uploading of %1$s.';
$txt['attach_max_total_file_size'] = 'Sorry, you are out of attachment space. The total attachment size allowed per post is %1$s KB. Space remaining is %2$s kB.';
$txt['attach_folder_warning'] = 'The attachments directory can not be located. Please notify an administrator of this problem.';
$txt['attach_folder_admin_warning'] = 'The path to the attachments directory (%1$s) is incorrect. Please correct it in the attachment settings area of your admin panel.';
$txt['attach_limit_nag'] = 'You have reached the maximum number of attachments allowed per post.';
$txt['attach_no_upload'] = 'There was a problem and your attachments could not be uploaded';
$txt['attach_remaining'] = '%1$d remaining';
$txt['attach_available'] = '%1$s KB available';
$txt['attach_kb'] = ' (%1$s KB)';
$txt['attach_0_byte_file'] = 'The file appears to be empty. Please contact your forum administrator if this continues to be a problem';
$txt['attached_files_in_session'] = 'The above underlined file(s) have been uploaded but will not be attached to this post until it is submitted.';

$txt['attach_php_error'] = 'Due to an error, your attachment could not be uploaded. Please contact the forum administrator if this problem continues.';
$txt['php_upload_error_1'] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini. Please contact your host if you are unable to correct this issue.';
$txt['php_upload_error_3'] = 'The uploaded file was only partially uploaded. This is a PHP related error. Please contact your host if this problem continues.';
$txt['php_upload_error_4'] = 'No file was uploaded. This is a PHP related error. Please contact your host if this problem continues.';
$txt['php_upload_error_6'] = 'Unable to save. Missing a temporary directory. Please contact your host if you are unable to correct this problem.';
$txt['php_upload_error_7'] = 'Failed to write file to disk. This is a PHP related error. Please contact your host if this problem continues.';
$txt['php_upload_error_8'] = 'A PHP extension stopped the file upload. This is a PHP related error. Please contact your host if this problem continues.';
$txt['error_temp_attachments_new'] = 'There are attachments which you had previously attached but not posted. These attachments are still attached to this post. This post does need to be submitted before these attachments are either saved or removed. You can do that here';
$txt['error_temp_attachments_found'] = 'The following attachments were found which you had previously attached to another post but not posted. It is advisable that you do not post until these are either removed or that post has been submitted.
Click here to remove those attachments. Or here to return to that post.%3$s';
$txt['error_temp_attachments_lost'] = 'The following attachments were found which you had previously attached to another post but not posted. It is advisable that you do not upload any more attachments until these are removed or that post has been submitted.
Click here to remove these attachments.%2$s';
$txt['error_temp_attachments_gone'] = 'Those attachments have now been removed and you have been returned to the page you were previously on';
$txt['error_temp_attachments_flushed'] = 'Please note that any files which had been previously attached, but not posted, have now been removed.';
$txt['error_topic_already_announced'] = 'Please note that this topic has already been announced.';

$txt['cant_access_upload_path'] = 'Cannot access attachments upload path!';
$txt['file_too_big'] = 'Your file is too large. The maximum attachment size allowed is %1$d KB.';
$txt['attach_timeout'] = 'Your attachment couldn\'t be saved. This might happen because it took too long to upload or the file is bigger than the server will allow.

Please consult your server administrator for more information.';
$txt['bad_attachment'] = 'Your attachment has failed security checks and cannot be uploaded. Please consult the forum administrator.';
$txt['ran_out_of_space'] = 'The upload directory is full. Please contact an administrator about this problem.';
$txt['attachments_no_write'] = 'The attachments upload directory is not writable.  Your attachment or avatar cannot be saved.';
$txt['attachments_no_create'] = 'Unable to create a new attachment directory.  Your attachment or avatar cannot be saved.';
$txt['attachments_limit_per_post'] = 'You may not upload more than %1$d attachments per post';


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
com.siemens.cmiv.avt.qiba.dicom
</TITLE>

<META NAME="date" CONTENT="2010-04-30">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

com.siemens.cmiv.avt.qiba.dicom

Classes

DicomImage

DicomPoint

DicomSlice

redpajama_set_name: RedPajamaGithub


text:
using System.Web;

/// <summary>
/// Summary description for NoOpHandler
/// </summary>
public class MyCustomHttpHandler : IHttpHandler
{
    public string MessageText = "<unconfigured>";

	public void ProcessRequest(HttpContext context)
	{
		context.Response.ContentType = "text/plain";
		context.Response.Write(@"Response from MyCustomHttpHandler:" + MessageText);
	}

	public bool IsReusable
	{
		get { return true; }
	}
}


text:
package com.github.trytocatch.regexreplacer.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.github.trytocatch.regexreplacer.model.expression.Controller;
import com.github.trytocatch.regexreplacer.model.expression.ExpressionParser;
import com.github.trytocatch.regexreplacer.model.expression.ResetObserver;
import com.github.trytocatch.regexreplacer.model.expression.ResultObserver;
import com.github.trytocatch.utils.concurrent.LatestResultsProvider;

/**
 * not thread-safe
 * @author trytocatch@163.com
 * @date 2013-2-3
 */
public class RegexController extends LatestResultsProvider implements Controller{
	private static final int DEFAULT_UPDATE_DELAY = 0;

	private final Vector<ResetObserver> observers;
	/** one ResultObserver at most */
	private volatile ResultObserver resultObserver;

	private volatile boolean liveUpdate;
	private volatile ExpressionParser parser;

	/**it's visible in getRealReplaceResult(happens-before rules in updateAndWait
	 * and barrier.nextCycle)
	 */
	private Object replacer = "";
	private Pattern pattern;
	private Matcher matcher;
	private boolean expressionAvailable;

	private String textCache = "";
	private String expressionCache = "";
	private String regexCache = "";
	private int patternFlag = 0;

	private boolean replaceExpressionChanged;
	private boolean patternChanged;

	private List<MatchResultInfo> result;
	private int absRowNum;//no volatile

	public RegexController() {
		this(true, DEFAULT_UPDATE_DELAY);
	}

	public RegexController(boolean liveUpdate, int updateDelay) {
		super(updateDelay, -1);
		observers = new Vector<ResetObserver>();
		parser = new ExpressionParser(this);
		this.liveUpdate = liveUpdate;
		expressionAvailable = true;
	}

	public ExpressionParser getParser() {
		return parser;
	}

	public void setParser(ExpressionParser parser) {
		if (parser == null)
			throw new IllegalArgumentException("ExpressionParser can't be null");
		this.parser = parser;
	}

	public boolean isLiveUpdate() {
		return liveUpdate;
	}

	/**
	 * @param liveUpdate
	 *            true:RegexController will update the result while some
	 *            parameters changed
	 */
	public void setLiveUpdate(boolean liveUpdate) {
		this.liveUpdate = liveUpdate;
		if (liveUpdate)
			update();
	}

	private void updateParameters(){
		updateParametersVersion();
		if (liveUpdate)
			update();
		else
			stopCurrentWorking();
	}

	public boolean isExpressionAvailable() {
		return expressionAvailable;
	}

	/**
	 *
	 * @param expressionAvailable
	 *            true: use function to replace false: replace with text
	 *            directly
	 */
	public void setExpressionAvailable(boolean expressionAvailable) {
		if (this.expressionAvailable != expressionAvailable) {
			this.expressionAvailable = expressionAvailable;
			replaceExpressionChanged = true;
			updateParameters();
		}
	}

	public void setText(String text) {
		if(setTextImpl(text)){
			updateParameters();
		}
	}

	private boolean setTextImpl(String text) {
		if (text == null)
			text = "";
		if (textCache.hashCode() != text.hashCode() || !textCache.equals(text)) {
			textCache = text;
			return true;
		}
		return false;
	}

	public void setPatternFlag(int flag) {
		if(setPatternFlagImpl(flag)){
			updateParameters();
		}
	}

	private boolean setPatternFlagImpl(int flag) {
		if (patternFlag != flag) {
			patternFlag = flag;
			patternChanged = true;
			return true;
		}
		return false;
	}

	public void setRegexStr(String regexStr) {
		if(setRegexStrImpl(regexStr))
			updateParameters();
	}

	private boolean setRegexStrImpl(String regexStr) {
		if (regexStr == null)
			regexStr = "";
		if (regexCache.equals(regexStr) == false) {
			regexCache = regexStr;
			patternChanged = true;
			return true;
		}
		return false;
	}

	public void setReplaceExpression(String expression) {
		if(setReplaceExpressionImpl(expression))
			updateParameters();
	}

	private boolean setReplaceExpressionImpl(String expression) {
		if (expression == null)
			expression = "";
		if (expressionCache.equals(expression) == false) {
			expressionCache = expression;
			replaceExpressionChanged = true;
			return true;
		}
		return false;
	}

	@Override
	protected void calculateResult() {
		if (resultObserver != null)
			resultObserver.onStart();
		try {
			workOutResultImpl();
		} catch (Throwable t) {
			fireCalculationFailed();
			if (resultObserver != null)
				resultObserver.onResultChanged(
						null,
						t.getClass().getSimpleName()
								+ (t.getMessage() != null ? ": "
										+ t.getMessage() : ""));
		}
	}

	private void workOutResultImpl() {
		ArrayList<MatchResultInfo> resultTemp = null;
		if (patternChanged && isWorking())
			reBuildPattern();
		if (replaceExpressionChanged && isWorking())
			reParseReplaceExpression();
		if (isWorking() && pattern != null && !textCache.isEmpty()) {
			matcher = pattern.matcher(textCache);
			reset();
			if (matcher != null && replacer != null) {// replacer ==null won't
														// happen
				resultTemp = new ArrayList<MatchResultInfo>(
						textCache.length() / 10);
				while (matcher.find() && isWorking()) {
					absRowNum++;
					resultTemp
							.add(new MatchResultInfo(matcher.group(), replacer
									.toString(), matcher.start(), matcher.end()));
				}
			}
		} else
			matcher = null;
		result = resultTemp;
		if (resultObserver != null && isWorking())
			resultObserver
					.onResultChanged(
							Collections
									.unmodifiableList(result == null ? new ArrayList<MatchResultInfo>(
											0) : result), null);
	}

	/**
	 *
	 * @param indexToBeReplace
	 *            : should in ascending order(1, 2, 4, 7), if
	 *            indexToBeReplace==null return all result
	 *
	 * @return
	 * @throws Exception
	 *             The result has changed and indexToBeReplace!=null !
	 */
	public List<MatchResultInfo> getRealReplaceResult(int[] indexToBeReplace)
			throws Exception {
		if (updateAndWait() != UPDATE_NO_NEED_TO_UPDATE && indexToBeReplace != null)
			throw new Exception(
					"The result has changed! Operation is canceled!");
		if (indexToBeReplace == null)
			return Collections
					.unmodifiableList(result == null ? new ArrayList<MatchResultInfo>(
							0) : result);
		ArrayList<MatchResultInfo> realReplaceResult = new ArrayList<MatchResultInfo>(
				indexToBeReplace.length);
		if (indexToBeReplace.length != 0) {
			reset();
			int row = 0, index = 0;
			if (matcher != null && replacer != null)
				while (matcher.find()) {
					absRowNum++;
					if (index >= indexToBeReplace.length)
						break;
					if (row++ != indexToBeReplace[index])
						continue;
					index++;
					realReplaceResult
							.add(new MatchResultInfo(matcher.group(), replacer
									.toString(), matcher.start(), matcher.end()));
				}
		}
		return Collections.unmodifiableList(realReplaceResult);
	}

	private void reParseReplaceExpression() {
		replaceExpressionChanged = false;
		observers.clear();
		if (expressionAvailable)
			try {
				replacer = parser.parse(expressionCache);
			} catch (Throwable t) {
				replaceExpressionChanged = true;
				if (t instanceof Error)
					throw (Error) t;
				else if (t instanceof RuntimeException)
					throw (RuntimeException) t;
			}
		else
			replacer = expressionCache == null ? "" : expressionCache;
	}

	private void reBuildPattern() {
		patternChanged = false;
		if (regexCache.isEmpty())
			pattern = null;
		else
			try{
				pattern = Pattern.compile(regexCache, patternFlag);
			}catch(Throwable t){
				patternChanged = true;
				if(t instanceof Error)
					throw (Error)t;
				else if(t instanceof RuntimeException)
					throw (RuntimeException)t;
			}
	}

	private void reset() {
		fireReset();
		absRowNum = 0;
		if (matcher != null)
			matcher.reset();
	}

	protected void fireReset() {
		for (ResetObserver observer : observers)
			if (observer != null)
				observer.onReset();
	}

	public void setResultObserver(ResultObserver resultObserver) {
		this.resultObserver = resultObserver;
	}

	@Override
	public void addResetObserver(ResetObserver observer) {
		observers.add(observer);
	}

	@Override
	public void addResetObserver(Collection<ResetObserver> observers) {
		this.observers.addAll(observers);
	}

	@Override
	public void removeResetObserver(ResetObserver observer) {
		observers.remove(observer);
	}

	@Override
	public Integer getAbsRowNum() {
		return absRowNum;
	}

	@Override
	public String getGroup(int n) {
		return matcher.group(n);
	}

	@Override
	public String getGroup(String groupName) {
		return matcher.group(groupName);
	}

}


text:
package marblechaincodeutil

import (
	"bytes"
	"encoding/base64"
	"fmt"

	"github.com/hyperledger/fabric/integration/nwo"
	"github.com/hyperledger/fabric/integration/nwo/commands"

	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
)

// AddMarble invokes marbles_private chaincode to add a marble
func AddMarble(n *nwo.Network, orderer *nwo.Orderer, channelID, chaincodeName, marbleDetails string, peer *nwo.Peer) {
	marbleDetailsBase64 := base64.StdEncoding.EncodeToString([]byte(marbleDetails))

	command := commands.ChaincodeInvoke{
		ChannelID: channelID,
		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
		Name:      chaincodeName,
		Ctor:      `{"Args":["initMarble"]}`,
		Transient: fmt.Sprintf(`{"marble":"%s"}`, marbleDetailsBase64),
		PeerAddresses: []string{
			n.PeerAddress(peer, nwo.ListenPort),
		},
		WaitForEvent: true,
	}
	invokeChaincode(n, peer, command)
	nwo.WaitUntilEqualLedgerHeight(n, channelID, nwo.GetLedgerHeight(n, peer, channelID), n.Peers...)
}

// DeleteMarble invokes marbles_private chaincode to delete a marble
func DeleteMarble(n *nwo.Network, orderer *nwo.Orderer, channelID, chaincodeName, marbleDelete string, peer *nwo.Peer) {
	marbleDeleteBase64 := base64.StdEncoding.EncodeToString([]byte(marbleDelete))

	command := commands.ChaincodeInvoke{
		ChannelID: channelID,
		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
		Name:      chaincodeName,
		Ctor:      `{"Args":["delete"]}`,
		Transient: fmt.Sprintf(`{"marble_delete":"%s"}`, marbleDeleteBase64),
		PeerAddresses: []string{
			n.PeerAddress(peer, nwo.ListenPort),
		},
		WaitForEvent: true,
	}
	invokeChaincode(n, peer, command)
	nwo.WaitUntilEqualLedgerHeight(n, channelID, nwo.GetLedgerHeight(n, peer, channelID), n.Peers...)
}

// PurgeMarble invokes marbles_private chaincode to purge a marble
func PurgeMarble(n *nwo.Network, orderer *nwo.Orderer, channelID, chaincodeName, marblePurge string, peer *nwo.Peer) {
	marblePurgeBase64 := base64.StdEncoding.EncodeToString([]byte(marblePurge))

	command := commands.ChaincodeInvoke{
		ChannelID: channelID,
		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
		Name:      chaincodeName,
		Ctor:      `{"Args":["purge"]}`,
		Transient: fmt.Sprintf(`{"marble_purge":"%s"}`, marblePurgeBase64),
		PeerAddresses: []string{
			n.PeerAddress(peer, nwo.ListenPort),
		},
		WaitForEvent: true,
	}
	invokeChaincode(n, peer, command)
	nwo.WaitUntilEqualLedgerHeight(n, channelID, nwo.GetLedgerHeight(n, peer, channelID), n.Peers...)
}

// TransferMarble invokes marbles_private chaincode to transfer marble's ownership
func TransferMarble(n *nwo.Network, orderer *nwo.Orderer, channelID, chaincodeName, marbleOwner string, peer *nwo.Peer) {
	marbleOwnerBase64 := base64.StdEncoding.EncodeToString([]byte(marbleOwner))

	command := commands.ChaincodeInvoke{
		ChannelID: channelID,
		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
		Name:      chaincodeName,
		Ctor:      `{"Args":["transferMarble"]}`,
		Transient: fmt.Sprintf(`{"marble_owner":"%s"}`, marbleOwnerBase64),
		PeerAddresses: []string{
			n.PeerAddress(peer, nwo.ListenPort),
		},
		WaitForEvent: true,
	}
	invokeChaincode(n, peer, command)
	nwo.WaitUntilEqualLedgerHeight(n, channelID, nwo.GetLedgerHeight(n, peer, channelID), n.Peers...)
}

// AssertGetMarblesByRange asserts that
func AssertGetMarblesByRange(n *nwo.Network, channelID, chaincodeName, marbleRange, expectedMsg string, peer *nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["getMarblesByRange", %s]}`, marbleRange)
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, true, peer)
}

// AssertPresentInCollectionM asserts that the private data for given marble is present in collection
// 'readMarble' at the given peers
func AssertPresentInCollectionM(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarble","%s"]}`, marbleName)
	expectedMsg := fmt.Sprintf(`"docType":"marble","name":"%s"`, marbleName)
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, true, peerList...)
}

// AssertPresentInCollectionMPD asserts that the private data for given marble is present
// in collection 'readMarblePrivateDetails' at the given peers
func AssertPresentInCollectionMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarblePrivateDetails","%s"]}`, marbleName)
	expectedMsg := fmt.Sprintf(`"docType":"marblePrivateDetails","name":"%s"`, marbleName)
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, true, peerList...)
}

// CheckPresentInCollectionM checks then number of peers that have the private data for given marble
// in collection 'readMarble'
func CheckPresentInCollectionM(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) int {
	query := fmt.Sprintf(`{"Args":["readMarble","%s"]}`, marbleName)
	expectedMsg := fmt.Sprintf(`{"docType":"marble","name":"%s"`, marbleName)
	command := commands.ChaincodeQuery{
		ChannelID: channelID,
		Name:      chaincodeName,
		Ctor:      query,
	}
	present := 0
	for _, peer := range peerList {
		sess, err := n.PeerUserSession(peer, "User1", command)
		Expect(err).NotTo(HaveOccurred())
		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
		if bytes.Contains(sess.Buffer().Contents(), []byte(expectedMsg)) {
			present++
		}
	}
	return present
}

// CheckPresentInCollectionMPD checks the number of peers that have the private data for given marble
// in collection 'readMarblePrivateDetails'
func CheckPresentInCollectionMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) int {
	query := fmt.Sprintf(`{"Args":["readMarblePrivateDetails","%s"]}`, marbleName)
	expectedMsg := fmt.Sprintf(`{"docType":"marblePrivateDetails","name":"%s"`, marbleName)
	command := commands.ChaincodeQuery{
		ChannelID: channelID,
		Name:      chaincodeName,
		Ctor:      query,
	}
	present := 0
	for _, peer := range peerList {
		sess, err := n.PeerUserSession(peer, "User1", command)
		Expect(err).NotTo(HaveOccurred())
		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
		if bytes.Contains(sess.Buffer().Contents(), []byte(expectedMsg)) {
			present++
		}
	}
	return present
}

// AssertNotPresentInCollectionM asserts that the private data for given marble is NOT present
// in collection 'readMarble' at the given peers
func AssertNotPresentInCollectionM(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarble","%s"]}`, marbleName)
	expectedMsg := "private data matching public hash version is not available"
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, false, peerList...)
}

// AssertNotPresentInCollectionMPD asserts that the private data for given marble is NOT present
// in collection 'readMarblePrivateDetails' at the given peers
func AssertNotPresentInCollectionMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarblePrivateDetails","%s"]}`, marbleName)
	expectedMsg := "private data matching public hash version is not available"
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, false, peerList...)
}

// AssertDoesNotExistInCollectionM asserts that the private data for given marble
// does not exist in collection 'readMarble' (i.e., is never created/has been deleted/has been purged)
func AssertDoesNotExistInCollectionM(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarble","%s"]}`, marbleName)
	expectedMsg := "Marble does not exist"
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, false, peerList...)
}

// AssertDoesNotExistInCollectionMPD asserts that the private data for given marble
// does not exist in collection 'readMarblePrivateDetails' (i.e., is never created/has been deleted/has been purged)
func AssertDoesNotExistInCollectionMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarblePrivateDetails","%s"]}`, marbleName)
	expectedMsg := "Marble private details does not exist"
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, false, peerList...)
}

// AssertOwnershipInCollectionM asserts that the private data for given marble is present
// in collection 'readMarble' at the given peers
func AssertOwnershipInCollectionM(n *nwo.Network, channelID, chaincodeName, marbleName, owner string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarble","%s"]}`, marbleName)
	expectedMsg := fmt.Sprintf(`"owner":"%s"`, owner)
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, true, peerList...)
}

// AssertNoReadAccessToCollectionMPD asserts that the orgs of the given peers do not have
// read access to private data for the collection readMarblePrivateDetails
func AssertNoReadAccessToCollectionMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, peerList ...*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["readMarblePrivateDetails","%s"]}`, marbleName)
	expectedMsg := "tx creator does not have read access permission"
	queryChaincodePerPeer(n, query, channelID, chaincodeName, expectedMsg, false, peerList...)
}

func queryChaincodePerPeer(n *nwo.Network, query, channelID, chaincodeName, expectedMsg string, expectSuccess bool, peerList ...*nwo.Peer) {
	command := commands.ChaincodeQuery{
		ChannelID: channelID,
		Name:      chaincodeName,
		Ctor:      query,
	}
	for _, peer := range peerList {
		queryChaincode(n, peer, command, expectedMsg, expectSuccess)
	}
}

// AssertMarblesPrivateHashM asserts that getMarbleHash is accessible from all peers that has the chaincode instantiated
func AssertMarblesPrivateHashM(n *nwo.Network, channelID, chaincodeName, marbleName string, expectedBytes []byte, peerList []*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["getMarbleHash","%s"]}`, marbleName)
	verifyPvtdataHash(n, query, channelID, chaincodeName, peerList, expectedBytes)
}

// AssertMarblesPrivateDetailsHashMPD asserts that getMarblePrivateDetailsHash is accessible from all peers that has the chaincode instantiated
func AssertMarblesPrivateDetailsHashMPD(n *nwo.Network, channelID, chaincodeName, marbleName string, expectedBytes []byte, peerList []*nwo.Peer) {
	query := fmt.Sprintf(`{"Args":["getMarblePrivateDetailsHash","%s"]}`, marbleName)
	verifyPvtdataHash(n, query, channelID, chaincodeName, peerList, expectedBytes)
}

// AssertInvokeChaincodeFails asserts that a chaincode invoke fails with a specified error message
func AssertInvokeChaincodeFails(n *nwo.Network, peer *nwo.Peer, command commands.ChaincodeInvoke, expectedMessage string) {
	sess, err := n.PeerUserSession(peer, "User1", command)
	Expect(err).NotTo(HaveOccurred())
	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
	Expect(sess.Err).To(gbytes.Say(expectedMessage))
}

func invokeChaincode(n *nwo.Network, peer *nwo.Peer, command commands.ChaincodeInvoke) {
	sess, err := n.PeerUserSession(peer, "User1", command)
	Expect(err).NotTo(HaveOccurred())
	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
	Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful."))
}

func queryChaincode(n *nwo.Network, peer *nwo.Peer, command commands.ChaincodeQuery, expectedMessage string, expectSuccess bool) {
	sess, err := n.PeerUserSession(peer, "User1", command)
	Expect(err).NotTo(HaveOccurred())
	if expectSuccess {
		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
		Expect(sess).To(gbytes.Say(expectedMessage))
	} else {
		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
		Expect(sess.Err).To(gbytes.Say(expectedMessage))
	}
}

// verifyPvtdataHash verifies the private data hash matches the expected bytes.
// Cannot reuse verifyAccess because the hash bytes are not valid utf8 causing gbytes.Say to fail.
func verifyPvtdataHash(n *nwo.Network, query, channelID, chaincodeName string, peers []*nwo.Peer, expected []byte) {
	command := commands.ChaincodeQuery{
		ChannelID: channelID,
		Name:      chaincodeName,
		Ctor:      query,
	}

	for _, peer := range peers {
		sess, err := n.PeerUserSession(peer, "User1", command)
		Expect(err).NotTo(HaveOccurred())
		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
		actual := sess.Buffer().Contents()
		// verify actual bytes contain expected bytes - cannot use equal because session may contain extra bytes
		Expect(bytes.Contains(actual, expected)).To(Equal(true))
	}
}


text:
"""Functional tests for Concat Op."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

from tensorflow.python.ops import gen_array_ops

class ConcatOpTest(tf.test.TestCase):

  def testHStack(self):
    with self.test_session():
      p1 = tf.placeholder(tf.float32, shape=[4, 4])
      p2 = tf.placeholder(tf.float32, shape=[4, 4])
      c = tf.concat(0, [p1, p2])
      params = {
          p1: np.random.rand(4, 4).astype("f"),
          p2: np.random.rand(4, 4).astype("f")
          }
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:4, :], params[p1])
    self.assertAllEqual(result[4:, :], params[p2])

  def testVStack(self):
    with self.test_session():
      p1 = tf.placeholder(tf.float32, shape=[4, 4])
      p2 = tf.placeholder(tf.float32, shape=[4, 4])
      c = tf.concat(1, [p1, p2])
      params = {
          p1: np.random.rand(4, 4).astype("f"),
          p2: np.random.rand(4, 4).astype("f")
          }
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:, :4], params[p1])
    self.assertAllEqual(result[:, 4:], params[p2])

  def testInt32GPU(self):
    with self.test_session(use_gpu=True):
      p1 = np.random.rand(2, 3).astype("i")
      p2 = np.random.rand(2, 3).astype("i")
      x1 = tf.constant(p1)
      x2 = tf.constant(p2)
      c = tf.concat(0, [x1, x2])
      result = c.eval()
    self.assertAllEqual(result[:2, :], p1)
    self.assertAllEqual(result[2:, :], p2)

  def testRefType(self):
    with self.test_session():
      p1 = np.random.rand(4, 4).astype("f")
      p2 = np.random.rand(4, 4).astype("f")
      v1 = tf.Variable(p1)
      v2 = tf.Variable(p2)
      c = tf.concat(0, [v1, v2])
      tf.initialize_all_variables().run()
      result = c.eval()

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:4, :], p1)
    self.assertAllEqual(result[4:, :], p2)

  def _testRandom(self, dtype, use_gpu=False):
    # Random dims of rank 5
    shape = np.random.randint(1, 5, size=5)
    # Random number of tensors, but always > 1.
    num_tensors = np.random.randint(2, 10)
    # Random dim to concat on
    concat_dim = np.random.randint(5)
    params = {}
    if dtype == tf.bfloat16:
      dtype_feed = tf.float32
    else:
      dtype_feed = dtype
    with self.test_session(use_gpu=use_gpu):
      p = []
      for i in np.arange(num_tensors):
        input_shape = shape
        input_shape[concat_dim] = np.random.randint(1, 5)
        placeholder = tf.placeholder(dtype_feed, shape=input_shape)
        p.append(placeholder)

        t = dtype_feed.as_numpy_dtype
        params[placeholder] = np.random.rand(*input_shape).astype(t)

      if dtype != dtype_feed:
        concat_inputs = [tf.cast(p_i, dtype) for p_i in p]
      else:
        concat_inputs = p
      c = tf.concat(concat_dim, concat_inputs)
      if dtype != dtype_feed:
        c = tf.cast(c, dtype_feed)
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    cur_offset = 0

    for i in np.arange(num_tensors):
      # The index into the result is the ':' along all dimensions
      # except the concat_dim. slice(0, size) is used for ':', and
      # a list of slices is used to index into result.
      ind = [slice(0, params[p[i]].shape[j]) for j in np.arange(5)]
      ind[concat_dim] = slice(cur_offset,
                              cur_offset + params[p[i]].shape[concat_dim])
      cur_offset += params[p[i]].shape[concat_dim]
      if dtype == dtype_feed:
        self.assertAllEqual(result[ind], params[p[i]])
      else:
        self.assertAllClose(result[ind], params[p[i]], 0.01)

  def testRandom(self):
    self._testRandom(tf.float32)
    self._testRandom(tf.float32, use_gpu=True)
    self._testRandom(tf.int16)
    self._testRandom(tf.int32, use_gpu=True)
    self._testRandom(tf.bfloat16)
    self._testRandom(tf.bfloat16, use_gpu=True)

  def testInvalidConcatDimTypeAndShape(self):
    a = tf.Variable(tf.constant(1.0, shape=[1]))
    b = tf.Variable(tf.constant(2.0, shape=[1]))
    with self.assertRaises(ValueError):
      tf.concat(a, b)
    with self.assertRaises(TypeError):
      tf.concat(4.2, 1)
    with self.assertRaises(ValueError):
      tf.concat(a, 1)
    with self.assertRaises(TypeError):
      tf.concat(a, [a, b])
    with self.assertRaises(ValueError):
      tf.concat([3], [a, b])
    with self.assertRaises(ValueError):
      tf.concat(0, [])
    # An integer tensor for shape dim should throw no error.
    tf.concat(tf.constant(0, shape=[]), 1)
    # A non-scalar tensor for shape should throw ValueError.
    with self.assertRaises(ValueError):
      tf.concat(tf.constant(0, shape=[1]), 1)

  def _testGradientsSimple(self, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      inp = []
      inp_tensors = []
      for x in [1, 2, 6]:
        shape = [10, x, 2]
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            tf.constant([float(y) for y in t.flatten()],
                                 shape=shape, dtype=tf.float32))
      c = tf.concat(1, inp_tensors)
      output_shape = [10, 9, 2]
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = tf.constant([float(x) for x in grad_inp.flatten()],
                                         shape=output_shape)
      grad = tf.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = tf.concat(1, grad)
      result = concated_grad.eval()
    self.assertAllEqual(result, grad_inp)

  def testGradientsSimpleAll(self):
    self._testGradientsSimple(use_gpu=True)
    self._testGradientsSimple(use_gpu=False)

  def _testGradientsFirstDim(self, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      inp = []
      inp_tensors = []
      for x in [1, 2, 6]:
        shape = [x, 10, 2]
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            tf.constant([float(y) for y in t.flatten()],
                                 shape=shape, dtype=tf.float32))
      c = tf.concat(0, inp_tensors)
      output_shape = [9, 10, 2]
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = tf.constant([float(x) for x in grad_inp.flatten()],
                                         shape=output_shape)
      grad = tf.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = tf.concat(0, grad)
      result = concated_grad.eval()

    self.assertAllEqual(result, grad_inp)

  def testGradientsFirstDimAll(self):
    self._testGradientsFirstDim(use_gpu=False)
    self._testGradientsFirstDim(use_gpu=True)

  def _testGradientsLastDim(self, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      inp = []
      inp_tensors = []
      for x in [1, 2, 6]:
        shape = [10, 2, x]
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            tf.constant([float(y) for y in t.flatten()],
                                 shape=shape, dtype=tf.float32))
      c = tf.concat(2, inp_tensors)
      output_shape = [10, 2, 9]
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = tf.constant([float(x) for x in grad_inp.flatten()],
                                         shape=output_shape)
      grad = tf.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = tf.concat(2, grad)
      result = concated_grad.eval()

    self.assertAllEqual(result, grad_inp)

  def testGradientsLastDimAll(self):
    self._testGradientsLastDim(use_gpu=False)
    self._testGradientsLastDim(use_gpu=True)

  def _RunAndVerifyGradientsRandom(self, use_gpu):
    # Random dims of rank 5
    input_shape = np.random.randint(1, 5, size=5)
    # Random number of tensors
    num_tensors = np.random.randint(12, 20)
    # Random dim to concat on
    concat_dim = np.random.randint(5)
    concat_dim_sizes = np.random.randint(1, 5, size=num_tensors)
    with self.test_session(use_gpu=use_gpu):
      inp = []
      inp_tensors = []
      for x in concat_dim_sizes:
        shape = input_shape
        shape[concat_dim] = x
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            tf.constant([float(y) for y in t.flatten()],
                                 shape=shape, dtype=tf.float32))
      c = tf.concat(concat_dim, inp_tensors)
      output_shape = input_shape
      output_shape[concat_dim] = concat_dim_sizes.sum()
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = tf.constant([float(x) for x in grad_inp.flatten()],
                                         shape=output_shape)
      grad = tf.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = tf.concat(concat_dim, grad)
      result = concated_grad.eval()

    self.assertAllEqual(result, grad_inp)

  def testGradientsRandom(self):
    for _ in range(5):
      self._RunAndVerifyGradientsRandom(use_gpu=False)
      self._RunAndVerifyGradientsRandom(use_gpu=True)

  def testShapeError(self):
    # Rank doesn't match.
    with self.assertRaises(ValueError):
      tf.concat(1, [tf.constant(10.0, shape=[4, 4, 4, 4]),
                    tf.constant(20.0, shape=[4, 4, 4])])

    # Dimensions don't match in a non-concat dim.
    with self.assertRaises(ValueError):
      tf.concat(1, [tf.constant(10.0, shape=[1, 2, 1]),
                    tf.constant(20.0, shape=[3, 2, 1])])

    # concat_dim out of range.
    with self.assertRaises(ValueError):
      tf.concat(3, [tf.constant(10.0, shape=[4, 4, 4]),
                    tf.constant(20.0, shape=[4, 4, 4])])

    # concat_dim out of range
    with self.assertRaises(ValueError):
      tf.concat(-4, [tf.constant(10.0, shape=[4, 4, 4]),
                     tf.constant(20.0, shape=[4, 4, 4])])

  def testShapeWithUnknownConcatDim(self):
    p1 = tf.placeholder(tf.float32)
    c1 = tf.constant(10.0, shape=[4, 4, 4, 4])
    p2 = tf.placeholder(tf.float32)
    c2 = tf.constant(20.0, shape=[4, 4, 4, 4])
    dim = tf.placeholder(tf.int32)
    concat = tf.concat(dim, [p1, c1, p2, c2])
    self.assertEqual(4, concat.get_shape().ndims)

    # All dimensions unknown.
    concat2 = tf.concat(dim, [p1, p2])
    self.assertEqual(None, concat2.get_shape())

    # Rank doesn't match.
    c3 = tf.constant(30.0, shape=[4, 4, 4])
    with self.assertRaises(ValueError):
      tf.concat(dim, [p1, c1, p2, c3])

  def testZeroSize(self):
    # Verify that concat doesn't crash and burn for zero size inputs
    np.random.seed(7)
    for use_gpu in False, True:
      with self.test_session(use_gpu=use_gpu) as sess:
        for shape0 in (), (2,):
          axis = len(shape0)
          for shape1 in (), (3,):
            for n0 in 0, 1, 2:
              for n1 in 0, 1, 2:
                x0 = np.random.randn(*(shape0 + (n0,) + shape1))
                x1 = np.random.randn(*(shape0 + (n1,) + shape1))
                correct = np.concatenate([x0, x1], axis=axis)
                # TODO(irving): Make tf.concat handle map, then drop list().
                xs = list(map(tf.constant, [x0, x1]))
                c = tf.concat(axis, xs)
                self.assertAllEqual(c.eval(), correct)
                # Check gradients
                dc = np.random.randn(*c.get_shape().as_list())
                dxs = sess.run(tf.gradients(c, xs, dc))
                self.assertAllEqual(dc, np.concatenate(dxs, axis=axis))

  def testTensorConcatDim0Grad(self):
    x_shapes = [[20, 7, 3], [10, 7, 3], [14, 7, 3]]
    output_shape = [44, 7, 3]
    x_vals = [np.random.random_sample(x_shape).astype(
        np.float64) for x_shape in x_shapes]
    with self.test_session():
      xs = [tf.constant(x_val) for x_val in x_vals]
      output = tf.concat(0, xs)
      err = tf.test.compute_gradient_error(xs, x_shapes, output, output_shape)
    self.assertLess(err, 1e-11)

  def testTensorConcatDim1Grad(self):
    x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]]
    output_shape = [20, 11, 3]
    x_vals = [np.random.random_sample(x_shape).astype(
        np.float64) for x_shape in x_shapes]
    with self.test_session():
      xs = [tf.constant(x_val) for x_val in x_vals]
      output = tf.concat(1, xs)
      err = tf.test.compute_gradient_error(xs, x_shapes, output, output_shape)
    self.assertLess(err, 1e-11)

  def testIndexedSlicesConcatDim0Grad(self):
    x_shapes = [[20, 7, 3], [10, 7, 3], [14, 7, 3]]
    output_shape = [4, 7, 3]
    x_vals = [np.random.random_sample(x_shape).astype(
        np.float64) for x_shape in x_shapes]
    with self.test_session():
      xs = [tf.constant(x_val) for x_val in x_vals]
      x_concat = tf.concat(0, xs)
      output = tf.gather(x_concat, [1, 2, 0, 5])
      err = tf.test.compute_gradient_error(xs, x_shapes, output, output_shape)
    self.assertLess(err, 1e-11)

  def testIndexedSlicesConcatDim1Grad(self):
    x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]]
    output_shape = [4, 11, 3]
    x_vals = [np.random.random_sample(x_shape).astype(
        np.float64) for x_shape in x_shapes]
    with self.test_session():
      xs = [tf.constant(x_val) for x_val in x_vals]
      x_concat = tf.concat(1, xs)
      output = tf.gather(x_concat, [1, 2, 0, 5])
      err = tf.test.compute_gradient_error(xs, x_shapes, output, output_shape)
    self.assertLess(err, 1e-11)

  def testIndexedSlicesConcatDim2Grad(self):
    x_shapes = [[20, 7, 3], [20, 7, 1], [20, 7, 2]]
    output_shape = [4, 7, 6]
    x_vals = [np.random.random_sample(x_shape).astype(
        np.float64) for x_shape in x_shapes]
    with self.test_session():
      xs = [tf.constant(x_val) for x_val in x_vals]
      x_concat = tf.concat(2, xs)
      output = tf.gather(x_concat, [1, 2, 0, 5])
      err = tf.test.compute_gradient_error(xs, x_shapes, output, output_shape)
    self.assertLess(err, 1e-11)

  def testConcatTuple(self):
    c1 = np.random.rand(4, 4)
    c2 = np.random.rand(4, 4)
    with self.test_session():
      concat_list_t = tf.concat(0, [c1, c2])
      concat_tuple_t = tf.concat(0, (c1, c2))
      self.assertAllEqual(concat_list_t.eval(), concat_tuple_t.eval())

  def testConcatNoScalars(self):
    with self.test_session():
      scalar = tf.constant(7)
      dim = tf.placeholder(tf.int32)
      with self.assertRaisesRegexp(
          ValueError, r"Can't concatenate scalars \(use tf\.pack instead\)"):
        tf.concat(dim, [scalar, scalar, scalar])

  def testConcatGradNumNodes(self):
    g = tf.Graph()
    n = 10
    with g.as_default():
      x = tf.constant([1, 1])
      y = tf.concat(0, [x] * n)
      before = len(g.get_operations())
      _ = tf.gradients([y], [x], [y])
      after = len(g.get_operations())
      self.assertEqual(2 * n + 2, after - before)
      print("graph = ", [x.name for x in g.get_operations()])

  # important as gpu implementation could fail if
  # shared memory is not large for all the inputs
  def testConcatLargeNumberOfTensors(self):
    with self.test_session(use_gpu=True):
      for concat_dim in range(2):
        params = {}
        p = []
        shape = np.array([7, 13])
        if tf.test.is_gpu_available():
          num_tensors = 10000
        else:
          num_tensors = 1000
        for i in np.arange(num_tensors):
          input_shape = shape
          placeholder = tf.placeholder(tf.float32, shape=input_shape)
          p.append(placeholder)

          params[placeholder] = np.random.rand(*input_shape).astype(np.float32)

        concat_inputs = p
        c = tf.concat(concat_dim, concat_inputs)
        result = c.eval(feed_dict=params)

        self.assertEqual(result.shape, c.get_shape())
        cur_offset = 0

        for i in np.arange(num_tensors):
          # The index into the result is the ':' along all dimensions
          # except the concat_dim. slice(0, size) is used for ':', and
          # a list of slices is used to index into result.
          index = [slice(0, params[p[i]].shape[j]) for j in np.arange(2)]
          index[concat_dim] = slice(cur_offset,
                                    cur_offset + params[p[i]].shape[concat_dim])
          cur_offset += params[p[i]].shape[concat_dim]
          self.assertAllEqual(result[index], params[p[i]])

  def testConcatNegativeAxis(self):
    with self.test_session(use_gpu=True):
      t1 = [[1, 2, 3], [4, 5, 6]]
      t2 = [[7, 8, 9], [10, 11, 12]]
      output = tf.concat(-2, [t1, t2]).eval()
      self.assertAllEqual(
          [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
          output)
      output = tf.concat(-1, [t1, t2]).eval()
      self.assertAllEqual(
          [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]],
          output)

  def testConcatV2Empty(self):
    with self.test_session(use_gpu=True):
      t1 = []
      t2 = []
      output = gen_array_ops._concat_v2([t1, t2], 0).eval()
      self.assertFalse(output)  # Checks that output is empty

  def testConcatV2InvalidAxis(self):
    with self.assertRaises(ValueError):
      with self.test_session(use_gpu=True):
        t1 = [1]
        t2 = [2]
        gen_array_ops._concat_v2([t1, t2], 1).eval()

  def testConcatV2NegativeAxis(self):
    with self.test_session(use_gpu=True):
      t1 = [[1, 2, 3], [4, 5, 6]]
      t2 = [[7, 8, 9], [10, 11, 12]]
      output = gen_array_ops._concat_v2([t1, t2], -2).eval()
      self.assertAllEqual(
          [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
          output)
      output = gen_array_ops._concat_v2([t1, t2], -1).eval()
      self.assertAllEqual(
          [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]],
          output)

class ConcatOffsetTest(tf.test.TestCase):

  def testBasic(self):
    for use_gpu in [False, True]:
      with self.test_session(use_gpu=use_gpu) as sess:
        cdim = tf.constant(1, tf.int32)
        s0 = tf.constant([2, 3, 5], tf.int32)
        s1 = tf.constant([2, 7, 5], tf.int32)
        s2 = tf.constant([2, 20, 5], tf.int32)
        off = gen_array_ops._concat_offset(cdim, [s0, s1, s2])
        ans = sess.run(off)
        self.assertAllEqual(ans, [[0, 0, 0], [0, 3, 0], [0, 10, 0]])

  def testNotVector(self):
    with self.test_session() as sess:
      cdim = tf.constant(1, tf.int32)
      s0 = tf.constant([[2, 3, 5]], tf.int32)
      s1 = tf.constant([[2, 7, 5]], tf.int32)
      off = gen_array_ops._concat_offset(cdim, [s0, s1])
      with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                   r"should be a vector"):
        sess.run(off)

  def testConcatDimOutOfRange(self):
    with self.test_session() as sess:
      cdim = tf.constant(4, tf.int32)
      s0 = tf.constant([2, 3, 5], tf.int32)
      s1 = tf.constant([2, 7, 5], tf.int32)
      off = gen_array_ops._concat_offset(cdim, [s0, s1])
      with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                   r"Concat dim is out of range: 4 vs. 3"):
        sess.run(off)

  def testDimMismatch(self):
    with self.test_session() as sess:
      cdim = tf.constant(1, tf.int32)
      s0 = tf.constant([2, 3, 5], tf.int32)
      s1 = tf.constant([2, 7, 5, 10], tf.int32)
      off = gen_array_ops._concat_offset(cdim, [s0, s1])
      with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                   r"should contain 3 elem"):
        sess.run(off)

  def testSizeMismatch(self):
    with self.test_session() as sess:
      cdim = tf.constant(1, tf.int32)
      s0 = tf.constant([2, 3, 5], tf.int32)
      s1 = tf.constant([2, 7, 10], tf.int32)
      off = gen_array_ops._concat_offset(cdim, [s0, s1])
      with self.assertRaisesRegexp(
          tf.errors.InvalidArgumentError,
          r"All dimensions except 1 must match. Input 1 has shape \[2 7 10\] "
          r"and doesn't match input 0 with shape \[2 3 5\]."):
        sess.run(off)

if __name__ == "__main__":
  tf.test.main()


text:
using System;

namespace LinFx.Extensions.Guids
{
    /// <summary>
    /// Used to generate Ids.
    /// </summary>
    public interface IGuidGenerator
    {
        /// <summary>
        /// Creates a new <see cref="Guid"/>.
        /// </summary>
        Guid Create();
    }
}


text:
// ======================================================================== //
// Copyright 2009-2017 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

#pragma once

#include "../sys/platform.h"

namespace embree
{
  /* Varying numeric types */
  template<int N>
  struct vfloat
  {
    union { float f[N]; int i[N]; };
    __forceinline const float& operator [](size_t index) const { assert(index < N); return f[index]; }
    __forceinline       float& operator [](size_t index)       { assert(index < N); return f[index]; }
  };

  template<int N>
  struct vdouble
  {
    union { double f[N]; long long i[N]; };
    __forceinline const double& operator [](size_t index) const { assert(index < N); return f[index]; }
    __forceinline       double& operator [](size_t index)       { assert(index < N); return f[index]; }
  };

  template<int N>
  struct vint
  {
    int i[N];
    __forceinline const int& operator [](size_t index) const { assert(index < N); return i[index]; }
    __forceinline       int& operator [](size_t index)       { assert(index < N); return i[index]; }
  };

  template<int N>
  struct vuint
  {
    unsigned int i[N];
    __forceinline const unsigned int& operator [](size_t index) const { assert(index < N); return i[index]; }
    __forceinline       unsigned int& operator [](size_t index)       { assert(index < N); return i[index]; }
  };

  template<int N>
  struct vllong
  {
    long long i[N];
    __forceinline const long long& operator [](size_t index) const { assert(index < N); return i[index]; }
    __forceinline       long long& operator [](size_t index)       { assert(index < N); return i[index]; }
  };

#if !defined(_MSC_VER) || _MSC_VER >= 1800
  /* Varying bool types */
  template<int N> struct vboolf { int       i[N]; }; // for float/int
  template<int N> struct vboold { long long i[N]; }; // for double/long long

  /* Aliases to default types */
  template<int N> using vreal = vfloat<N>;
  template<int N> using vbool = vboolf<N>;
#else
  /* Workaround for VS2012 */
  #define vreal  vfloat
  #define vboolf vbool

  template<int N> struct vboolf { int       i[N]; };
  template<int N> struct vboold { long long i[N]; };
#endif

  /* Maximum supported varying size */
#if defined(__AVX512F__)
  const int VSIZEX = 16;
#elif defined(__AVX__)
  const int VSIZEX = 8;
#else
  const int VSIZEX = 4;
#endif

  /* Extends varying size N to optimal or up to max(N, N2) */
  template<int N, int N2 = VSIZEX>
  struct vextend
  {
#if defined(__AVX512F__) && !defined(__AVX512VL__) // KNL
    /* use 16-wide SIMD calculations on KNL even for 4 and 8 wide SIMD */
    static const int size = (N2 == VSIZEX) ? VSIZEX : N;
    #define SIMD_MODE(N) N, 16
#else
    /* calculate with same SIMD width otherwise */
    static const int size = N;
    #define SIMD_MODE(N) N, N
#endif
  };

  /* 4-wide shortcuts */
  typedef vfloat<4>  vfloat4;
  typedef vdouble<4> vdouble4;
  typedef vreal<4>   vreal4;
  typedef vint<4>    vint4;
  typedef vllong<4>  vllong4;
  typedef vbool<4>   vbool4;
  typedef vboolf<4>  vboolf4;
  typedef vboold<4>  vboold4;

  /* 8-wide shortcuts */
  typedef vfloat<8>  vfloat8;
  typedef vdouble<8> vdouble8;
  typedef vreal<8>   vreal8;
  typedef vint<8>    vint8;
  typedef vllong<8>  vllong8;
  typedef vbool<8>   vbool8;
  typedef vboolf<8>  vboolf8;
  typedef vboold<8>  vboold8;

  /* 16-wide shortcuts */
  typedef vfloat<16>  vfloat16;
  typedef vdouble<16> vdouble16;
  typedef vreal<16>   vreal16;
  typedef vint<16>    vint16;
  typedef vuint<16>   vuint16;
  typedef vllong<16>  vllong16;
  typedef vbool<16>   vbool16;
  typedef vboolf<16>  vboolf16;
  typedef vboold<16>  vboold16;

  /* Maximum shortcuts */
  typedef vfloat<VSIZEX>  vfloatx;
  typedef vdouble<VSIZEX> vdoublex;
  typedef vreal<VSIZEX>   vrealx;
  typedef vint<VSIZEX>    vintx;
  typedef vllong<VSIZEX>  vllongx;
  typedef vbool<VSIZEX>   vboolx;
  typedef vboolf<VSIZEX>  vboolfx;
  typedef vboold<VSIZEX>  vbooldx;
}


text:
namespace base {
namespace internal {

// A SequenceLocalStorageMap holds (slot_id) -> (value, destructor) items for a
// sequence. When a task runs, it is expected that a pointer to its sequence's
// SequenceLocalStorageMap is set in TLS using
// ScopedSetSequenceMapLocalStorageForCurrentThread. When a
// SequenceLocalStorageMap is destroyed, it invokes the destructors associated
// with values stored within it.
// The Get() and Set() methods should not be accessed directly.
// Use SequenceLocalStorageSlot to Get() and Set() values in the current
// sequence's SequenceLocalStorageMap.
class BASE_EXPORT SequenceLocalStorageMap {
 public:
  SequenceLocalStorageMap();
  ~SequenceLocalStorageMap();

  // Returns the SequenceLocalStorage bound to the current thread. It is invalid
  // to call this outside the scope of a
  // ScopedSetSequenceLocalStorageForCurrentThread.
  static SequenceLocalStorageMap& GetForCurrentThread();

  // Holds a pointer to a value alongside a destructor for this pointer.
  // Calls the destructor on the value upon destruction.
  class BASE_EXPORT ValueDestructorPair {
   public:
    using DestructorFunc = void(void*);

    ValueDestructorPair(void* value, DestructorFunc* destructor);
    ~ValueDestructorPair();

    ValueDestructorPair(ValueDestructorPair&& value_destructor_pair);

    ValueDestructorPair& operator=(ValueDestructorPair&& value_destructor_pair);

    void* value() const { return value_; }

   private:
    void* value_;
    DestructorFunc* destructor_;

    DISALLOW_COPY_AND_ASSIGN(ValueDestructorPair);
  };

  // Returns the value stored in |slot_id| or nullptr if no value was stored.
  void* Get(int slot_id);

  // Stores |value_destructor_pair| in |slot_id|. Overwrites and destroys any
  // previously stored value.
  void Set(int slot_id, ValueDestructorPair value_destructor_pair);

 private:
  // Map from slot id to ValueDestructorPair.
  // flat_map was chosen because there are expected to be relatively few entries
  // in the map. For low number of entries, flat_map is known to perform better
  // than other map implementations.
  base::flat_map<int, ValueDestructorPair> sls_map_;

  DISALLOW_COPY_AND_ASSIGN(SequenceLocalStorageMap);
};

// Within the scope of this object,
// SequenceLocalStorageMap::GetForCurrentThread() will return a reference to the
// SequenceLocalStorageMap object passed to the constructor. There can be only
// one ScopedSetSequenceLocalStorageMapForCurrentThread instance per scope.
class BASE_EXPORT ScopedSetSequenceLocalStorageMapForCurrentThread {
 public:
  ScopedSetSequenceLocalStorageMapForCurrentThread(
      SequenceLocalStorageMap* sequence_local_storage);

  ~ScopedSetSequenceLocalStorageMapForCurrentThread();

 private:
  DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceLocalStorageMapForCurrentThread);
};
}  // namespace internal
}  // namespace base

#endif  // BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_


text:
Aria.classDefinition({
    $classpath : "test.aria.utils.overlay.loadingIndicator.zindex.IndexTestCase",
    $extends : "aria.jsunit.TemplateTestCase",
    $prototype : {
        runTemplateTest : function () {
            var document = Aria.$window.document;
            // overlay is last element inserted in DOM
            var bodyChilds = document.body.childNodes;
            var overlay = bodyChilds[bodyChilds.length - 1];
            // IE text nodes
            if (overlay.nodeType != 1) {
                overlay = overlay.previousSibling;
            }
            this.assertTrue(parseInt(overlay.style.zIndex, 10) > 40000, "overlay zIndex makes it non visible");

            this.notifyTemplateTestEnd();
        }
    }
});


text:
import { addQuarters } from 'date-fns'
export default addQuarters


text:
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('news', '0031_auto_20160503_1354'),
    ]

    operations = [
        migrations.AlterField(
            model_name='tweet',
            name='stream',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='news.TwitterStream'),
        ),
    ]


text:
import libCloudSnakeClient as SnakeClient

import pprint, sys

client = SnakeClient.CloudSnakeClient( 'http://localhost:8500', 'main' )

add_node = client.get_method( 'add_node' )

print add_node( node = sys.argv[ 1 ] )


text:
Example Markdown Page
=====================
> This page was rendered using [remarkable](https://github.com/jonschlinkert/remarkable)

**Make Lists**

- one
- two
- three

**Write JavaScript Snippets**

```javascript
var foo = function() {
    return 'foo';
};
```


text:
// qcommon.h -- definitions common between client and server, but not game.dll

#include "../game/q_shared.h"

#define	VERSION		3.21

#define	BASEDIRNAME	"baseq2"

#ifdef WIN32

#ifdef NDEBUG
#define BUILDSTRING "Win32 RELEASE"
#else
#define BUILDSTRING "Win32 DEBUG"
#endif

#ifdef _M_IX86
#define	CPUSTRING	"x86"
#elif defined _M_ALPHA
#define	CPUSTRING	"AXP"
#endif

#elif defined __linux__

#define BUILDSTRING "Linux"

#ifdef __i386__
#define CPUSTRING "i386"
#elif defined __alpha__
#define CPUSTRING "axp"
#else
#define CPUSTRING "Unknown"
#endif

#elif defined __sun__

#define BUILDSTRING "Solaris"

#ifdef __i386__
#define CPUSTRING "i386"
#else
#define CPUSTRING "sparc"
#endif

#else	// !WIN32

#define BUILDSTRING "NON-WIN32"
#define	CPUSTRING	"NON-WIN32"

#endif

//============================================================================

typedef struct sizebuf_s
{
	qboolean	allowoverflow;	// if false, do a Com_Error
	qboolean	overflowed;		// set to true if the buffer size failed
	byte	*data;
	int		maxsize;
	int		cursize;
	int		readcount;
} sizebuf_t;

void SZ_Init (sizebuf_t *buf, byte *data, int length);
void SZ_Clear (sizebuf_t *buf);
void *SZ_GetSpace (sizebuf_t *buf, int length);
void SZ_Write (sizebuf_t *buf, void *data, int length);
void SZ_Print (sizebuf_t *buf, char *data);	// strcats onto the sizebuf

//============================================================================

struct usercmd_s;
struct entity_state_s;

void MSG_WriteChar (sizebuf_t *sb, int c);
void MSG_WriteByte (sizebuf_t *sb, int c);
void MSG_WriteShort (sizebuf_t *sb, int c);
void MSG_WriteLong (sizebuf_t *sb, int c);
void MSG_WriteFloat (sizebuf_t *sb, float f);
void MSG_WriteString (sizebuf_t *sb, char *s);
void MSG_WriteCoord (sizebuf_t *sb, float f);
void MSG_WritePos (sizebuf_t *sb, vec3_t pos);
void MSG_WriteAngle (sizebuf_t *sb, float f);
void MSG_WriteAngle16 (sizebuf_t *sb, float f);
void MSG_WriteDeltaUsercmd (sizebuf_t *sb, struct usercmd_s *from, struct usercmd_s *cmd);
void MSG_WriteDeltaEntity (struct entity_state_s *from, struct entity_state_s *to, sizebuf_t *msg, qboolean force, qboolean newentity);
void MSG_WriteDir (sizebuf_t *sb, vec3_t vector);

void	MSG_BeginReading (sizebuf_t *sb);

int		MSG_ReadChar (sizebuf_t *sb);
int		MSG_ReadByte (sizebuf_t *sb);
int		MSG_ReadShort (sizebuf_t *sb);
int		MSG_ReadLong (sizebuf_t *sb);
float	MSG_ReadFloat (sizebuf_t *sb);
char	*MSG_ReadString (sizebuf_t *sb);
char	*MSG_ReadStringLine (sizebuf_t *sb);

float	MSG_ReadCoord (sizebuf_t *sb);
void	MSG_ReadPos (sizebuf_t *sb, vec3_t pos);
float	MSG_ReadAngle (sizebuf_t *sb);
float	MSG_ReadAngle16 (sizebuf_t *sb);
void	MSG_ReadDeltaUsercmd (sizebuf_t *sb, struct usercmd_s *from, struct usercmd_s *cmd);

void	MSG_ReadDir (sizebuf_t *sb, vec3_t vector);

void	MSG_ReadData (sizebuf_t *sb, void *buffer, int size);

//============================================================================

extern	qboolean		bigendien;

extern	short	BigShort (short l);
extern	short	LittleShort (short l);
extern	int		BigLong (int l);
extern	int		LittleLong (int l);
extern	float	BigFloat (float l);
extern	float	LittleFloat (float l);

//============================================================================

int	COM_Argc (void);
char *COM_Argv (int arg);	// range and null checked
void COM_ClearArgv (int arg);
int COM_CheckParm (char *parm);
void COM_AddParm (char *parm);

void COM_Init (void);
void COM_InitArgv (int argc, char **argv);

char *CopyString (char *in);

//============================================================================

void Info_Print (char *s);

/* crc.h */

void CRC_Init(unsigned short *crcvalue);
void CRC_ProcessByte(unsigned short *crcvalue, byte data);
unsigned short CRC_Value(unsigned short crcvalue);
unsigned short CRC_Block (byte *start, int count);

/*
==============================================================

PROTOCOL

==============================================================
*/

// protocol.h -- communications protocols

#define	PROTOCOL_VERSION	34

//=========================================

#define	PORT_MASTER	27900
#define	PORT_CLIENT	27901
#define	PORT_SERVER	27910

//=========================================

#define	UPDATE_BACKUP	16	// copies of entity_state_t to keep buffered
							// must be power of two
#define	UPDATE_MASK		(UPDATE_BACKUP-1)

//==================
// the svc_strings[] array in cl_parse.c should mirror this
//==================

//
// server to client
//
enum svc_ops_e
{
	svc_bad,

	// these ops are known to the game dll
	svc_muzzleflash,
	svc_muzzleflash2,
	svc_temp_entity,
	svc_layout,
	svc_inventory,

	// the rest are private to the client and server
	svc_nop,
	svc_disconnect,
	svc_reconnect,
	svc_sound,					// <see code>
	svc_print,					// [byte] id [string] null terminated string
	svc_stufftext,				// [string] stuffed into client's console buffer, should be \n terminated
	svc_serverdata,				// [long] protocol ...
	svc_configstring,			// [short] [string]
	svc_spawnbaseline,
	svc_centerprint,			// [string] to put in center of the screen
	svc_download,				// [short] size [size bytes]
	svc_playerinfo,				// variable
	svc_packetentities,			// [...]
	svc_deltapacketentities,	// [...]
	svc_frame
};

//==============================================

//
// client to server
//
enum clc_ops_e
{
	clc_bad,
	clc_nop,
	clc_move,				// [[usercmd_t]
	clc_userinfo,			// [[userinfo string]
	clc_stringcmd			// [string] message
};

//==============================================

// plyer_state_t communication

#define	PS_M_TYPE			(1<<0)
#define	PS_M_ORIGIN			(1<<1)
#define	PS_M_VELOCITY		(1<<2)
#define	PS_M_TIME			(1<<3)
#define	PS_M_FLAGS			(1<<4)
#define	PS_M_GRAVITY		(1<<5)
#define	PS_M_DELTA_ANGLES	(1<<6)

#define	PS_VIEWOFFSET		(1<<7)
#define	PS_VIEWANGLES		(1<<8)
#define	PS_KICKANGLES		(1<<9)
#define	PS_BLEND			(1<<10)
#define	PS_FOV				(1<<11)
#define	PS_WEAPONINDEX		(1<<12)
#define	PS_WEAPONFRAME		(1<<13)
#define	PS_RDFLAGS			(1<<14)

//==============================================

// user_cmd_t communication

// ms and light always sent, the others are optional
#define	CM_ANGLE1 	(1<<0)
#define	CM_ANGLE2 	(1<<1)
#define	CM_ANGLE3 	(1<<2)
#define	CM_FORWARD	(1<<3)
#define	CM_SIDE		(1<<4)
#define	CM_UP		(1<<5)
#define	CM_BUTTONS	(1<<6)
#define	CM_IMPULSE	(1<<7)

//==============================================

// a sound without an ent or pos will be a local only sound
#define	SND_VOLUME		(1<<0)		// a byte
#define	SND_ATTENUATION	(1<<1)		// a byte
#define	SND_POS			(1<<2)		// three coordinates
#define	SND_ENT			(1<<3)		// a short 0-2: channel, 3-12: entity
#define	SND_OFFSET		(1<<4)		// a byte, msec offset from frame start

#define DEFAULT_SOUND_PACKET_VOLUME	1.0
#define DEFAULT_SOUND_PACKET_ATTENUATION 1.0

//==============================================

// entity_state_t communication

// try to pack the common update flags into the first byte
#define	U_ORIGIN1	(1<<0)
#define	U_ORIGIN2	(1<<1)
#define	U_ANGLE2	(1<<2)
#define	U_ANGLE3	(1<<3)
#define	U_FRAME8	(1<<4)		// frame is a byte
#define	U_EVENT		(1<<5)
#define	U_REMOVE	(1<<6)		// REMOVE this entity, don't add it
#define	U_MOREBITS1	(1<<7)		// read one additional byte

// second byte
#define	U_NUMBER16	(1<<8)		// NUMBER8 is implicit if not set
#define	U_ORIGIN3	(1<<9)
#define	U_ANGLE1	(1<<10)
#define	U_MODEL		(1<<11)
#define U_RENDERFX8	(1<<12)		// fullbright, etc
#define	U_EFFECTS8	(1<<14)		// autorotate, trails, etc
#define	U_MOREBITS2	(1<<15)		// read one additional byte

// third byte
#define	U_SKIN8		(1<<16)
#define	U_FRAME16	(1<<17)		// frame is a short
#define	U_RENDERFX16 (1<<18)	// 8 + 16 = 32
#define	U_EFFECTS16	(1<<19)		// 8 + 16 = 32
#define	U_MODEL2	(1<<20)		// weapons, flags, etc
#define	U_MODEL3	(1<<21)
#define	U_MODEL4	(1<<22)
#define	U_MOREBITS3	(1<<23)		// read one additional byte

// fourth byte
#define	U_OLDORIGIN	(1<<24)		// FIXME: get rid of this
#define	U_SKIN16	(1<<25)
#define	U_SOUND		(1<<26)
#define	U_SOLID		(1<<27)

/*
==============================================================

CMD

Command text buffering and command execution

==============================================================
*/

/*

Any number of commands can be added in a frame, from several different sources.
Most commands come from either keybindings or console line input, but remote
servers can also send across commands and entire text files can be execed.

The + command line options are also added to the command buffer.

The game starts with a Cbuf_AddText ("exec quake.rc\n"); Cbuf_Execute ();

*/

#define	EXEC_NOW	0		// don't return until completed
#define	EXEC_INSERT	1		// insert at current position, but don't run yet
#define	EXEC_APPEND	2		// add to end of the command buffer

void Cbuf_Init (void);
// allocates an initial text buffer that will grow as needed

void Cbuf_AddText (char *text);
// as new commands are generated from the console or keybindings,
// the text is added to the end of the command buffer.

void Cbuf_InsertText (char *text);
// when a command wants to issue other commands immediately, the text is
// inserted at the beginning of the buffer, before any remaining unexecuted
// commands.

void Cbuf_ExecuteText (int exec_when, char *text);
// this can be used in place of either Cbuf_AddText or Cbuf_InsertText

void Cbuf_AddEarlyCommands (qboolean clear);
// adds all the +set commands from the command line

qboolean Cbuf_AddLateCommands (void);
// adds all the remaining + commands from the command line
// Returns true if any late commands were added, which
// will keep the demoloop from immediately starting

void Cbuf_Execute (void);
// Pulls off \n terminated lines of text from the command buffer and sends
// them through Cmd_ExecuteString.  Stops when the buffer is empty.
// Normally called once per frame, but may be explicitly invoked.
// Do not call inside a command function!

void Cbuf_CopyToDefer (void);
void Cbuf_InsertFromDefer (void);
// These two functions are used to defer any pending commands while a map
// is being loaded

//===========================================================================

/*

Command execution takes a null terminated string, breaks it into tokens,
then searches for a command or variable that matches the first token.

*/

typedef void (*xcommand_t) (void);

void	Cmd_Init (void);

void	Cmd_AddCommand (char *cmd_name, xcommand_t function);
// called by the init functions of other parts of the program to
// register commands and functions to call for them.
// The cmd_name is referenced later, so it should not be in temp memory
// if function is NULL, the command will be forwarded to the server
// as a clc_stringcmd instead of executed locally
void	Cmd_RemoveCommand (char *cmd_name);

qboolean Cmd_Exists (char *cmd_name);
// used by the cvar code to check for cvar / command name overlap

char 	*Cmd_CompleteCommand (char *partial);
// attempts to match a partial command for automatic command line completion
// returns NULL if nothing fits

int		Cmd_Argc (void);
char	*Cmd_Argv (int arg);
char	*Cmd_Args (void);
// The functions that execute commands get their parameters with these
// functions. Cmd_Argv () will return an empty string, not a NULL
// if arg > argc, so string operations are always safe.

void	Cmd_TokenizeString (char *text, qboolean macroExpand);
// Takes a null terminated string.  Does not need to be /n terminated.
// breaks the string up into arg tokens.

void	Cmd_ExecuteString (char *text);
// Parses a single line of text into arguments and tries to execute it
// as if it was typed at the console

void	Cmd_ForwardToServer (void);
// adds the current command line as a clc_stringcmd to the client message.
// things like godmode, noclip, etc, are commands directed to the server,
// so when they are typed in at the console, they will need to be forwarded.

/*
==============================================================

CVAR

==============================================================
*/

/*

cvar_t variables are used to hold scalar or string variables that can be changed or displayed at the console or prog code as well as accessed directly
in C code.

The user can access cvars from the console in three ways:
r_draworder			prints the current value
r_draworder 0		sets the current value to 0
set r_draworder 0	as above, but creates the cvar if not present
Cvars are restricted from having the same names as commands to keep this
interface from being ambiguous.
*/

extern	cvar_t	*cvar_vars;

cvar_t *Cvar_Get (char *var_name, char *value, int flags);
// creates the variable if it doesn't exist, or returns the existing one
// if it exists, the value will not be changed, but flags will be ORed in
// that allows variables to be unarchived without needing bitflags

cvar_t 	*Cvar_Set (char *var_name, char *value);
// will create the variable if it doesn't exist

cvar_t *Cvar_ForceSet (char *var_name, char *value);
// will set the variable even if NOSET or LATCH

cvar_t 	*Cvar_FullSet (char *var_name, char *value, int flags);

void	Cvar_SetValue (char *var_name, float value);
// expands value to a string and calls Cvar_Set

float	Cvar_VariableValue (char *var_name);
// returns 0 if not defined or non numeric

char	*Cvar_VariableString (char *var_name);
// returns an empty string if not defined

char 	*Cvar_CompleteVariable (char *partial);
// attempts to match a partial variable name for command line completion
// returns NULL if nothing fits

void	Cvar_GetLatchedVars (void);
// any CVAR_LATCHED variables that have been set will now take effect

qboolean Cvar_Command (void);
// called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a known
// command.  Returns true if the command was a variable reference that
// was handled. (print or change)

void 	Cvar_WriteVariables (char *path);
// appends lines containing "set variable value" for all variables
// with the archive flag set to true.

void	Cvar_Init (void);

char	*Cvar_Userinfo (void);
// returns an info string containing all the CVAR_USERINFO cvars

char	*Cvar_Serverinfo (void);
// returns an info string containing all the CVAR_SERVERINFO cvars

extern	qboolean	userinfo_modified;
// this is set each time a CVAR_USERINFO variable is changed
// so that the client knows to send it to the server

/*
==============================================================

NET

==============================================================
*/

// net.h -- quake's interface to the networking layer

#define	PORT_ANY	-1

#define	MAX_MSGLEN		1400		// max length of a message
#define	PACKET_HEADER	10			// two ints and a short

typedef enum {NA_LOOPBACK, NA_BROADCAST, NA_IP, NA_IPX, NA_BROADCAST_IPX} netadrtype_t;

typedef enum {NS_CLIENT, NS_SERVER} netsrc_t;

typedef struct
{
	netadrtype_t	type;

	byte	ip[4];
	byte	ipx[10];

	unsigned short	port;
} netadr_t;

void		NET_Init (void);
void		NET_Shutdown (void);

void		NET_Config (qboolean multiplayer);

qboolean	NET_GetPacket (netsrc_t sock, netadr_t *net_from, sizebuf_t *net_message);
void		NET_SendPacket (netsrc_t sock, int length, void *data, netadr_t to);

qboolean	NET_CompareAdr (netadr_t a, netadr_t b);
qboolean	NET_CompareBaseAdr (netadr_t a, netadr_t b);
qboolean	NET_IsLocalAddress (netadr_t adr);
char		*NET_AdrToString (netadr_t a);
qboolean	NET_StringToAdr (char *s, netadr_t *a);
void		NET_Sleep(int msec);

//============================================================================

#define	OLD_AVG		0.99		// total = oldtotal*OLD_AVG + new*(1-OLD_AVG)

#define	MAX_LATENT	32

typedef struct
{
	qboolean	fatal_error;

	netsrc_t	sock;

	int			dropped;			// between last packet and previous

	int			last_received;		// for timeouts
	int			last_sent;			// for retransmits

	netadr_t	remote_address;
	int			qport;				// qport value to write when transmitting

// sequencing variables
	int			incoming_sequence;
	int			incoming_acknowledged;
	int			incoming_reliable_acknowledged;	// single bit

	int			incoming_reliable_sequence;		// single bit, maintained local

	int			outgoing_sequence;
	int			reliable_sequence;			// single bit
	int			last_reliable_sequence;		// sequence number of last send

// reliable staging and holding areas
	sizebuf_t	message;		// writing buffer to send to server
	byte		message_buf[MAX_MSGLEN-16];		// leave space for header

// message is copied to this buffer when it is first transfered
	int			reliable_length;
	byte		reliable_buf[MAX_MSGLEN-16];	// unacked reliable message
} netchan_t;

extern	netadr_t	net_from;
extern	sizebuf_t	net_message;
extern	byte		net_message_buffer[MAX_MSGLEN];

void Netchan_Init (void);
void Netchan_Setup (netsrc_t sock, netchan_t *chan, netadr_t adr, int qport);

qboolean Netchan_NeedReliable (netchan_t *chan);
void Netchan_Transmit (netchan_t *chan, int length, byte *data);
void Netchan_OutOfBand (int net_socket, netadr_t adr, int length, byte *data);
void Netchan_OutOfBandPrint (int net_socket, netadr_t adr, char *format, ...);
qboolean Netchan_Process (netchan_t *chan, sizebuf_t *msg);

qboolean Netchan_CanReliable (netchan_t *chan);

/*
==============================================================

CMODEL

==============================================================
*/

#include "../qcommon/qfiles.h"

cmodel_t	*CM_LoadMap (char *name, qboolean clientload, unsigned *checksum);
cmodel_t	*CM_InlineModel (char *name);	// *1, *2, etc

int			CM_NumClusters (void);
int			CM_NumInlineModels (void);
char		*CM_EntityString (void);

// creates a clipping hull for an arbitrary box
int			CM_HeadnodeForBox (vec3_t mins, vec3_t maxs);

// returns an ORed contents mask
int			CM_PointContents (vec3_t p, int headnode);
int			CM_TransformedPointContents (vec3_t p, int headnode, vec3_t origin, vec3_t angles);

trace_t		CM_BoxTrace (vec3_t start, vec3_t end,
						  vec3_t mins, vec3_t maxs,
						  int headnode, int brushmask);
trace_t		CM_TransformedBoxTrace (vec3_t start, vec3_t end,
						  vec3_t mins, vec3_t maxs,
						  int headnode, int brushmask,
						  vec3_t origin, vec3_t angles);

byte		*CM_ClusterPVS (int cluster);
byte		*CM_ClusterPHS (int cluster);

int			CM_PointLeafnum (vec3_t p);

// call with topnode set to the headnode, returns with topnode
// set to the first node that splits the box
int			CM_BoxLeafnums (vec3_t mins, vec3_t maxs, int *list,
							int listsize, int *topnode);

int			CM_LeafContents (int leafnum);
int			CM_LeafCluster (int leafnum);
int			CM_LeafArea (int leafnum);

void		CM_SetAreaPortalState (int portalnum, qboolean open);
qboolean	CM_AreasConnected (int area1, int area2);

int			CM_WriteAreaBits (byte *buffer, int area);
qboolean	CM_HeadnodeVisible (int headnode, byte *visbits);

void		CM_WritePortalState (FILE *f);
void		CM_ReadPortalState (FILE *f);

/*
==============================================================

PLAYER MOVEMENT CODE

Common between server and client so prediction matches

==============================================================
*/

extern float pm_airaccelerate;

void Pmove (pmove_t *pmove);

/*
==============================================================

FILESYSTEM

==============================================================
*/

void	FS_InitFilesystem (void);
void	FS_SetGamedir (char *dir);
char	*FS_Gamedir (void);
char	*FS_NextPath (char *prevpath);
void	FS_ExecAutoexec (void);

int		FS_FOpenFile (char *filename, FILE **file);
void	FS_FCloseFile (FILE *f);
// note: this can't be called from another DLL, due to MS libc issues

int		FS_LoadFile (char *path, void **buffer);
// a null buffer will just return the file length without loading
// a -1 length is not present

void	FS_Read (void *buffer, int len, FILE *f);
// properly handles partial reads

void	FS_FreeFile (void *buffer);

void	FS_CreatePath (char *path);

/*
==============================================================

MISC

==============================================================
*/

#define	ERR_FATAL	0		// exit the entire game with a popup window
#define	ERR_DROP	1		// print to console and disconnect from game
#define	ERR_QUIT	2		// not an error, just a normal exit

#define	EXEC_NOW	0		// don't return until completed
#define	EXEC_INSERT	1		// insert at current position, but don't run yet
#define	EXEC_APPEND	2		// add to end of the command buffer

#define	PRINT_ALL		0
#define PRINT_DEVELOPER	1	// only print when "developer 1"

void		Com_BeginRedirect (int target, char *buffer, int buffersize, void (*flush));
void		Com_EndRedirect (void);
void 		Com_Printf (char *fmt, ...);
void 		Com_DPrintf (char *fmt, ...);
void 		Com_Error (int code, char *fmt, ...);
void 		Com_Quit (void);

int			Com_ServerState (void);		// this should have just been a cvar...
void		Com_SetServerState (int state);

unsigned	Com_BlockChecksum (void *buffer, int length);
byte		COM_BlockSequenceCRCByte (byte *base, int length, int sequence);

float	frand(void);	// 0 ti 1
float	crand(void);	// -1 to 1

extern	cvar_t	*developer;
extern	cvar_t	*dedicated;
extern	cvar_t	*host_speeds;
extern	cvar_t	*log_stats;

extern	FILE *log_stats_file;

// host_speeds times
extern	int		time_before_game;
extern	int		time_after_game;
extern	int		time_before_ref;
extern	int		time_after_ref;

void Z_Free (void *ptr);
void *Z_Malloc (int size);			// returns 0 filled memory
void *Z_TagMalloc (int size, int tag);
void Z_FreeTags (int tag);

void Qcommon_Init (int argc, char **argv);
void Qcommon_Frame (int msec);
void Qcommon_Shutdown (void);

#define NUMVERTEXNORMALS	162
extern	vec3_t	bytedirs[NUMVERTEXNORMALS];

// this is in the client code, but can be used for debugging from server
void SCR_DebugGraph (float value, int color);

/*
==============================================================

NON-PORTABLE SYSTEM SERVICES

==============================================================
*/

void	Sys_Init (void);

void	Sys_AppActivate (void);

void	Sys_UnloadGame (void);
void	*Sys_GetGameAPI (void *parms);
// loads the game dll and calls the api init function

char	*Sys_ConsoleInput (void);
void	Sys_ConsoleOutput (char *string);
void	Sys_SendKeyEvents (void);
void	Sys_Error (char *error, ...);
void	Sys_Quit (void);
char	*Sys_GetClipboardData( void );
void	Sys_CopyProtect (void);

/*
==============================================================

CLIENT / SERVER SYSTEMS

==============================================================
*/

void CL_Init (void);
void CL_Drop (void);
void CL_Shutdown (void);
void CL_Frame (int msec);
void Con_Print (char *text);
void SCR_BeginLoadingPlaque (void);

void SV_Init (void);
void SV_Shutdown (char *finalmsg, qboolean reconnect);
void SV_Frame (int msec);