text:
package refactor

import (
	"encoding/xml"
	"reflect"
	"testing"
)

func TestChangeSet_UnmarshalXML(t *testing.T) {
	source := `
		<ChangeSet id="id" name="name" author="author">
			<RawSql></RawSql>

			<!-- comment should be ignored -->
			<! directive should be ignored >
			<?proc inst should be ignored?>

			<DoesNotExist /> <!-- ignore things that do no exist in our definition -->

			<RawSql></RawSql>
		</ChangeSet>
	`

	cs := &ChangeSet{}
	decodeSourceIntoValue(t, cs, source)

	if !reflect.DeepEqual(cs.XMLName, xml.Name{"", "ChangeSet"}) {
		t.Fail()
	}
	if cs.Id != "id" || *cs.Name != "name" || *cs.Author != "author" {
		t.Fail()
	}
	if !reflect.DeepEqual(cs.changes, []ChangeStmts{
		&RawSql{XMLName: xml.Name{Local: "RawSql"}},
		&RawSql{XMLName: xml.Name{Local: "RawSql"}},
	}) {
		t.Fail()
	}
}

func TestChangeSet_UnmarshalXML_errorUnknownTokenType(t *testing.T) {
	source := `
		<ChangeSet id="id" name="name" author="author">
			<RawSql>raw</RawSql>
			<? proc inst invalid ?>
		</ChangeSet>
	`

	cs := &ChangeSet{}
	err := decodeSourceIntoValueError(t, cs, source)

	if err != errUnknownTokenType {
		t.Fail()
	}
}

func TestChangeSet_UnmarshalXML_errorInvalidChanger(t *testing.T) {
}


text:
// src/polyfills.js must be the first import.
import '../polyfills';

import {draw3p, init} from '../integration-lib';
import {register} from '../3p';

import {smartadserver} from '../../ads/vendors/smartadserver';

init(window);
register('smartadserver', smartadserver);

window.draw3p = draw3p;

redpajama_set_name: RedPajamaGithub


text:
require 'test_helper'

class GamesControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should get create" do
    get :create
    assert_response :success
  end

end


text: Hudson Wilder email capture for preview launch


text:
using Orchard.ContentManagement.Records;

namespace Orchard.Search.Models {
    public class SearchSettingsPartRecord : ContentPartRecord {
        public virtual bool FilterCulture { get; set; }
        public virtual string SearchedFields { get; set; }

        public SearchSettingsPartRecord() {
            FilterCulture = false;
            SearchedFields = "body, title";
        }
    }
}


text:
use std::collections::HashSet;
use std::sync::{Mutex, Arc, Barrier};
use std::sync::atomic::{AtomicBool, Ordering};

use std::thread;

struct CounterInternal {
    numbers: Vec<i32>,
    threads_name: HashSet<Option<String>>
}

impl CounterInternal {

    fn new() -> CounterInternal {
        CounterInternal {
            numbers: Vec::new(),
            threads_name: HashSet::new()
        }
    }

    fn numbers(&self) -> HashSet<i32> {
        self.numbers.iter().cloned().collect::<HashSet<i32>>()
    }

    fn threads(&self) -> usize {
        self.threads_name.len()
    }

    fn count(&mut self, n: i32) {
        let thread_name = thread::current().name().map(|n| n.to_owned());
        self.threads_name.insert(thread_name);
        self.numbers.push(n);
    }
}

#[derive(Clone)]
pub struct Counter {
    internal: Arc<Mutex<CounterInternal>>
}

impl Counter {

    pub fn new() -> Counter {
        Counter {
            internal: Arc::new(Mutex::new(CounterInternal::new()))
        }
    }

    pub fn numbers(&self) -> HashSet<i32> {
        let guard = self.internal.lock().unwrap();
        guard.numbers()
    }

    pub fn threads(&self) -> usize {
        let guard = self.internal.lock().unwrap();
        guard.threads()
    }

    fn count(&self, n: i32) {
        let mut guard = self.internal.lock().unwrap();
        guard.count(n);
    }
}

pub struct ThreadCounter {
    number_of_threads: usize,
    limit: usize
}

impl ThreadCounter {

    pub fn new(number_of_threads: usize, limit: usize) -> ThreadCounter {
        ThreadCounter {
            number_of_threads: number_of_threads,
            limit: limit
        }
    }

    #[allow(unused_must_use)]
    pub fn count_in_threads(&self, counter: &Counter) {
        let mut flags = Vec::with_capacity(self.number_of_threads);
        for _ in 0..self.number_of_threads {
            flags.push(Arc::new(AtomicBool::new(false)));
        }
        flags[0].store(true, Ordering::SeqCst);
        let barrier = Arc::new(Barrier::new(self.number_of_threads+1));
        for i in 0..self.number_of_threads {
            let counter = counter.clone();
            let barrier = barrier.clone();
            let start = i+1;
            let limit = self.limit as i32;
            let increment = self.number_of_threads as i32;
            let allow_flag = flags[i % self.number_of_threads].clone();
            let readiness_flag = flags[(i+1) % self.number_of_threads].clone();
            let thread_name = format!("Thread-{}", i);
            thread::Builder::new().name(thread_name).spawn(
                move || {
                    let mut current = start as i32;
                    while current <= limit {
                        while !allow_flag.load(Ordering::SeqCst) {
                        }
                        counter.count(current);
                        allow_flag.store(false, Ordering::SeqCst);
                        readiness_flag.store(true, Ordering::SeqCst);
                        current += increment;
                    }
                    barrier.wait();
                }
            );
        }
        barrier.wait();
    }
}


text: Tests should be here, but we don't have any by now :(


text:
{% extends "tech/base.html" %}

{% block main %}

            Choose category
            <md-menu md-offset="0 40">

                <md-button ng-click="$mdOpenMenu($event)" class="md-raised" aria-label="Open sample menu">

                    {$ selected_category.name $}
                </md-button>
                <md-menu-content>
                    <md-menu-item ng-repeat="cat in categories">
                        <md-button ng-click="get_properties_for_categories(cat)">{$ cat.name $}</md-button>
                    </md-menu-item>
                </md-menu-content>
            </md-menu>

                mode_edit

                    Property Name
                    Property Type

{$ prop.property_name $}

                        <md-input-container ng-show="edit_mode" >

                            <input type="text" ng-model="prop.property_name" >
                        </md-input-container>

                    <p ng-show="!edit_mode && ($index <= original_prop_length -1)">{$ prop.property_type $}

 original_prop_length - 1)">
                            <select ng-model="prop.property_type">
                                <option value="BOOL">BOOL</option>
                                <option value="INT">INT</option>
                                <option value="STRING">STRING</option>
                            </select>

                add_circle_outline
                save
                cancel

{% endblock %}


text:
//$Id: AssociationType.java 7017 2005-06-05 04:31:34Z oneovthafew $
package org.hibernate.type;

import org.hibernate.MappingException;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.persister.entity.Joinable;

import java.util.Map;

/**
 * A type that represents some kind of association between entities.
 * @see org.hibernate.engine.Cascade
 * @author Gavin King
 */
public interface AssociationType extends Type {

	/**
	 * Get the foreign key directionality of this association
	 */
	public ForeignKeyDirection getForeignKeyDirection();

	//TODO: move these to a new JoinableType abstract class,
	//extended by EntityType and PersistentCollectionType:

	/**
	 * Is the primary key of the owning entity table
	 * to be used in the join?
	 */
	public boolean useLHSPrimaryKey();
	/**
	 * Get the name of a property in the owning entity
	 * that provides the join key (null if the identifier)
	 */
	public String getLHSPropertyName();

	/**
	 * The name of a unique property of the associated entity
	 * that provides the join key (null if the identifier of
	 * an entity, or key of a collection)
	 */
	public String getRHSUniqueKeyPropertyName();

	/**
	 * Get the "persister" for this association - a class or
	 * collection persister
	 */
	public Joinable getAssociatedJoinable(SessionFactoryImplementor factory) throws MappingException;

	/**
	 * Get the entity name of the associated entity
	 */
	public String getAssociatedEntityName(SessionFactoryImplementor factory) throws MappingException;

	/**
	 * Get the "filtering" SQL fragment that is applied in the
	 * SQL on clause, in addition to the usual join condition
	 */
	public String getOnCondition(String alias, SessionFactoryImplementor factory, Map enabledFilters)
	throws MappingException;

	/**
	 * Do we dirty check this association, even when there are
	 * no columns to be updated?
	 */
	public abstract boolean isAlwaysDirtyChecked();

	public boolean isEmbeddedInXML();
}


text:
#include <iostream>
#include <string>
#include <algorithm>

#include "lib3mf_dynamic.hpp"

using namespace Lib3MF;

void printVersion(PWrapper wrapper) {
	Lib3MF_uint32 nMajor, nMinor, nMicro;
	wrapper->GetLibraryVersion(nMajor, nMinor, nMicro);
	std::cout << "lib3mf version = " << nMajor << "." << nMinor << "." << nMicro;
	std::string sReleaseInfo, sBuildInfo;
	if (wrapper->GetPrereleaseInformation(sReleaseInfo)) {
		std::cout << "-" << sReleaseInfo;
	}
	if (wrapper->GetBuildInformation(sBuildInfo)) {
		std::cout << "+" << sBuildInfo;
	}
	std::cout << std::endl;
}

void ShowThumbnailInformation(PModel model)
{
	/*
	// TODO: this is not yet implemented in lib3mf
	*/
}

void ShowMetaDataInformation(PMetaDataGroup metaDataGroup)
{
	Lib3MF_uint32 nMetaDataCount = metaDataGroup->GetMetaDataCount();

	for (Lib3MF_uint32 iMeta = 0; iMeta < nMetaDataCount; iMeta++) {

		PMetaData metaData = metaDataGroup->GetMetaData(iMeta);
		std::string sMetaDataValue = metaData->GetValue();
		std::string sMetaDataName = metaData->GetName();
		std::cout << "Metadatum: " << iMeta << ":" << std::endl;
		std::cout << "Name  = \"" << sMetaDataName << "\"" << std::endl;
		std::cout << "Value = \"" << sMetaDataValue << "\"" << std::endl;
	}
}

void ShowSliceStack(PSliceStack sliceStack, std::string indent)
{
	std::cout << indent << "SliceStackID:  " << sliceStack->GetResourceID() << std::endl;
	if (sliceStack->GetSliceCount() > 0) {
		std::cout << indent << "  Slice count:  " << sliceStack->GetSliceCount() << std::endl;
	}
	if (sliceStack->GetSliceRefCount() > 0) {
		std::cout << indent << "  Slice ref count:  " << sliceStack->GetSliceRefCount() << std::endl;
		for (Lib3MF_uint64 iSliceRef = 0; iSliceRef < sliceStack->GetSliceRefCount(); iSliceRef++) {
			std::cout << indent << "  Slice ref :  " << sliceStack->GetSliceStackReference(iSliceRef)->GetResourceID() << std::endl;
		}
	}
}

void ShowObjectProperties(PObject object)
{
	std::cout << "   Name:            \"" << object->GetName() << "\"" << std::endl;
	std::cout << "   PartNumber:      \"" << object->GetPartNumber() << "\"" << std::endl;

	switch (object->GetType()) {
	case eObjectType::Model:
		std::cout << "   Object type:     model" << std::endl;
		break;
	case eObjectType::Support:
		std::cout << "   Object type:     support" << std::endl;
		break;
	case eObjectType::SolidSupport:
		std::cout << "   Object type:     solidsupport" << std::endl;
		break;
	case eObjectType::Other:
		std::cout << "   Object type:     other" << std::endl;
		break;
	default:
		std::cout << "   Object type:     invalid" << std::endl;
		break;
	}

	if (object->HasSlices(false)) {
		PSliceStack sliceStack = object->GetSliceStack();
		ShowSliceStack(sliceStack, "   ");
	}

	if (object->GetMetaDataGroup()->GetMetaDataCount() > 0) {
		ShowMetaDataInformation(object->GetMetaDataGroup());
	}
}

void ShowMeshObjectInformation(PMeshObject meshObject)
{
	std::cout << "mesh object #" << meshObject->GetResourceID() << ": " << std::endl;

	ShowObjectProperties(meshObject);

	Lib3MF_uint64 nVertexCount = meshObject->GetVertexCount();
	Lib3MF_uint64 nTriangleCount = meshObject->GetTriangleCount();
	PBeamLattice beamLattice = meshObject->BeamLattice();

	// Output data
	std::cout << "   Vertex count:    " << nVertexCount << std::endl;
	std::cout << "   Triangle count:  " << nTriangleCount << std::endl;

	Lib3MF_uint64 nBeamCount = beamLattice->GetBeamCount();
	if (nBeamCount > 0) {
		std::cout << "   Beam count:  " << nBeamCount << std::endl;
		Lib3MF_uint32 nRepresentationMesh;
		if (beamLattice->GetRepresentation(nRepresentationMesh))
			std::cout << "   |_Representation Mesh ID:  " << nRepresentationMesh << std::endl;
		eLib3MFBeamLatticeClipMode eClipMode;
		Lib3MF_uint32 nClippingMesh;
		beamLattice->GetClipping(eClipMode, nClippingMesh);
		if (eClipMode != eBeamLatticeClipMode::NoClipMode)
			std::cout << "   |_Clipping Mesh ID:  " << nClippingMesh << "(mode=" << (int)eClipMode << ")" << std::endl;
		if (beamLattice->GetBeamSetCount() > 0) {
			std::cout << "   |_BeamSet count:  " << beamLattice->GetBeamSetCount() << std::endl;
		}
	}

}

void ShowTransform(sLib3MFTransform transform, std::string indent) {
	std::cout << indent << "Transformation:  [ " << transform.m_Fields[0][0] << " " << transform.m_Fields[1][0] << " " << transform.m_Fields[2][0] << " " << transform.m_Fields[3][0] << " ]" << std::endl;
	std::cout << indent << "                 [ " << transform.m_Fields[0][1] << " " << transform.m_Fields[1][1] << " " << transform.m_Fields[2][1] << " " << transform.m_Fields[3][1] << " ]" << std::endl;
	std::cout << indent << "                 [ " << transform.m_Fields[0][2] << " " << transform.m_Fields[1][2] << " " << transform.m_Fields[2][2] << " " << transform.m_Fields[3][2] << " ]" << std::endl;
}

void ShowComponentsObjectInformation(PComponentsObject componentsObject)
{
	std::cout << "components object #" << componentsObject->GetResourceID() << ": " << std::endl;

	ShowObjectProperties(componentsObject);
	std::cout << "   Component count:    " << componentsObject->GetComponentCount() << std::endl;
	for (Lib3MF_uint32 nIndex = 0; nIndex < componentsObject->GetComponentCount(); nIndex++) {
		PComponent component = componentsObject->GetComponent(nIndex);

		std::cout << "   Component " << nIndex << ":    Object ID:   " << component->GetObjectResourceID() << std::endl;
		if (component->HasTransform()) {
			ShowTransform(component->GetTransform(), "                   ");
		}
		else {
			std::cout << "                   Transformation:  none" << std::endl;
		}
	}
}

void ExtractInfoExample(std::string sFileName) {
	std::cout << "------------------------------------------------------------------" << std::endl;
	std::cout << "3MF Read example" << std::endl;

	std::string libpath = ("."); // TODO: put the location of the Lib3MF-library file here.
	auto wrapper = Lib3MF::CWrapper::loadLibrary(libpath + "/lib3mf" + LSUFFIXOUT); // TODO: add correct suffix of the library

	printVersion(wrapper);
	std::cout << "------------------------------------------------------------------" << std::endl;

	PModel model = wrapper->CreateModel();

	// Import Model from 3MF File
	{
		PReader reader = model->QueryReader("3mf");
		// And deactivate the strict mode (default is "false", anyway. This just demonstrates where/how to use it).
		reader->SetStrictModeActive(false);
		reader->ReadFromFile(sFileName);

		for (Lib3MF_uint32 iWarning = 0; iWarning < reader->GetWarningCount(); iWarning++) {
			Lib3MF_uint32 nErrorCode;
			std::string sWarningMessage = reader->GetWarning(iWarning, nErrorCode);
			std::cout << "Encountered warning #" << nErrorCode << " : " << sWarningMessage << std::endl;
		}
	}
	ShowThumbnailInformation(model);

	ShowMetaDataInformation(model->GetMetaDataGroup());

	PSliceStackIterator sliceStacks = model->GetSliceStacks();
	while (sliceStacks->MoveNext()) {
		PSliceStack sliceStack = sliceStacks->GetCurrentSliceStack();
		ShowSliceStack(sliceStack, "");
	}

	PObjectIterator objectIterator = model->GetObjects();
	while (objectIterator->MoveNext()) {
		PObject object = objectIterator->GetCurrentObject();
		if (object->IsMeshObject()) {
			ShowMeshObjectInformation(model->GetMeshObjectByID(object->GetResourceID()));
		}
		else if (object->IsComponentsObject()) {
			ShowComponentsObjectInformation(model->GetComponentsObjectByID(object->GetResourceID()));
		}
		else {
			std::cout << "unknown object #" << object->GetResourceID() << ": " << std::endl;
		}
	}

	PBuildItemIterator buildItemIterator = model->GetBuildItems();
	while (buildItemIterator->MoveNext()) {
		PBuildItem buildItem = buildItemIterator->GetCurrent();

		std::cout << "Build item (Object #" << buildItem->GetObjectResourceID() << "): " << std::endl;

		if (buildItem->HasObjectTransform()) {
			ShowTransform(buildItem->GetObjectTransform(), "   ");
		}
		else {
			std::cout << "   Transformation:  none" << std::endl;
		}
		std::cout << "   Part number:     \"" << buildItem->GetPartNumber() << "\"" << std::endl;
		if (buildItem->GetMetaDataGroup()->GetMetaDataCount() > 0) {
			ShowMetaDataInformation(buildItem->GetMetaDataGroup());
		}
	}

	std::cout << "done" << std::endl;
}

int main(int argc, char** argv) {
	// Parse Arguments
	if (argc != 2) {
		std::cout << "Usage: " << std::endl;
		std::cout << "ExtractInfo.exe model.3mf" << std::endl;
		return 0;
	}

	try {
		ExtractInfoExample(argv[1]);
	}
	catch (ELib3MFException &e) {
		std::cout << e.what() << std::endl;
		return e.getErrorCode();
	}
	return 0;
}


text:
namespace gitstatus {

// If the pointers are null, prints nothing.
//
// Requires: !begin == !end.
void CEscape(std::ostream& strm, const char* begin, const char* end);

// If the pointers are null, prints null without quotes.
//
// Requires: !begin == !end.
void Quote(std::ostream& strm, const char* begin, const char* end);

}  // namespace gitstatus

#endif  // ROMKATV_GITSTATUS_STRING_VIEW_H_


text:
package com.jdk.reference;

//: containers/References.java
// Demonstrates Reference objects
import java.lang.ref.*;
import java.util.*;

class VeryBig {

	private static final int SIZE = 10000;

	private long[] la = new long[SIZE];

	private String ident;

	public VeryBig(String id) {
		ident = id;
	}

	public String toString() {
		return ident;
	}

	protected void finalize() {
		System.out.println("Finalizing " + ident);
	}
}

public class References {

	private static ReferenceQueue<VeryBig> rq = new ReferenceQueue<VeryBig>();

	public static void checkQueue() {
		Reference<? extends VeryBig> inq = rq.poll();
		if (inq != null) {
			System.out.println("In queue: " + inq.get());
		} else {
			System.out.println(inq);
		}
	}

	public static void main(String[] args) throws InterruptedException {
		int size = 10;
		// Or, choose size via the command line:
		if (args.length > 0)
			size = new Integer(args[0]);
		LinkedList<SoftReference<VeryBig>> sa = new LinkedList<SoftReference<VeryBig>>();
		for (int i = 0; i < size; i++) {
			sa.add(new SoftReference<VeryBig>(new VeryBig("Soft " + i), rq));
			System.out.println("Just created: " + sa.getLast());
			checkQueue();
		}
		System.out.println("=======================================");
		LinkedList<WeakReference<VeryBig>> wa = new LinkedList<WeakReference<VeryBig>>();
		for (int i = 0; i < size; i++) {
			wa.add(new WeakReference<VeryBig>(new VeryBig("Weak " + i), rq));
			System.out.println("Just created: " + wa.getLast());
			checkQueue();
		}
		SoftReference<VeryBig> s = new SoftReference<VeryBig>(new VeryBig("Soft"));
		WeakReference<VeryBig> w = new WeakReference<VeryBig>(new VeryBig("Weak"));
		System.out.println("=============== gc() =====================");
		System.gc();
		System.out.println("============= after gc =================");
		System.out.println(s.get());
		System.out.println(w.get());
		LinkedList<PhantomReference<VeryBig>> pa = new LinkedList<PhantomReference<VeryBig>>();
		for (int i = 0; i < size; i++) {
			pa.add(new PhantomReference<VeryBig>(new VeryBig("Phantom " + i), rq));
			System.out.println("Just created: " + pa.getLast());
			checkQueue();
		}
		Thread.sleep(3000);
	}
} /* (Execute to see output) */// :~
/**
 * Just created: java.lang.ref.SoftReference@659e0bfd null Just created:
 * java.lang.ref.SoftReference@2a139a55 null Just created:
 * java.lang.ref.SoftReference@15db9742 null Just created:
 * java.lang.ref.SoftReference@6d06d69c null Just created:
 * java.lang.ref.SoftReference@7852e922 null Just created:
 * java.lang.ref.SoftReference@4e25154f null Just created:
 * java.lang.ref.SoftReference@70dea4e null Just created:
 * java.lang.ref.SoftReference@5c647e05 null Just created:
 * java.lang.ref.SoftReference@33909752 null Just created:
 * java.lang.ref.SoftReference@55f96302 null Just created:
 * java.lang.ref.WeakReference@3d4eac69 null Just created:
 * java.lang.ref.WeakReference@42a57993 null Just created:
 * java.lang.ref.WeakReference@75b84c92 null Just created:
 * java.lang.ref.WeakReference@6bc7c054 null Just created:
 * java.lang.ref.WeakReference@232204a1 null Just created:
 * java.lang.ref.WeakReference@4aa298b7 null Just created:
 * java.lang.ref.WeakReference@7d4991ad null Just created:
 * java.lang.ref.WeakReference@28d93b30 null Just created:
 * java.lang.ref.WeakReference@1b6d3586 null Just created:
 * java.lang.ref.WeakReference@4554617c null =============== gc()
 * ===================== Finalizing Weak Finalizing Weak 9 Finalizing Weak 8
 * Finalizing Weak 7 Finalizing Weak 6 Finalizing Weak 5 Finalizing Weak 4
 * Finalizing Weak 3 Finalizing Weak 2 Finalizing Weak 1 Finalizing Weak 0 Just
 * created: java.lang.ref.PhantomReference@74a14482 In queue: null Just created:
 * java.lang.ref.PhantomReference@1540e19d In queue: null Just created:
 * java.lang.ref.PhantomReference@677327b6 In queue: null Just created:
 * java.lang.ref.PhantomReference@14ae5a5 In queue: null Just created:
 * java.lang.ref.PhantomReference@7f31245a In queue: null Just created:
 * java.lang.ref.PhantomReference@6d6f6e28 In queue: null Just created:
 * java.lang.ref.PhantomReference@135fbaa4 In queue: null Just created:
 * java.lang.ref.PhantomReference@45ee12a7 In queue: null Just created:
 * java.lang.ref.PhantomReference@330bedb4 In queue: null Just created:
 * java.lang.ref.PhantomReference@2503dbd3 In queue: null
 */


text:
<?php

namespace Skinny\Application\Response\Http\Header;

/**
 * Header response type
 *
 * @author Mario <mwintoch@confido.pl>
 */
class ContentType extends HeaderBase {

    /**
     * Constructor
     *
     * @param string $contentType
     */
    public function __construct($contentType) {
        parent::__construct();

        $this->_name  = 'Content-Type';
        $this->_value = $contentType;
    }

    /**
     * Factory for JSON content type,
     * most used in AJAX
     *
     * @return ContentType
     */
    public static function JSON() {
        return new ContentType('application/json');
    }

    /**
     * Factory for text plain content type
     *
     * @return ContentType
     */
    public static function PLAIN() {
        return new ContentType('text/plain');
    }

    /**
     * Factory for text event stream content type,
     * used in Server-Sent Events (SSE)
     *
     * @return ContentType
     */
    public static function EVENT_STREAM() {
        return new ContentType('text/event-stream');
    }

}


text:
<?php
/**
 * Utils.class.php
 *
 * Path: /lib/utils/Utils.class.php
 *
 * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
 * @package utils
 */

/**
 * Utils class
 *
 * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
 */
class Utils {
    function seemsUTF8($str) {
        if (is_array($str)) { // This should be removed and replaced by iterative check, but works
            return true;
        }
        $length = strlen($str);
        for ($i=0; $i < $length; $i++) {
            $c = ord($str[$i]);
            if ($c < 0x80) $n = 0; # 0bbbbbbb
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) {
                    return false;
                }
            }
        }
        return true;
    }

    // Alternative to utf8_encode()
    function removeNonUTF8Characters ($string) {
        $string = iconv('UTF-8', 'UTF-8//IGNORE', $string);
        return ($string);
    }

    /**
     * Translates a camel case string into a string with underscores (e.g. firstName -> first_name)
     * @param    string   $str    String in camel case format
     * @return    string            $str Translated into underscore format
     */
    public static function fromCamelCase($str) {
        $str[0] = strtolower($str[0]);
        $func = create_function('$c', 'return "_" . strtolower($c[1]);');
        return preg_replace_callback('/([A-Z])/', $func, $str);
    }

    /**
     * Translates a string with underscores into camel case (e.g. first_name -> firstName)
     * @param    string   $str                     String in underscore format
     * @param    bool     $capitalise_first_char   If true, capitalise the first char in $str
     * @return   string                              $str translated into camel caps
     */
    public static function toCamelCase($str, $capitalise_first_char = false) {
        if($capitalise_first_char) {
            $str[0] = strtoupper($str[0]);
        }
        $func = create_function('$c', 'return strtoupper($c[1]);');
        return preg_replace_callback('/_([a-z])/', $func, $str);
    }

    /**
     * Returns a random id after taking into account the priority of each element.
     * $arrayWithPriority format example:
     *          array(array(1 => 5.4),
     *                array(2 => 10.2),
     *                array(3 => 4.21),
     *                array(4 => 17.33),
     *                array(5 => 7.99),
     *               );
     *
     * @param   array   $arrayWithPriority
     * @param   int     $maximumValue
     * @return  int
     */
    public static function getArrayWithPriority($arrayWithPriority, $maximumPriority = 10) {
        $cache = Caching::getInstance();
        $cacheID = 'Utils_getArrayWithPriority_'.md5(serialize($arrayWithPriority)).'_'.$maximumPriority;

        if ($cachedData = $cache->get($cacheID)) {
            return $cachedData;
        } else {
            if ($arrayWithPriority) {
                $minimum = PHP_INT_MAX;
                $maximum = 0;

                foreach($arrayWithPriority as $row) {
                    if ($minimum > $row) {
                        $minimum = $row;
                    }
                    if ($maximum < $row) {
                        $maximum = $row;
                    }
                }
                $augmentedArray = array();

                foreach($arrayWithPriority as $key => $row) {
                    // It works, believe me, don't mess with it!!!
                    $normalizedPriority = 1;
                    if (($maximum-$minimum) > 0){
                        $normalizedPriority = intval((($row-$minimum)/($maximum-$minimum)) * ($maximumPriority - 1)) + 1;
                    } else {
                        $normalizedPriority = $maximumPriority / 2;
                    }

                    for ($i = 0; $i < $normalizedPriority; $i++) {
                        $augmentedArray[] = $key;
                    }
                }

                $cache->save($cacheID, $augmentedArray, rand(55, 70)); // About 1 minute
                return $augmentedArray;
            } else {
                $cache->save($cacheID, 0, rand(55, 70) * 10); // About 10 minutes
                return 0;
            }
        }
    }

    public static function getRandomArrayIDWithPriority($arrayWithPriority, $maximumPriority = 10) {
        $augmentedArray = self::getArrayWithPriority($arrayWithPriority, $maximumPriority);

        return $augmentedArray[array_rand($augmentedArray)];
    }

    // Parameters can be arrays of strings or strings!!!
    public static function aproximateCompare($firstArray, $secondArray) {
        $cache = Caching::getInstance();
        $cacheID = 'Utils_aproximateCompare_'.md5(serialize($firstArray).serialize($secondArray));

        if ($cachedData = $cache->get($cacheID)) {
            return $cachedData;
        } else {
            if (is_string($firstArray)) {
                $firstArray = array($firstArray);
            } elseif (is_array($firstArray)) {
            } else {
                return 0;
            }

            if (is_string($secondArray)) {
                $secondArray = array($secondArray);
            } elseif (is_array($secondArray)) {
            } else {
                return 0;
            }

            $equivalence = 0;
            foreach ($firstArray as $first) {
                foreach ($secondArray as $second) {
                    // Test real equivalence all lowercase
                    if (strtolower($first) == strtolower($second)) {
                        $equivalence = 100;
                        break(2);
                    } else {
                        $length = strlen($first);
                        if (strlen($second) < $length) {
                            $length = strlen($second);
                        }

                        $firstSub = substr($first,0,$length);
                        $secondSub = substr($second,0,$length);

                        similar_text(strtolower($firstSub), strtolower($secondSub), $similarity);

                        if ($similarity == 100) {
                            $equivalence = 100;
                            break(2);
                        } elseif ($similarity > $equivalence) {
                            $equivalence = $similarity;
                        }
                    }
                }
            }

            $cache->save($cacheID, $equivalence, rand(55, 70) * 15); // About 15 minutes
            return $equivalence;
        }
    }

    // Direct alternative for file_get_contents();
    public static function getUrlContent($url, $timeout = 2, $force = false, $getCurlInfo = false) {
        // Avoid sending postbacks when in dev
        if (gethostname() == 'YDSERVER-LX' && !$force) {
            $log = DebugLog::getInstance();
            $log->debugNonScalarObject($url);

            #echo "Postback: $url
";

            return $url;
        } else {
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout * 2);

            $data = curl_exec($ch);

            if($getCurlInfo === false){
                curl_close($ch);
                return $data;
            }else{
                $curlInfo = curl_getinfo($ch);
                curl_close($ch);
                return array_merge(array("data"=>$data), array("curlInfo"=>$curlInfo));
            }
        }
    }

    /**
     * From: http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
       Example:
            $data = array(
             'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json',
             'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Pearl+Jam&output=json',
             'http://search.yahooapis.com/AudioSearchService/V1/artistSearch?appid=YahooDemo&artist=Pearl+Jam&output=json'
            );
            $r = multiRequest($data);
     *
     * @param type $data
     * @param type $options
     * @return type
     */
    function getMultipleUrlContents($data, $options = array()) {
        // array of curl handles
        $curly = array();
        // data to be returned
        $result = array();

        // multi handle
        $mh = curl_multi_init();

        // loop through $data and create curl handles
        // then add them to the multi-handle
        foreach ($data as $id => $d) {

          $curly[$id] = curl_init();

          $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
          curl_setopt($curly[$id], CURLOPT_URL,            $url);
          curl_setopt($curly[$id], CURLOPT_HEADER,         0);
          curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

          // post?
          if (is_array($d)) {
            if (!empty($d['post'])) {
              curl_setopt($curly[$id], CURLOPT_POST,       1);
              curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
            }
          }

          // extra options?
          if (!empty($options)) {
            curl_setopt_array($curly[$id], $options);
          }

          curl_multi_add_handle($mh, $curly[$id]);
        }

        // execute the handles
        $running = null;
        do {
          curl_multi_exec($mh, $running);
        } while($running > 0);

        // get content and remove handles
        foreach($curly as $id => $c) {
          $result[$id] = curl_multi_getcontent($c);
          curl_multi_remove_handle($mh, $c);
        }

        // all done
        curl_multi_close($mh);

        return $result;
    }

    // Splits string at the nth occurrence of a character
    function splitStringAtNth($string, $needle, $nth){
        $max = strlen($string);
        $n = 0;
        for ($i = 0; $i < $max; $i++) {
            if ($string[$i] == $needle) {
                $n++;
                if($n >= $nth) {
                    break;
                }
            }
        }
        /*$arr[] = substr($string, 0, $i);
        $arr[] = substr($string, $i+1, $max);

        return $arr;*/

        $str = substr($string, 0, $i);

        return $str;
    }

    public static function compileParameterStringFromArray($parameters) {
        if (is_array($parameters)) {
            $parametersStep = array();
            foreach ($parameters as $parameterKey => $parameterValue) {
                if ($parameterKey == 'yd') {
                    $parametersStep[] = $parameterKey.'='.$parameterValue;
                } else {
                    $parametersStep[] = $parameterKey.'='.urlencode($parameterValue);
                }
            }

            $parametersString = join('&', $parametersStep);

            return ($parametersString);
        } else {
            return '';
        }
    }

    public static function compileUrlWithParameters($url, $parameters) {
        $parametersString = '';
        if (is_array($parameters)) {
            $parametersString = self::compileParameterStringFromArray($parameters);
        } else {
            $parametersString = $parameters;
        }

        // If URL already as parameters
        if (strpos($url, '?')) {
            if ((substr($url, -1) == '?') || (substr($url, -1) == '&')) {
                $url = $url.$parametersString;
            } else {
                $url = $url.'&'.$parametersString;
            }
        // If URL does not have parameters
        } else {
            $url = $url.'?'.$parametersString;
        }

        return ($url);
    }

    /**
     * Call url with separated parameters
     *
     * @param   string      $url            URL to call
     * @param   array       $parameters     Parameters to add to URL
     * @param   integer     $timeout        Timeout in seconds
     *
     * @return  string      Url Content
     *
     */
    public static function getUrlWithParameters($url, $parameters, $timeout = 15) {
        $url = self::compileUrlWithParameters ($url, $parameters);

        return self::getUrlContent($url, $timeout);
    }

    /**
     * Check if variable exists before using it
     *
     * @param   array   $var    Variable to check if exists
     * @param   array   $key    Variable key to check if exists
     * @param   array   $safe   What to return when empty
     *
     * @return  mixed   Variable value or '' or $safe
     *
     */
    public static function safeGetVariable($var, $key, $safe = '') {
        if (!empty($var[$key])) {
            $safe = $var[$key];
        }
        return $safe;
    }

    /**
     * Truncate text
     *
     * @author  Chirp Internet: www.chirp.com.au
     *
     * @param   string      $string     Text to be truncated
     * @param   integer     $limit      Amount of characters we want to have
     * @param   string      $break      Where to break the string
     * @param   string      $pad        What to insert at the end of the string
     *
     * @return  string      truncated text
     *
     */
    public static function truncate($string, $limit, $break=" ", $pad="...") {
        // return with no change if string is shorter than $limit
        if(strlen($string) <= $limit) return $string;

        $string = substr($string, 0, $limit);
        if(false !== ($breakpoint = strrpos($string, $break))) {
            $string = substr($string, 0, $breakpoint);
        }

        return $string . $pad;
    }

    /**
     * Safer substitute to die(), so that errors are not shown in production
     *
     * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
     *
     * @param string|bool $msg Message to show
     */
    public static function abort($msg = NULL) {
        if (!is_null($msg)) {
            if (is_int($msg)) {
                $msg = "$msg";
            } elseif (empty($msg)) {
                $msg = _("abort() called.");
            } elseif (!is_string($msg)) {
                $msg = "$msg";
            }

            if (!ini_get("display_errors")) {
                echo (_("An error occurred. Please report timestamp '").gmdate('c')._("' to YDigital Media support."));
            }

            $level = error_reporting();
            error_reporting($level | E_USER_ERROR);
            trigger_error($msg, E_USER_ERROR);
            error_reporting($level);
        }

        die();
    }

    /**
     * Sets locale based on what comes from $_GET, and returns real locale
     *
     * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
     *
     * @param string $locale Locale guessed from $_GET
     */
    public static function setLocale($locale) {
        if (isset($locale) && $locale) {
            if (strlen($locale) == 2) {
                $locale = $locale."_".strtoupper($locale);
            } else {
                $locale = $locale;
            }
            $_SESSION['locale'] = $locale;
        } elseif (isset($_SESSION['locale'])) {
            $locale = $_SESSION['locale'];
        } else {
            if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) == 'fr') {
                    $language = 'fr_FR';
                } else {
                    $language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
                }
            } else {
                $language = 'en_UK';
            }

            if (strlen($language) > 2) {
                $locale = strtolower(substr($language, 0, 2))."_".strtoupper(substr($language, 3, 2));
            } else {
                $locale = strtolower($language)."_".strtoupper($language);
            }
        }

        putenv("LC_ALL=".$locale);
        putenv("LANG=".$locale);
        setlocale(LC_ALL, $locale);

        bindtextdomain($locale, getenv('app_root')."/locale");
        bind_textdomain_codeset($locale, 'UTF-8');
        textdomain($locale);

        return $locale;
    }

    public static function sessionStart() {
        $salt = 'whatever123!"#/$/%&2';

       // self::checkSession();
        if (!session_id()) {
            session_start();
            $config                   = ApplicationConfig::getInstance();
             if ( isset($_SESSION['user']) && isset($_SESSION['organization']))
            {
                if ( ( $_SESSION['user'] != $config->u ) || ( $_SESSION['organization'] != $config->o ) )
                {
                    session_destroy();
                    Utils::abort("invalid session number");
                }
            }

            $_SESSION['user']         = $config->u;
            $_SESSION['organization'] = $config->o;
        }

        if (!isset($_SESSION['initiated'])) {
            session_regenerate_id(true);
            $_SESSION['initiated'] = true;

            // apply Update if needed
            self::applyUpdateRoutine();
        }

        if (isset($_SESSION['HTTP_USER_AGENT'])) {
            if ($_SESSION['HTTP_USER_AGENT'] != md5($salt.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'])) {
                unset($_SESSION['HTTP_USER_AGENT']);
                unset($_SESSION['initiated']);
                session_regenerate_id(true);
                session_destroy();

                session_start();
                session_regenerate_id(true);
                $_SESSION['initiated'] = true;
                $_SESSION['HTTP_USER_AGENT'] = md5($salt.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);

                header('Location: /');
                Utils::abort();

                return 0;
            } else {
                return 1;
            }
        } else {
            if (isset($_SERVER['HTTP_USER_AGENT']) && isset($_SERVER['REMOTE_ADDR'])) {
                $_SESSION['HTTP_USER_AGENT'] = md5($salt.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);
                return 1;
            } else {
                return 0;
            }
        }
    }

     public static function checkSession() {
        if (session_id()) {
            $file     = session_save_path() . '/sess_' . session_id();
            if(file_exists($file)){
            $config   = ApplicationConfig::getInstance();
            $contents = file_get_contents($file);
            session_decode($contents);

            if ($_SESSION['user'] != $config->u OR $_SESSION['organization'] != $config->o) {
                session_destroy();
                header('Location: '.$config->basePath.'dashboard/index.php');
            }
        }
        }
    }

    public static function sessionRegenerate() {
        $salt = 'whatever123!"#/$/%&2';

        if (!session_id()) {
            session_start();
        }

        session_regenerate_id(true);
        $_SESSION['HTTP_USER_AGENT'] = md5($salt.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);
    }

    /**
     * Transliterate a string
     * @param string    $string
     * @param char      $replacementChar
     * @return string
     */
    public static function translit($string, $replacementChar = '_') {
        return preg_replace('/[^a-z0-9]+/i', $replacementChar, $string);
    }

    /**
        * Return an array of values from an undefined number of values (single or array)
        * @access public
        * @static
        * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
        * @param  Mixed     $value
        * @param  boolean   $keepNullValues     Define if we keep null values
        * @return Array
        */
    public static function getMultiValues($value = null, $keepNullValues = false)
    {
        // init
        $aValues                = array();

        // single value
        if (is_string($value) OR is_numeric($value) OR is_bool($value) OR is_object($value)) {
            $aValues    = array($value);
        // multiple values
        } else if (is_array($value) AND (count($value) > 0)) {
            $aValues    = $value;
        }

        if (!$keepNullValues) {
            if (is_object(reset($aValues)) OR is_array($aValues)) {
                $aValues        = array_filter($aValues);
            } else {
                $aValues        = array_filter($aValues, 'strlen');
            }
        }

        return $aValues;
    }

    /**
     * Returns first non-null value
     * @param Mixed $_  Args
     * @return Mixed
     */
    public static function coalesce($_)
    {
        // Args
        $aArgs = func_get_args();

        $aValues = array_filter($aArgs, array('Utils', 'isNotNull'));
        return reset($aValues);
    }

    /**
     * Test variable nullity
     * @access public
     * @static
     * @param String $arg
     * @return mixed
     */
    public static function isNotNull($arg)
    {
        if ((is_string($arg) AND (($arg == '') OR (strtoupper($arg) == 'NULL') OR ($arg == 'undefined') OR (strtoupper($arg) == 'NAN') OR ($arg == '0000-00-00 00:00:00') OR ($arg == '0000-00-00') OR ($arg == '00:00:00')))
            OR ((is_integer($arg) OR is_float($arg) OR is_numeric($arg)) AND ($arg == 0)) OR ($arg == null)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Repeats <tt>$string</tt> <tt>$multiplier</tt> times, separated with <tt>$sep</tt>.
     *
     * str_repeat_sep('?', ',', 3) ==> "?,?,?"
     * str_repeat_seap('..', '/', 3) ==> "../../.."
     *
     * @param string $string
     * @param string $sep
     * @param int $multiplier
     * @return string
     */
    public static function str_repeat_sep($string, $sep, $multiplier) {
        $ret = "";
        for($i=0;$i<$multiplier;$i++) {
            if ($i) $ret.=$sep;
            $ret.=$string;
        }
        return $ret;
    }

    public static function sprintf_array($format, $values)
    {
        return call_user_func_array('sprintf', array_merge((array)$format, $values));
    }

    public static function formatNumberDec($number, $rangeMin = -999999999999, $rangeMax = 999999999999)
    {
        $number         = str_replace("'", '', $number);
        $number         = str_replace(',', '.', $number);

        if (floatval($number) < $rangeMin) {
            $number  = $rangeMin;
        }
        if (floatval($number) > $rangeMax) {
            $number  = $rangeMax;
        }

        return $number;
    }

    /**
     * Format an amount as 1'337.42
     * @param float     $amount
     * @param boolean   $wCurrency  If true, result =  CHF 1'337.42
     */
    public static function formatAmountToDisplay($amount, $currency = '', $nbDec = 2)
    {
        $sAmount        = number_format($amount, (int)$nbDec, '.', "'");
        if ($currency != '') {
            $sAmount    = $currency.' '.$sAmount;
        }

        return $sAmount;
    }
    /**
        * Add a log message to the logFile
        * @access public
        * @static
        * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
        * @param Mixed $input   Content to display
        * @param String $label   Label of the content to display (optionnal)
        * @return void
        */
    public static function log($input, $label = null)
    {
        // init
        $log    = DebugLog::getInstance();

        if ($log) {
            if (!is_scalar($input)) {
                $log->debugNonScalarObject($input, $label);
            } else {
                    if (is_bool($input)) {
                            $log->debug('(bool) '.(int) $input);
                    } else {
                            $log->debug($input);
                    }
            }
        }
    }

    public static function logTimer($label = '', $timerReference = null)
    {
        // init
        $log    = DebugLog::getInstance();
        if ($log) {
            $log->logTimer($label, $timerReference);
        }
    }

    public static function logMemoryUsage($label = '', $formated = true)
    {
        $memory         = memory_get_usage();
        $memoryReal     = memory_get_usage(true);
        $sLog           = ''.$memory.'(REAL = '.$memoryReal.')';
        if ($formated) {
            $sLog   = round(($memory/1024)/1024, 5)."M (REAL = ".round(($memoryReal/1024)/1024, 5)."M, Max :".ini_get('memory_limit').")";
        }
        $sLog       = (($label)?$label.' : ':'').$sLog;
        self::log($sLog);
    }

    /**
        * Get constants variables of an object
        * @access public
        * @static
        * @param Object $object
        * @param String $f_lib      Regex for the constant label
        * @param String $f_value    Regex for the constant value
        * @return Array
        */
    public static function getConstants($object, $f_lib = null, $f_value = null)
    {
        // init
        $reflect = new ReflectionClass($object);
        $constants = $reflect->getConstants();

        foreach ($constants as $name => $value) {
            if (!is_null($f_lib) AND !preg_match($f_lib, $name)) {
                unset($constants[$name]);
                continue;
            }
            if (!is_null($f_value) AND !preg_match($f_value, $value)) {
                unset($constants[$name]);
                continue;
            }
        }
        return $constants;
    }

    /**
     * Create a directory if not exists
     * @param String $path
     * @return Boolean
     */
    public static function createDirectory($path){

        if(!is_dir($path)){
            mkdir($path, 0777);
            return TRUE;
        } else {
        return FALSE;
        }

    }
    /**
     * Save file with his path
     * @param type $path
     * @return Boolean
     */
    public static function saveFile($path,$content){
        if($path AND $content){
            $fp = fopen($path, 'w');
            fwrite($fp, $content);
            fclose($fp);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Delete dir recursively (Be careful with that ! )
     * @param String $dir
     * @return Boolean
     */
     public static function rrmdir($dir) {
       if (is_dir($dir)) {
         $objects = scandir($dir);
         foreach ($objects as $object) {
           if ($object != "." && $object != "..") {
             if (filetype($dir."/".$object) == "dir") self::rrmdir($dir."/".$object); else unlink($dir."/".$object);
           }
         }
         reset($objects);
         rmdir($dir);
         return true;
       } else {
           return false;
       }
    }

    /**
     *
     * @param type $line
     * @param type $charMajWidth
     * @param type $charMinWidth
     * @return type
     */
    public static function getLineWidth( $line = '' , $charMajWidth = 10 , $charMinWidth = 8){
        $majPattern = '/[A-Z_ 23456789mw]+/';
        $smallPattern = '/[i\.jtl1I\')(]+/';
        preg_match_all($majPattern, $line, $matchesMaj);
        preg_match_all($smallPattern, $line, $matchesSmall);
        $nbMaj = 0;
        $nbSmall = 0;
        foreach($matchesMaj[0] as $value){
            $nbMaj += strlen($value);
        }
        foreach($matchesSmall[0] as $value){
            $nbSmall += strlen($value);
        }

        $nbMin = strlen($line) - $nbMaj;
        $lenght = $nbMaj*$charMajWidth + $nbMin*$charMinWidth - $nbSmall*8;
        return $lenght;
    }

    # Utils Array

    /**
     * Sort an std array depending of a property
     * @access public
     * @static
     * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
     * @param Array      $array
     * @param String     $property
     * @param Boolean    $sort       SORT_ASC or SORT_DESC
     * @return Array
     */
    public static function array_std_sort($array, $property, $sortValue = SORT_ASC)
    {
        // init
        global $key;
        global $sort;

        $key    = $property;
        $sort   = $sortValue;

        // sorting array
        if (is_array($array)) {
            usort($array, array('UtilsArray', 'compareStdArray'));
        }

        return $array;
    }

    /**
     * Generate an array using one or more attributes of a parent array
     * @access public
     * @static
     * @param Array $aData          Parent array
     * @param Array $attributeName  Name of the attribute we want to fetch
     * @param Array $_              Others attributes
     * @return Array
     */
    public static function array_child($aData, $attributeName, $_ = null)
    {
        // Init
        $aReturn = array();
        if (is_array($aData) OR ($aData instanceof Traversable)) {
            // getting attributes
            $aArgs  = func_get_args();
            $aArgs  = array_slice($aArgs, 1);

            $isMultiAtt = false;
            if (count($aArgs) > 1) {
                $isMultiAtt = true;
            }

            if ($attributeName) {

                foreach ($aData as $element) {

                    // Init
                    $value = null;

                    if ($isMultiAtt) {
                        $isArray = false;
                        if (is_array($element)) {
                            $isArray = true;
                            $value = array();
                        } else {
                            $value = new stdClass();
                        }

                        foreach ($aArgs as $attribut) {
                            if ($isArray) {
                                if (isset($element[$attribut])) {
                                    $value[$attribut]   = $element[$attribut];
                                } else {
                                    $value[$attribut]   = null;
                                }
                            } else {
                                if (isset($element->$attribut)) {
                                    $value->$attribut   = $element->$attribut;
                                } else {
                                    $value->$attribut   = null;
                                }
                            }
                        }
                    } else {
                        if (is_array($element) AND isset($element[$attributeName])) {
                            $value = $element[$attributeName];
                        } else if (isset($element->$attributeName)) {
                            $value = $element->$attributeName;
                        }
                    }

                    $aReturn[] = $value;
                }
            }
        }

        return $aReturn;
    }

    public static function array_map_recursive($function, $array) {
        $returnArray = array();
        foreach ($array as $k => $v) {
            $returnArray[$k] = (is_array($v))? self::array_map_recursive($function, $v) : $function($v); // or call_user_func($fn, $v)
        }

        return $returnArray;
    }

    /**
    * Flattens an array, or returns FALSE on fail.
    */
    public static function array_flatten($array, $onlyFirstValue = false) {
        if (!is_array($array)) {
            return FALSE;
        }
        $result = array();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                if (!$onlyFirstValue) {
                    $result = array_merge($result, self::array_flatten($value));
                } else {
                    $result[$key] = reset($value);
                }
            } else {
                $result[$key] = $value;
            }
        }
        return $result;
    }

    public static function array_sort_recursive(&$a)
    {
        sort($a);
        $c = count($a);
        for($i = 0; $i < $c; $i++)
            if (is_array($a[$i]))
                self::array_sort_recursive($a[$i]);
    }

    //////////////// CALLBACKS /////////////////

    public static function filter_numeric($var)
    {
        return is_numeric($var);
    }

    /**
     * Callback function used with usort().
     * @access private
     * @static
     * @author Nuno Barreto <n.barreto@ydigitalmedia.com>
     * @param Mixed $a
     * @param Mixed $b
     * @return Integer -1, 0 or 1.
     */
    private static function compareStdArray($a, $b)
    {
        // init
        $return         = 0;
        if (is_array($a)) {
            $aProperty      = $a[$GLOBALS['key']];
        } else {
            $aProperty      = $a->$GLOBALS['key'];
        }
        if (is_array($b)) {
            $bProperty      = $b[$GLOBALS['key']];
        } else {
            $bProperty      = $b->$GLOBALS['key'];
        }

        if ((isset ($aProperty)) AND (isset ($bProperty))) {
            switch(true) {
                case ((!is_string($aProperty)) AND ((!is_string($bProperty)))) : {
                    if ($aProperty != $bProperty) {
                        if ($GLOBALS['sort'] == SORT_ASC) {
                            if ($aProperty > $bProperty) {
                                $return     = 1;
                            } elseif ($aProperty < $bProperty) {
                                $return     = -1;
                            }
                        } elseif ($GLOBALS['sort'] == SORT_DESC) {
                            if ($aProperty > $bProperty) {
                                $return     = -1;
                            } elseif ($aProperty < $bProperty) {
                                $return     = 1;
                            }
                        }
                    } else {
                        $return = 0;
                    }
                    break;
                }
                default : {
                    $aProperty  = strval($aProperty);
                    $bProperty  = strval($bProperty);

                    if (strcasecmp($aProperty, $bProperty) != 0) {
                        if ($GLOBALS['sort'] == SORT_ASC) {
                            if (strcasecmp($aProperty, $bProperty) > 0) {
                                $return     = 1;
                            } elseif (strcasecmp($aProperty, $bProperty) < 0) {
                                $return     = -1;
                            }
                        } elseif ($GLOBALS['sort'] == SORT_DESC) {
                            if (strcasecmp($aProperty, $bProperty) > 0) {
                                $return     = -1;
                            } elseif (strcasecmp($aProperty, $bProperty) < 0) {
                                $return     = 1;
                            }
                        }
                    } else {
                        $return = 0;
                    }
                    break;
                }
            }
        }
        return $return;
    }

    # Utils JSON

    public static function jsEscape($str) {
        return addcslashes($str,"\\\'\"&\n\r<>");
    }

    public static function generateLink($href = '', $imgUrl = '', $imgTitle = '', $linkText = '', $target = '')
    {
        $link = '';
        $link = "";
        if ($imgUrl) {
            $imgTitle   = htmlspecialchars($imgTitle);
            $link       .= "";
        }
        if ($linkText) {
            $link       .= $linkText;
        }
        $link .= "";

        return $link;
    }

    public static function generateMainDialogLink($dialogTitle = '', $href = '', $imgUrl = '', $imgTitle = '', $linkText = '')
    {
        $link = '';
        $link = "";
        if ($imgUrl) {
            $imgTitle   = htmlspecialchars($imgTitle);
            $link       .= "";
        }
        if ($linkText) {
            $link       .= $linkText;
        }
        $link .= "";

        return $link;
    }

    public static function stringUrlToArray($string){
        $infoArrayCampaignAux = explode('&', $string);
        $infoArrayCampaign = array();
        foreach($infoArrayCampaignAux as $value){
            if(!empty($value)){
                list($key, $value) = explode('=', $value);
                $infoArrayCampaign[$key] = urldecode($value);
            }

        }

        return $infoArrayCampaign;
    }
    public static function compare2DateTimes($startDate, $endDate){
        $tsStartDate = strtotime($startDate);
        $tsEndDate = strtotime($endDate);

        if($tsStartDate > $tsEndDate){
            return array('status'=>'ko', 'message'=> 'START DATE: '.$startDate.' bigger then END DATA:'.$endDate);
        }elseif($tsStartDate == $tsEndDate){
            return array('status'=>'ko', 'message'=> 'START DATE: '.$startDate.' equals END DATE:'.$endDate);
        }else{
            return 'ok';
        }
    }

    public static function getInfoFile($file){
        //GET MIME TYPE
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $mimeType = $finfo->file($file);

        //GET INFO FILE
        list($width, $height, $type, $attr) = getimagesize($file);

        return array('width' => $width, 'height' => $height, 'type' => $type, 'attr' =>$attr, 'mimeType' => $mimeType);
    }
    public static function getInfoFileResource($file){
        //GET MIME TYPE
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $mimeType = $finfo->buffer($file);

        //GET INFO FILE
        $image = imagecreatefromstring($file);
        $width = imagesx($image);
        $height = imagesy($image);

        return array('width' => $width, 'height' => $height, 'mimeType' => $mimeType);
    }

    public static function decryptPathFile($file){
        $firstFolder = substr($file, 0, 2);
        $secondFolder = substr($file, 2, 2);

        return $firstFolder.DIRECTORY_SEPARATOR.$secondFolder.DIRECTORY_SEPARATOR.$file;
    }

    public static function parsePutToVariables($raw_data){
        // Fetch content and determine boundary
        //$raw_data = file_get_contents('php://input');
        $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
        $data = array();

        // Fetch each part
        if(!empty($raw_data)){
            $parts = array_slice(explode($boundary, $raw_data), 1);

            foreach ($parts as $part) {
                // If this is the last part, break
                if ($part == "--\r\n") break;

                // Separate content from headers
                $part = ltrim($part, "\r\n");
                list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);

                // Parse the headers list
                $raw_headers = explode("\r\n", $raw_headers);
                $headers = array();
                foreach ($raw_headers as $header) {
                    list($name, $value) = explode(':', $header);
                    $headers[strtolower($name)] = ltrim($value, ' ');
                }

                // Parse the Content-Disposition to get the field name, etc.
                if (isset($headers['content-disposition'])) {
                    $filename = null;
                    preg_match(
                        '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
                        $headers['content-disposition'],
                        $matches
                    );
                    list(, $type, $name) = $matches;
                    isset($matches[4]) and $filename = $matches[4];

                    // handle your fields here
                    switch ($name) {
                        // this is a file upload
                        case 'userfile':
                             file_put_contents($filename, $body);
                             break;

                        // default for all other files is to populate $data
                        default:
                             $data[$name] = substr($body, 0, strlen($body) - 2);
                             break;
                    }
                }
            }
        }

        return $data;
    }
}
?>


text:
@implementation DSTTestObject
@synthesize unsignedInteger, aFloat, aSize, aString, aPoint, aRect, someValues, someValuesDictionary, customPersistentClass, readonlyValue;

- (NSUInteger)version {
	return 1;
}

- (void)setDefaults {
	unsignedInteger = 123;
	aFloat = 1.23;
	aString = @"String";
	aSize = CGSizeMake(10, 20);
	aPoint = CGPointMake(20, 30);
	aRect = CGRectMake(10, 20, 30, 40);
	someValues = [NSArray arrayWithObjects:@"aString", [NSNumber numberWithInt:123], nil];
	someValuesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"abcdefghijklmnopq", @"aString", [NSNumber numberWithInt:123], @"anInteger", nil];
	customPersistentClass = nil;
	readonlyValue = 200;
}

- (NSString *)description {
	return [NSString stringWithFormat:@"%@ = { unsignedInteger = %d, aFloat: %f, aString = '%@', aSize = %@, aPoint = %@, aRect = %@, someValues = %@, someValuesDictionary = %@, customPersistentClass = %p }",
		  [self class], unsignedInteger, aFloat, aString, NSStringFromCGSize(aSize), NSStringFromCGPoint(aPoint), NSStringFromCGRect(aRect), someValues, someValuesDictionary, customPersistentClass];
}

- (void)didLoadFromContext {
	NSLog(@"just loaded this object from disk");
}

@end


text:
/*
 * File:   Sequence.hpp
 * Author: Arthur Lisek-Koper <deathvango@gmail.com>
 *
 * Created on February 2, 2015, 11:00 PM
 */

#ifndef SEQUENCE_HPP
#define	SEQUENCE_HPP

/*
 * Class Description here
 */
struct Sequence {
public:

	///False if the sequence has not been initialized yet, true otherwise.
	//An uninitialized sequence means the object does not have this sequence.
	bool initialized;

	///The length of time for the entire sequence to be completed.
	float duration;

	///Can the sequence be canceled by another action? If false,
	///then no action can be taken until the sequence is completely
	///finished
	//bool isCancelable;

	///At what point in time of the sequence can the sequence
	///be canceled.
	float cancelPoint;

};

#endif	/* SEQUENCE_HPP */