text:
/////////////////////////////////////////////////////////////////////////////////
//
// SourceFile: WebConfigIIS.cpp
//
// Marlin Server: Internet server/client
//
// Copyright (c) 2014-2022 ir. W.E. Huisman
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "stdafx.h"
#include "WebConfigIIS.h"
#include "XMLMessage.h"
#include "FileBuffer.h"
#include <http.h>
#include <io.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

WebConfigIIS::WebConfigIIS(XString p_application /*=""*/)
             :m_application(p_application)
{
}

WebConfigIIS::~WebConfigIIS()
{
}

bool
WebConfigIIS::ReadConfig()
{
  // Reads the central IIS application host configuration file first
  // this file contains the defaults for IIS.
  return ReadConfig("%windir%\\system32\\inetsrv\\config\\ApplicationHost.Config", nullptr);
}

bool
WebConfigIIS::ReadConfig(XString p_application,XString p_extraWebConfig /*= ""*/)
{
  // Reads the central IIS application host configuration file first
  // this file contains the defaults for IIS.
  bool result = ReadConfig("%windir%\\system32\\inetsrv\\config\\ApplicationHost.Config",nullptr);
  if(!p_application.IsEmpty())
  {
    SetApplication(p_application);
  }
  else if(!p_extraWebConfig.IsEmpty())
  {
    ReadConfig(p_extraWebConfig,nullptr);
  }
  return result;
}

void
WebConfigIIS::SetApplication(XString p_application)
{
  // Try reading applications web.config
  IISSite* site = GetSite(p_application);
  if(site)
  {
    // Get virtual directory: can be outside "inetpub\wwwroot"
    XString config = site->m_path + "\\web.config";
    // Read the web.config of the application site, if any requested
    if(ReadConfig(config,site))
    {
      m_webconfig = config;
    }
  }
  // Store the application
  m_application = p_application;
}

XString
WebConfigIIS::GetSiteName(XString p_site)
{
  IISSite* site = GetSite(p_site);
  if (site)
  {
    return site->m_name;
  }
  return "";
}

XString
WebConfigIIS::GetSetting(XString p_key)
{
  return m_settings[p_key];
}

XString
WebConfigIIS::GetSiteAppPool(XString p_site)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_appPool;
  }
  return XString();
}

XString
WebConfigIIS::GetSiteBinding(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_binding;
  }
  return p_default;
}

XString
WebConfigIIS::GetSiteProtocol(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_protocol;
  }
  return p_default;
}

int
WebConfigIIS::GetSitePort(XString p_site,int p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_port;
  }
  return p_default;
}

bool
WebConfigIIS::GetSiteSecure(XString p_site,bool p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_secure;
  }
  return p_default;
}

XString
WebConfigIIS::GetSiteWebroot(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_path;
  }
  return p_default;
}

XString
WebConfigIIS::GetSitePathname(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_path;
  }
  return p_default;
}

ULONG
WebConfigIIS::GetSiteScheme(XString p_site,ULONG   p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_authScheme;
  }
  return p_default;
}

XString
WebConfigIIS::GetSiteRealm(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_realm;
  }
  return p_default;
}

XString
WebConfigIIS::GetSiteDomain(XString p_site,XString p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_domain;
  }
  return p_default;
}

bool
WebConfigIIS::GetSiteNTLMCache(XString p_site,bool p_default)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_ntlmCache;
  }
  return p_default;
}

bool
WebConfigIIS::GetSitePreload(XString p_site)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return site->m_preload;
  }
  return false;
}

IISError
WebConfigIIS::GetSiteError(XString p_site)
{
  IISSite* site = GetSite(p_site);
  if (site)
  {
    return site->m_error;
  }
  return IISER_NoError;
}

XString
WebConfigIIS::GetPoolStartMode(XString p_pool)
{
  IISAppPool* pool = GetPool(p_pool);
  if(pool)
  {
    return pool->m_startMode;
  }
  return XString();
}

XString
WebConfigIIS::GetPoolPeriodicRestart(XString p_pool)
{
  IISAppPool* pool = GetPool(p_pool);
  if(pool)
  {
    return pool->m_periodicRestart;
  }
  return XString();
}

XString
WebConfigIIS::GetPoolIdleTimeout(XString p_pool)
{
  IISAppPool* pool = GetPool(p_pool);
  if(pool)
  {
    return pool->m_idleTimeout;
  }
  return XString();
}

bool
WebConfigIIS::GetPoolAutostart(XString p_pool)
{
  IISAppPool* pool = GetPool(p_pool);
  if(pool)
  {
    return pool->m_autoStart;
  }
  return false;
}

XString
WebConfigIIS::GetPoolPipelineMode(XString p_pool)
{
  IISAppPool* pool = GetPool(p_pool);
  if(pool)
  {
    return pool->m_pipelineMode;
  }
  return XString();
}

//////////////////////////////////////////////////////////////////////////
//
// PRIVATE METHODS
//
//////////////////////////////////////////////////////////////////////////

// Replace environment variables in a string
bool
WebConfigIIS::ReplaceEnvironVars(XString& p_string)
{
  bool replaced = false;
  XString local(p_string);

  while(true)
  {
    // Find first and second '%' marker
    int beginPos = local.Find('%');
    if(beginPos < 0)
    {
      return replaced;
    }
    int endPos = local.Find('%',beginPos+1);
    if(endPos <= beginPos)
    {
      break;
    }

    // Two markers found
    XString var = local.Mid(beginPos + 1, (endPos - beginPos - 1));
    XString value;
    if(value.GetEnvironmentVariable(var) && !value.IsEmpty())
    {
      local = local.Left(beginPos) + value + local.Mid(endPos + 1);
      p_string = local;
      replaced = true;
    }
    else
    {
      break;
    }
  }
  return false;
}

bool
WebConfigIIS::ReadConfig(XString p_configFile,IISSite* p_site /*=nullptr*/)
{
  // Find the file, see if we have read access at least
  ReplaceEnvironVars(p_configFile);
  if(_access(p_configFile,4) != 0)
  {
    return false;
  }

  // See if we did already read this file earlier
  p_configFile.MakeLower();
  WCFiles::iterator it = m_files.find(p_configFile);
  if(it != m_files.end() && !p_site)
  {
    return true;
  }

  // Parse the incoming file
  XMLMessage msg;
  msg.LoadFile(p_configFile);

  ReadLogPath(msg);
  ReadAppPools(msg);
  ReadSites(msg);
  ReadSettings(msg);

  // Read web.config overrides for application
  if(p_site)
  {
    ReadStreamingLimit(msg,nullptr);
    ReadAuthentication(*p_site,msg);
    ReadHandlerMapping(*p_site,msg);
  }

  // Remember we did read this file
  m_files.insert(std::make_pair(p_configFile,1));

  return true;
}

void
WebConfigIIS::ReadLogPath(XMLMessage& p_msg)
{
  XMLElement* log = p_msg.FindElement("log");
  if(log)
  {
    XMLElement* central = p_msg.FindElement(log,"centralW3CLogFile");
    if(central)
    {
      XString enabled   = p_msg.GetAttribute(central,"enabled");
      XString directory = p_msg.GetAttribute(central,"directory");

      // Process the directory
      ReplaceEnvironVars(directory);

      // Remember this
      m_logging = enabled.CompareNoCase("true") == 0;
      m_logpath = directory;
    }
  }
}

void
WebConfigIIS::ReadAppPools(XMLMessage& p_msg)
{
  XMLElement* appPools = p_msg.FindElement("applicationPools");
  if(!appPools) return;

  XString defaultStartmode; // empty or "AlwaysRunning"
  XString defaultRecycling; // time "days.hours:minutes:seconds"

  // Find defaults for all the pools
  XMLElement* defs = p_msg.FindElement(appPools,"applicationPoolDefaults");
  if(defs)
  {
    defaultStartmode = p_msg.GetAttribute(defs,"startMode");
    XMLElement* restart = p_msg.FindElement(defs,"periodicRestart");
    if(restart)
    {
      defaultRecycling = p_msg.GetAttribute(restart,"time");
    }
  }

  // Find pools and specific settings for the pool
  XMLElement* pools = p_msg.FindElement(appPools,"add");
  while(pools)
  {
    IISAppPool pool;
    pool.m_autoStart       = false;
    pool.m_startMode       = defaultStartmode;
    pool.m_periodicRestart = defaultRecycling;

    // Getting pool name
    XString name = p_msg.GetAttribute(pools,"name");
    pool.m_name = name;

    // Getting pool startmode
    XString startMode = p_msg.GetAttribute(pools,"startMode");
    if(!startMode.IsEmpty())
    {
      pool.m_startMode = startMode;
    }

    // Getting pool autostart
    XString autostart = p_msg.GetAttribute(pools,"autoStart");
    if(autostart.CompareNoCase("true") == 0)
    {
      pool.m_autoStart = true;
    }

    // Getting the pipeline mode
    pool.m_pipelineMode = p_msg.GetAttribute(pools,"managedPipelineMode");

    // Getting idle timeout
    XMLElement* process = p_msg.FindElement(pools,"processModel");
    if(process)
    {
      pool.m_idleTimeout = p_msg.GetAttribute(process,"idleTimeout");
    }

    // Retain pool information
    name.MakeLower();
    m_pools.insert(std::make_pair(name,pool));

    // Getting the next pool
    pools = p_msg.GetElementSibling(pools);
    if(pools && pools->GetName().Compare("add"))
    {
      break;
    }
  }
}

void
WebConfigIIS::ReadSites(XMLMessage& p_msg)
{
  XMLElement* sites = p_msg.FindElement("sites");
  if(!sites) return;

  XMLElement* site = p_msg.FindElement(sites,"site");
  while(site)
  {
    if(site->GetName() == "site")
    {
      IISSite theSite;
      theSite.m_ntlmCache  = true;
      theSite.m_secure     = false;
      theSite.m_preload    = false;
      theSite.m_authScheme = 0;
      theSite.m_error      = IISER_NoError;
      theSite.m_name       = p_msg.GetAttribute(site,"name");

      // Application
      XMLElement* applic = p_msg.FindElement(site,"application");
      if(applic)
      {
        theSite.m_appPool = p_msg.GetAttribute(applic,"applicationPool");
        theSite.m_preload = p_msg.GetAttribute(applic,"preloadEnabled").CompareNoCase("true") == 0;
      }

      // Virtual path
      XMLElement* virtdir = p_msg.FindElement(site,"virtualDirectory");
      if(virtdir)
      {
        theSite.m_path = p_msg.GetAttribute(virtdir,"physicalPath");
        ReplaceEnvironVars(theSite.m_path);
      }

      // First binding
      XMLElement* binding = p_msg.FindElement(site,"binding");
      if(binding)
      {
        theSite.m_binding  = p_msg.GetAttribute(binding,"bindingInformation");
        theSite.m_protocol = p_msg.GetAttribute(binding,"protocol");
        theSite.m_secure   = p_msg.GetAttribute(binding,"sslFlags").GetLength() > 0;
      }

      // Port is part of URLACL binding "*:80" or "+:443" or something
      theSite.m_port = atoi(theSite.m_binding.Mid(2));

      // Check for secure site
      if(theSite.m_protocol.CompareNoCase("https") == 0)
      {
        theSite.m_secure = true;
      }

      ReadStreamingLimit(p_msg,site);
      ReadAuthentication(theSite,p_msg);

      // Add to sites
      XString name = theSite.m_name;
      name.MakeLower();
      m_sites.insert(std::make_pair(name,theSite));
    }
    // Next site
    site = p_msg.GetElementSibling(site);
  }
}

void
WebConfigIIS::ReadSettings(XMLMessage& p_msg)
{
  XMLElement* settings = p_msg.FindElement("appSettings");
  if(settings)
  {
    XMLElement* add = p_msg.FindElement(settings,"add");
    while(add)
    {
      XString key   = p_msg.GetAttribute(add,"key");
      XString value = p_msg.GetAttribute(add,"value");

      m_settings[key] = value;

      // Next setting
      add = p_msg.GetElementSibling(add);
    }
  }
}

void
WebConfigIIS::ReadStreamingLimit(XMLMessage& p_msg,XMLElement* p_elem)
{
  XMLElement* webserv = p_msg.FindElement(p_elem,"system.webServer");
  if(webserv)
  {
    XMLElement* limit = p_msg.FindElement(webserv,"requestLimits");
    if(limit)
    {
      XString max = p_msg.GetAttribute(limit,"maxAllowedContentLength");
      long theLimit = atol(max);
      if(theLimit > 0)
      {
        m_streamingLimit = theLimit;
      }
    }
  }
}

void
WebConfigIIS::ReadAuthentication(IISSite& p_site,XMLMessage& p_msg)
{
  XMLElement* location = p_msg.FindElement("location");

  while(location)
  {
    XString path = p_msg.GetAttribute(location,"path");
    if(path.CompareNoCase(p_site.m_name) == 0)
    {
       ReadAuthentication(p_site,p_msg,location);
       return;
    }
    // Next location
    location = p_msg.GetElementSibling(location);
  }
}

void
WebConfigIIS::ReadAuthentication(IISSite& p_site,XMLMessage& p_msg,XMLElement* p_elem)
{
  bool anonymousFound = false;

  XMLElement* auth = p_msg.FindElement(p_elem,"authentication");
  if(!auth) return;

  // Check anonymous authentication
  XMLElement* anonymous = p_msg.FindElement(auth,"anonymousAuthentication");
  if(anonymous)
  {
    XString enable = p_msg.GetAttribute(anonymous,"enabled");
    if(enable.CompareNoCase("true") == 0)
    {
      // Anonymous authentication IS enabled
      // No other authentication will EVER BE DONE
      // Do not read all the other authentications
      p_site.m_authScheme = 0;
      anonymousFound = true;
    }
  }

  // Check basic authentication
  XMLElement* basic = p_msg.FindElement(auth,"basicAuthentication");
  if(basic)
  {
    XString enable = p_msg.GetAttribute(basic,"enabled");
    if(enable.CompareNoCase("true") == 0)
    {
      p_site.m_authScheme |= HTTP_AUTH_ENABLE_BASIC;

      XString realm = p_msg.GetAttribute(basic,"realm");
      if(!realm.IsEmpty())
      {
        p_site.m_realm = realm;
      }
      XString domain = p_msg.GetAttribute(basic,"defaultLogonDomain");
      if(!domain.IsEmpty())
      {
        p_site.m_domain = domain;
      }
    }
  }

  // Check Digest authentication
  XMLElement* digest = p_msg.FindElement(auth,"digestAuthentication");
  if(digest)
  {
    XString enable = p_msg.GetAttribute(digest,"enabled");
    if(enable.CompareNoCase("true") == 0)
    {
      p_site.m_authScheme |= HTTP_AUTH_ENABLE_DIGEST;

      XString realm = p_msg.GetAttribute(digest,"realm");
      if(!realm.IsEmpty())
      {
        p_site.m_realm = realm;
      }
    }
  }

  // Check Windows Authentication
  XMLElement* windows = p_msg.FindElement(auth,"windowsAuthentication");
  if(windows)
  {
    XString enable = p_msg.GetAttribute(windows,"enabled");
    if(enable.CompareNoCase("true") == 0)
    {
      // See if we must de-activate NTLM caching
      XString persist = p_msg.GetAttribute(windows,"authPersistSingleRequest");
      if(persist.CompareNoCase("true") == 0)
      {
        p_site.m_ntlmCache = false;
      }

      // Find providers
      XMLElement* providers = p_msg.FindElement(windows,"providers");
      if(providers)
      {
        XMLElement* add = p_msg.FindElement(providers,"add");
        while(add)
        {
          XString value = p_msg.GetAttribute(add,"value");
          if(value.CompareNoCase("NTLM") == 0)
          {
            p_site.m_authScheme |= HTTP_AUTH_ENABLE_NTLM;
          }
          if(value.CompareNoCase("Negotiate") == 0)
          {
            p_site.m_authScheme |= HTTP_AUTH_ENABLE_NEGOTIATE;
          }
          if(value.CompareNoCase("Negotiate:Kerberos") == 0)
          {
            p_site.m_authScheme |= HTTP_AUTH_ENABLE_NEGOTIATE;
            p_site.m_authScheme |= HTTP_AUTH_ENABLE_KERBEROS;
          }
          // Next provider
          add = p_msg.GetElementSibling(add);
        }
      }
    }
  }

  if(p_site.m_authScheme && anonymousFound)
  {
    // ERROR on this site. Authentication will **NOT** work
    // Both anonymous and some other authentication found!!
    p_site.m_error = IISER_AuthenticationConflict;
  }
}

void
WebConfigIIS::ReadHandlerMapping(IISSite& p_site, XMLMessage& p_msg)
{
  XMLElement* location = p_msg.FindElement("location");

  while(location)
  {
    XString path = p_msg.GetAttribute(location, "path");
    if (path.CompareNoCase(p_site.m_name) == 0)
    {
      ReadHandlerMapping(p_site,p_msg,location);
      return;
    }
    // Next location
    location = p_msg.GetElementSibling(location);
  }
}

// Getting the handler mappings from IIS
// (through ReadConfig/ReadSites)
//
void
WebConfigIIS::ReadHandlerMapping(IISSite& p_site,XMLMessage& p_msg,XMLElement* p_elem)
{
  XMLElement* sws = p_msg.FindElement(p_elem,"system.webServer");
  if (!sws) return;
  XMLElement* handlers = p_msg.FindElement(sws,"handlers");
  if (!handlers) return;
  XMLElement* add = p_msg.FindElement(handlers,"add");
  while (add)
  {
    IISHandler handler;

    XString name           = p_msg.GetAttribute(add,"name");
    handler.m_path         = p_msg.GetAttribute(add,"path");
    handler.m_verb         = p_msg.GetAttribute(add,"verb");
    handler.m_modules      = p_msg.GetAttribute(add,"modules");
    handler.m_resourceType = p_msg.GetAttribute(add,"resourceType");
    handler.m_precondition = p_msg.GetAttribute(add,"preCondition");

    p_site.m_handlers.insert(std::make_pair(name,handler));

    // Next handler mapping
    add = p_msg.GetElementSibling(add);
  }
}

IISHandlers*
WebConfigIIS::GetAllHandlers(XString p_site)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    return &(site->m_handlers);
  }
  return nullptr;
}

IISHandler*
WebConfigIIS::GetHandler(XString p_site,XString p_handler)
{
  IISSite* site = GetSite(p_site);
  if(site)
  {
    IISHandlers::iterator it = site->m_handlers.find(p_handler);
    if(it != site->m_handlers.end())
    {
      return &(it->second);
    }
  }
  return nullptr;
}

// Finding a site registration
// Site/Subsite -> Finds "site"
// Site         -> FInds "site"
IISSite*
WebConfigIIS::GetSite(XString p_site)
{
  // Registration is in lower case
  p_site.MakeLower();
  p_site.Trim('/');

  // Knock off the sub sites
  int pos = p_site.Find('/');
  if(pos > 0)
  {
    p_site = p_site.Left(pos);
  }

  // Find the site structure
  IISSites::iterator it = m_sites.find(p_site);
  if(it != m_sites.end())
  {
    return &it->second;
  }
  return nullptr;
}

// Finding a application pool registration
IISAppPool*
WebConfigIIS::GetPool(XString p_pool)
{
  p_pool.MakeLower();
  IISPools::iterator it = m_pools.find(p_pool);
  if(it != m_pools.end())
  {
    return &(it->second);
  }
  return nullptr;
}


text:
// @flow

import {
FullMonth,
PartialMonth,
NumberMonth,
FullYear,
PartialYear,
DayOfTheWeek,
Hour,
Hour24,
Minutes,
Seconds,
PostOrAnteMeridiem,
UserText,
Day,
DayOfTheMonth,
} from './subs';
import type { Token } from './parser'
import type { TinyTimeOptions } from './index'

/**
 * These types help ensure we don't misspell them anywhere. They will be
 * removed during build.
 */
type Days = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"
type Month =  "January" | "February" | "March" | "April" | "May" | "June" | "July" | "August" | "September" | "October" | "November" | "December"

const months: Array<Month> = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
]

const days: Array<Days> = [
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
]

/**
 * Takes an integer and returns a string left padded with
 * a zero to the left. Used to display minutes and hours (1:01:00PM);
 */
function paddWithZeros(int: number) : string {
  return int < 10 ? '0' + int : '' + int;
}

/**
 * Adds suffix to day, so 16 becomes 16th.
 */
function suffix(int: number): string {
    return (int % 10) == 1 && int != 11
      ? int  + "st"
      : (int % 10) == 2 && int != 12
      ? int + "nd"
      : (int % 10) == 3 && int != 13
      ? int + "rd"
      : int + "th";
}

/**
 * The compiler takes in our array of tokens returned from the parser
 * and returns the formed template. It just iterates over the tokens and
 * appends some text to the returned string depending on the type of token.
 * @param {Array<Tokens>} tokens
 * @param {Date} date
 * @param {TinyTimeOptions} options
 * @returns {String}
 */
export default function compiler(tokens: Array<Token>, date: Date, options: TinyTimeOptions): string {
  const month = date.getMonth();
  const year = date.getFullYear();
  const hours = date.getHours();
  const seconds = date.getSeconds();
  const minutes = date.getMinutes();
  const day = date.getDate();
  let compiled = '';
  let index = 0;
  while (index < tokens.length) {
    const token = tokens[index];
    switch (token.t) {
      case UserText:
      // $FlowFixMe flow doesn't know that v is always populated on UserText
        compiled += token.v;
        break;
      case Day:
        compiled += suffix(day);
        break;
      case PartialMonth:
        compiled += months[month].slice(0, 3);
        break;
      case FullMonth:
        compiled += months[month];
        break;
      case NumberMonth:
        let mnth = month + 1;
        if (options.padMonth) {
          mnth = paddWithZeros(mnth);
        }
        compiled += mnth;
        break;
      case FullYear:
        compiled += year;
        break;
      case PartialYear:
        compiled += (year + '').slice(2);
        break;
      case DayOfTheWeek:
        compiled += days[date.getDay()];
        break;
      case DayOfTheMonth:
        compiled += options.padDays ? paddWithZeros(day) : day
        break;
      case Hour:
        let hour = hours === 0 || hours === 12 ? 12 : hours % 12;
        if (options.padHours) {
          hour = paddWithZeros(hour)
        }
        compiled += hour
        break;
      case Hour24:
        let hour24 = hours;
        if (options.padHours) {
          hour24 = paddWithZeros(hour24)
        }
        compiled += hour24
        break;
      case Minutes:
        compiled += paddWithZeros(minutes);
        break;
      case Seconds:
        compiled += paddWithZeros(seconds);
        break;
      case PostOrAnteMeridiem:
        compiled += hours >= 12 ? 'PM' : 'AM';
        break;
    }
    index++;
  }
  return compiled;
}

redpajama_set_name: RedPajamaGithub


text:
package tw.soleil.tw.village.fragment;

import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;

import tw.soleil.tw.village.R;
import tw.soleil.tw.village.activity.MainActivity;

/**
 * Created by bryan on 2015/4/11.
 */
public class RunnerFragment extends PlaceholderFragment {

    private int numberOfSteps;

    private TextView stepsTextView;

    private MediaPlayer countDownMediaPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ((MainActivity)getActivity()).getSupportActionBar().hide();

        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

        countDownMediaPlayer = MediaPlayer.create(getActivity(), R.raw.motion_graphics);
    }

    public void onDestroy() {
        countDownMediaPlayer.stop();
        super.onDestroy();

    }

    public static RunnerFragment newInstance(int sectionNumber) {
        RunnerFragment fragment = new RunnerFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_runner, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final ImageButton beforeButton = (ImageButton)view.findViewById(R.id.imageButtonBefore);
        final ImageButton nextButton = (ImageButton)view.findViewById(R.id.imageButtonNext);

        final ImageView trollFaceImageView = (ImageView)view.findViewById(R.id.trollFaceImageView);

        final TextView yourNameTextView = (TextView)view.findViewById(R.id.name_Text_View);

        stepsTextView = (TextView)view.findViewById(R.id.steps_text_view);

        beforeButton.setEnabled(false);

        beforeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                beforeButton.setEnabled(false);
                nextButton.setEnabled(true);

                trollFaceImageView.setImageResource(R.drawable.troll_face_left);

                numberOfSteps++;

                stepsTextView.setText("Steps: "+numberOfSteps);
            }
        });

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextButton.setEnabled(false);
                beforeButton.setEnabled(true);

                trollFaceImageView.setImageResource(R.drawable.troll_face_right);
            }
        });

        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        AskNameDialog askNameDialog = new AskNameDialog() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (EditorInfo.IME_ACTION_DONE == actionId){

                    EditText nameEditText = (EditText)getDialog().findViewById(R.id.your_name_editText);
                    if (nameEditText.getText() != null) {
                        yourNameTextView.setText(nameEditText.getText().toString());

                        startGaming();

                        dismiss();
                    }
                    return true;
                }
                return false;
            }

            @Override
            public void onClick(View v) {

                EditText nameEditText = (EditText)getDialog().findViewById(R.id.your_name_editText);
                if (nameEditText.getText() != null) {
                    yourNameTextView.setText(nameEditText.getText().toString());

                    startGaming();

                    dismiss();
                }

            }
        };

        askNameDialog.show(fragmentManager, "ask name");

    }

    private void startGaming() {

        numberOfSteps = 0;
        stepsTextView.setText("Steps: "+numberOfSteps);

        final TextView yourNameTextView = (TextView)getActivity().findViewById(R.id.name_Text_View);
        final TextView timerTextView = (TextView)getActivity().findViewById(R.id.timerTextView);

        CountDownTimer countDownTimer = new CountDownTimer(20 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timerTextView.setText("Timer: " + millisUntilFinished / 1000);

                if (millisUntilFinished / 1000 == 15) {
                    countDownMediaPlayer.start();
                }
            }

            @Override
            public void onFinish() {
                timerTextView.setText("TIME'S UP");

                GameOverFragment gameOverFragment = GameOverFragment.newInstance();
                gameOverFragment.setOnRestartListener(new GameOverFragment.OnRestartListener() {
                    @Override
                    public void onRestart() {
                        startGaming();
                    }
                });
                gameOverFragment.setTotalSteps(numberOfSteps);
                gameOverFragment.setPlayerName(yourNameTextView.getText().toString());

                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .add(R.id.container, gameOverFragment)
                        .commitAllowingStateLoss();

            }
        }.start();
    }

}


text:
@interface UILabelCollectionViewCell()

- (void)commonInit;

@end

@implementation UILabelCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}

	return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	self = [super initWithCoder:aDecoder];
	if (self) {
		[self commonInit];
	}

	return self;
}

- (void)commonInit
{
	const float height = 0.5f;
	const float tableViewInsetFactor = 0.89f;

	float width = floorf(self.layer.bounds.size.width * tableViewInsetFactor);
	UIColor *tableViewCellGray = [UIColor colorWithRed:(200.f/255.f) green:(199.f/255.f) blue:(205.f/255.f) alpha:1.f];

	CALayer *separatorLayer = [CALayer layer];
	separatorLayer.contentsScale = [UIScreen mainScreen].scale;
	separatorLayer.backgroundColor = tableViewCellGray.CGColor;
	separatorLayer.bounds = CGRectMake(0.f, 0.f, width, height);
	separatorLayer.anchorPoint = CGPointMake(1.f, 1.f);
	separatorLayer.position = CGPointMake(self.layer.bounds.size.width, self.layer.bounds.size.height);
	[self.layer addSublayer:separatorLayer];
}

@end


text:
package main

import "fmt"

func main() {
	fmt.Println(ledr(5.5, 1.2, 0.015, 0.1))
	fmt.Println(ledr(5.5, 1.7, 0.018, 0.1))
	fmt.Println(ledr(5.5, 3.0, 0.07, 0.1))
}

// The model here is based on a series connection of an LED with a resistor
// We want to calculate the resistor value for the LED, we can use Ohm's law
// for this
// V=IR, but we do need to figure out what V and I should be
// Vs = supply voltage, how much voltage we feed into the circuit - 5 +- 0.5V
// Vf = LED forward voltage, since an LED is a form of diode, the forward voltage is the amount of minimum voltage needed to get across a diode (1.2-4.0V)
// If = LED forward current, the maximum recommended is anywhere from 15mA to 80mA
// Tr = Resistor tolerance, the resistor fluctations 1-10%
// So what we want is to maximize the resistor value such that it can handle all cases in this model range
// we want to maximize voltage and minimize current

// Maximizing the voltage means we supply the best we can
// so it will be around 5.5 V, the LED forward voltage should be as small as possible, so assume 1.2V
// For the current, we want to minimize it so we use 15 mA multiply by the largest tolerance which is 10%
// this will give a resistor value, then we can round it up to a standard resistor value for simplicity, or else
// we will have to chain resistors together to achieve more precision
func ledr(Vs_max, Vf_min, If_min, Tr_max float64) float64 {
	return (Vs_max - Vf_min) / (If_min * (1 - Tr_max))
}


text:
title: Install Composer on Elementary
description: How to install the PHP dependency manager Composer on Elementary OS.
author: 'Kirk'
categories: [Linux]
tags: [elementary, tools, apps, composer]

---
Installing Composer is fairly straight forward, however using it properly is a bit more complicated.

## Installing Locally

Installing Composer locally is a matter of just running the installer in your project directory:

```
curl -sS https://getcomposer.org/installer | php
```
If the above fails for some reason, you can download the installer with php instead:

```
php -r "readfile('https://getcomposer.org/installer');" | php
```

The installer will just check a few PHP settings and then download `composer.phar` to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line.

Now just run `php composer.phar` in order to run Composer.

You can install Composer to a specific directory by using the `--install-dir` option and additionally rename it as well using the `--filename option`:

```
curl -sS https://getcomposer.org/installer | php -- --install-dir=bin --filename=composer
```

Now just run `php bin/composer` in order to run Composer.

## Installing Globally

You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your PATH, you can access it globally. On unixy systems you can even make it executable and invoke it without directly using the php interpreter.

Run these commands to globally install composer on your system:

```
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
```
If the above fails due to permissions, run the `mv` line again with `sudo`.
A quick copy-paste version including sudo:

```
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
```

To add `/usr/local/bin` to your path:
```
export PATH=$PATH:/usr/local/bin
```


text:
#include "watchdog_api.h"

#if DEVICE_WATCHDOG

#include "reset_reason_api.h"
#include "fsl_rtwdog.h"
#include "fsl_clock.h"
#include "platform/mbed_wait_api.h"

// Platform specific watchdog definitions
#define LPO_CLOCK_FREQUENCY 32768
#define MAX_TIMEOUT         0xFFFFUL
#define DELAY_TIME          100000U

// Number of decrements in the timeout register per millisecond
#define TICKS_PER_MS ((LPO_CLOCK_FREQUENCY) / 1000)

// Maximum timeout that can be specified in milliseconds
#define MAX_TIMEOUT_MS ((MAX_TIMEOUT) / (TICKS_PER_MS))

watchdog_status_t hal_watchdog_init(const watchdog_config_t *config)
{
    uint32_t temp;
    rtwdog_config_t cfg;

    /* When system is boot up, WDOG32 is disabled. We must wait for at least 2.5
     * periods of wdog32 clock to reconfigure wodg32. So Delay a while to wait for
     * the previous configuration taking effect. */
    for (temp = 0; temp < DELAY_TIME; temp++) {
        __NOP();
    }

    RTWDOG_GetDefaultConfig(&cfg);

    cfg.workMode.enableStop = true;
    cfg.workMode.enableDebug = true;
    cfg.timeoutValue = (TICKS_PER_MS * config->timeout_ms);

    RTWDOG_Init(RTWDOG, &cfg);

    return WATCHDOG_STATUS_OK;
}

void hal_watchdog_kick(void)
{
    RTWDOG_Refresh(RTWDOG);
}

watchdog_status_t hal_watchdog_stop(void)
{
    RTWDOG_Deinit(RTWDOG);

    return WATCHDOG_STATUS_OK;
}

uint32_t hal_watchdog_get_reload_value(void)
{
    const uint32_t timeout = RTWDOG->TOVAL;

    return (timeout / TICKS_PER_MS);
}

watchdog_features_t hal_watchdog_get_platform_features(void)
{
    watchdog_features_t features;
    features.max_timeout = MAX_TIMEOUT_MS;
    features.update_config = true;
    features.disable_watchdog = true;
    features.clock_typical_frequency = 32000;
    features.clock_max_frequency = 32768;

    return features;
}

#endif // DEVICE_WATCHDOG


text:
set -e

RED="$(echo -e '\033[1;31m')"
NORM="$(echo -e '\033[0m')"

function handle_err() {
  echo -e "\n${RED}ERROR: build script failed.${NORM}\n"
}

trap handle_err ERR

function handle_cleanup() {
  echo -n "Removing the fingerprint... "
  [ -f ${container_fingerprint} ] && rm -f "${container_fingerprint}"
  echo "Done."
}

trap handle_cleanup  INT TERM EXIT

sudo echo -e "\nTesting sudo works...\n"

# We MUST be in the same directory as this script for the build to work properly
cd $(dirname $0)

# generate container fingerprint (user/location/timestamp/git information)
container_fingerprint='./container-build-env-fingerprint.output'
./container-build-env-fingerprint.sh > ${container_fingerprint}

# Build ourselves
echo
echo "Building oso-rhel7-host-monitoring..."
sudo time docker build $@ -t oso-rhel7-host-monitoring .


text:
- Added: Use css being imported with `addDepedencyTo`
- Added: `resolve` option to override resolving paths
- Updated: Improve ability to resolve npm modules

## 2.0.0 (2016-07-01)

- Added: Imports look in node_modules, bower_components, etc
- Added: `dirs` lets imports look in user specified directories
- Added: `plugins` lets PostCSS plugins run over individual partials
- Removed: Caching (for now)
- Updated: Complete rewrite of the plugin
- Updated: `extension` now includes the dot (e.g. `.css`)

## 1.3.0 (2015-09-14)

- Added: Support for generating imported files
- Updated: Switched to `fs-promise` from `fs` and `mkdirp`
- Updated: Switched to `assign` from `_extend`

## 1.2.0 (2015-09-09)

- Added: Support for media queries
- Added: Support for caching sources

## 1.1.1 (2015-09-08)

- Fixed: Plugins no longer run on imports

## 1.1.0 (2015-09-08)

- Updated: Refactored code
- Removed: `root` option

## 1.0.1 (2015-09-08)

- Fixed: Package dependencies
- Fixed: Documented syntax

## 1.0.0 (2015-09-07)

- Added: Initial release


text:
init:
	pip install -r requirements.txt

.PHONY: test

test:
	python -m unittest discover -s test -p "Test*.py"


text:
package com.abyx.loyalty.activities;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.abyx.loyalty.contents.Card;
import com.abyx.loyalty.contents.Database;
import com.abyx.loyalty.extra.Constants;
import com.abyx.loyalty.extra.ReceivedPermission;
import com.abyx.loyalty.extra.recycler.BaseAdapter;
import com.abyx.loyalty.extra.recycler.MultiMode;
import com.abyx.loyalty.fragments.ListFragment;
import com.abyx.loyalty.fragments.ListInteractor;
import com.abyx.loyalty.fragments.OverviewFragment;
import com.abyx.loyalty.R;
import com.abyx.loyalty.managers.ShortcutHandler;
import com.abyx.loyalty.managers.cache.CacheManager;
import com.abyx.loyalty.managers.ChangeListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends PermissionActivity implements ListInteractor<Card>, ChangeListener<List<Card>> {
    private static final String sortedString = "sorted_descending";

    private ArrayList<Card> data;
    private boolean sortedDescending;
    protected ListFragment<Card> overviewFragment;

    private Database db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This is the main Activity. We do not want to show the up-arrow in the Toolbar
        setDisplayHomeAsUp = false;

        setContentView(R.layout.layout_main);

        // The sortedDescending value from the last time the user used this app. Use DefaultShared-
        // Preferences. This allows us to get a hold on the same preferences from within different
        // activities.
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        sortedDescending = sharedPref.getBoolean(sortedString, false);

        overviewFragment = OverviewFragment.newInstance();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ShortcutHandler shortcutHandler = new ShortcutHandler(MainActivity.this);
        shortcutHandler.updatePopularDynamicShortcuts();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.db.unsubscribe(this);
    }

    @Override
    protected void onResume(){
        super.onResume();

        this.db = new Database(getApplicationContext());
        this.db.subscribe(this);

        if (isPermissionGranted(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            loadData();
        } else {
            requestWritePermissions(MainActivity.this, new ReceivedPermission() {
                @Override
                public void onPermissionGranted() {
                    loadData();
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_sort){
            sortedDescending = !sortedDescending;

            db.openDatabase();
            db.getAllCardsSortedByName(!sortedDescending);
            db.closeDatabase();

            // Save the sort preference of the user, so he doesn't has to choose this every time
            // the app starts
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean(sortedString, sortedDescending);
            editor.apply();
            return true;
        } else if (id == R.id.action_about) {
            Intent temp = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(temp);
            return true;
        } else if (id == R.id.action_backup) {
            Intent temp = new Intent(MainActivity.this, BackupRestoreActivity.class);
            temp.putParcelableArrayListExtra(Constants.INTENT_LIST_ARG, data);
            startActivity(temp);
            return true;
        } else if (id == R.id.search) {
            Intent temp = new Intent(MainActivity.this, SearchResultsActivity.class);
            temp.putParcelableArrayListExtra(Constants.INTENT_LIST_ARG, data);
            startActivity(temp);
            return true;
        } else if (id == R.id.action_invalidate_cache) {
            CacheManager cacheManager = new CacheManager(getApplicationContext());
            cacheManager.clearCache();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (this.overviewFragment.multiModeEnabled()) {
            this.overviewFragment.disableMultiMode();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public List<Card> requestData() {
        if (this.data == null) {
            return new ArrayList<>();
        } else {
            return data;
        }
    }

    @Override
    public void onItemClick(int dataPos, Card item) {
        Intent intent = new Intent(MainActivity.this, CardActivity.class);
        intent.putExtra(Constants.INTENT_CARD_ID_ARG, item.getID());
        startActivity(intent);
    }

    @Override
    public Toolbar getToolbar() {
        return (Toolbar) findViewById(R.id.toolbar);
    }

    @Override
    public MultiMode.Callback getOptionsMenuCallback() {
        return new MultiMode.Callback() {
            @Override
            public boolean onMenuItemClick(BaseAdapter adapter, MenuItem item) {
                return onOptionsItemSelected(item);
            }
        };
    }

    @Override
    public void removeCards(List<Card> cards) {
        ShortcutHandler shortcutHandler = new ShortcutHandler(MainActivity.this);

        db.openDatabase();
        for (Card current: cards) {
            db.deleteCard(current);
            this.data.remove(current);
            shortcutHandler.removePinnedShortcut(current);
        }
        db.closeDatabase();
        this.overviewFragment.dataChanged(this.data);
    }

    @Override
    public void change(List<Card> resource) {
        this.data = (ArrayList<Card>) resource;
        overviewFragment.dataChanged(resource);
    }

    private void loadData() {
        // Read all data from the internal storage
        db.openDatabase();
        db.getAllCardsSortedByName(!sortedDescending);
        db.closeDatabase();

        getSupportFragmentManager().beginTransaction().replace(R.id.overviewContainer, overviewFragment).commitAllowingStateLoss();
    }

    /**
     * This function is called when the add-button (the FAB) is pressed.
     *
     * @param view The view that was clicked and triggered this method.
     */
    public void addData(View view) {
        Intent intent = new Intent(MainActivity.this, AddStoreActivity.class);
        startActivity(intent);
    }
}


text:
package io.ckl.notificrash.events;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.UUID;

public class EventRequest implements Serializable {

    private String mEventData;
    private UUID uuid;

    public EventRequest(EventBuilder builder) {
        mEventData = new JSONObject(builder.getEvent()).toString();
        uuid = UUID.randomUUID();
    }

    /**
     * @return String
     */
    public String getEventData() {
        return mEventData;
    }

    /**
     * @return UUID
     */
    public UUID getUuid() {
        return uuid;
    }

    @Override
    public boolean equals(Object other) {
        EventRequest otherRequest = (EventRequest) other;

        if (this.uuid != null && otherRequest.uuid != null) {
            return uuid.equals(otherRequest.uuid);
        }

        return false;
    }
}


text:
using UnityEngine;
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Mono.Data.Sqlite;

/// <summary>
/// This is used to poll the database and then generate the
/// inventory for the store. It will pull the sprite path,
/// price, and quantity available.
///
/// Another Object will need to either generate the store itself
/// or that could be done here.
///
/// Descriptions of each item should be pulled from an item database
/// that would contain all of the items.
///
/// Should probably move the image path to the generic item list too
///
/// </summary>
public class oldStoreCreator : MonoBehaviour {
	//Name of the Database
	public string databaseName = "/gameDB.db";
	public string tableName = "ItemsForStoreTable";

	//Sprite Dictionary to allow for finding the right sprite
	public Dictionary<string, Sprite> spriteDict = new Dictionary<string, Sprite>();

	// Use this for initialization
	void Start ()
	{
		//Path to database
		string conn = "URI=file:" + Application.dataPath + databaseName;

		IDbConnection dbconn;
		dbconn = (IDbConnection)new SqliteConnection (conn);
		dbconn.Open (); //Open the conection.
		IDbCommand dbcmd = dbconn.CreateCommand();

		//This is the actual sql command, it is then passed to the db command
		string sqlQuery = "SELECT name, price, imagepath, quantity " + "From " + tableName;
		dbcmd.CommandText = sqlQuery;
		IDataReader reader = dbcmd.ExecuteReader ();

		//Fill the sprite dictionary
		//Using this command requires all the sprites to be in a
		//folder named sprites within a folder names Resources
		Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites");

		foreach (Sprite sprite in sprites)
		{
			print (sprite.name);
			spriteDict.Add (sprite.name, sprite);
		}

		//This dictionary doesnt really make sense right now as the
		//name and path are the same, not sure if its worth leaving in
		//in case we want them to be able to be changed later
		Dictionary<string, string> names = new Dictionary<string, string>();

		while (reader.Read ())
		{
			string name = reader.GetString (0);
			//int price = reader.GetInt32 (1);
			string img = reader.GetString (2);
			//int quant = reader.GetInt32 (3);

			names.Add(name, img);

			Debug.Log ("  name = " + name + " path = " + img);
		}

		//for each item in the dictionary,
		//display the sprite
		//probably will want to rename them too
		//WILL need to move them into a pretty fashion
		//and ensure all the sizes are correct.

		foreach (string img in names.Values){
			GameObject item = new GameObject ();
			item.AddComponent<SpriteRenderer> ();
			print (img);
			item.GetComponent<SpriteRenderer> ().sprite = spriteDict[img];
		}

		//This will be to load the sprites
		//Should be turned into a function at some point

		reader.Close ();
		reader = null;
		dbcmd.Dispose ();
		dbcmd = null;
		dbconn.Close ();
		dbconn = null;
	}

	// Update is called once per frame
	void Update ()
	{

	}
}


text:
import React from 'react';
import Axis from './Axis';

export default props => {
  const xSettings = {
    translate: `translate(0, ${props.height - props.padding})`,
    scale: props.xScale,
    orient: 'bottom'
  };
  const ySettings = {
    translate: `translate(${props.padding}, 0)`,
    scale: props.yScale,
    orient: 'left'
  };
  return (
    <g className="xy-axis">
      <Axis {...xSettings} />
      <Axis {...ySettings} />
    </g>
  );
};


text:
.progress-button {
	display: inline-block;
	font-size: 24px;
	color: #fff !important;
	text-decoration: none !important;
	padding: 14px 60px;
	line-height: 1;
	overflow: hidden;
	position: relative;
	cursor: pointer;

	box-shadow: 0 1px 1px #ccc;
	border-radius: 2px;

	background-color: #51b7e6;
	background-image: -webkit-linear-gradient(top, #51b7e6, #4dafdd);
	background-image: linear-gradient(top, #51b7e6, #4dafdd);
}

.progress-button-text {
	color: #fff;
	position: relative;
	z-index: 2;
}

/* The colorful bar that grows depending on the progress */

.progress-button-bar {
	background-color: #e667c0;
	height: 3px;
	bottom: 0;
	left: 0;
	width: 0;
	position: absolute;
	z-index: 1;

	border-radius: 0 0 2px 2px;

	-webkit-transition: width 0.5s, height 0.5s;
	transition: width 0.5s, height 0.5s;
}

/* The bar can be either horizontal, or vertical */

.progress-button-bar.progress-button-horizontal {
	height: 100%;
	border-radius: 2px;
}

.progress-button-bar.progress-button-vertical {
	height: 0;
	top: 0;
	width: 100%;
	border-radius: 2px;
}


text: * Return weak etags in bookmark links


text:
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var __decorate = this.__decorate || function (decorators, target, key, value) {
    var kind = typeof (arguments.length == 2 ? value = target : value);
    for (var i = decorators.length - 1; i >= 0; --i) {
        var decorator = decorators[i];
        switch (kind) {
            case "function": value = decorator(value) || value; break;
            case "number": decorator(target, key, value); break;
            case "undefined": decorator(target, key); break;
            case "object": value = decorator(target, key, value) || value; break;
        }
    }
    return value;
};
define(["require", "exports", '../validation/validation'], function (require, exports, validation_1) {
    var ValidationRule = (function () {
        function ValidationRule(threshold, onValidate, message) {
            this.onValidate = onValidate;
            this.threshold = threshold;
            this.message = message;
            this.errorMessage = null;
            this.ruleName = this.constructor.name;
        }
        ValidationRule.prototype.withMessage = function (message) {
            this.message = message;
        };
        ValidationRule.prototype.explain = function () {
            return this.errorMessage;
        };
        ValidationRule.prototype.validate = function (currentValue) {
            if (typeof (currentValue) === 'string') {
                if (String.prototype.trim) {
                    currentValue = currentValue.trim();
                }
                else {
                    currentValue = currentValue.replace(/^\s+|\s+$/g, '');
                }
            }
            var result = this.onValidate(currentValue, this.threshold);
            if (result) {
                this.errorMessage = null;
            }
            else {
                if (this.message) {
                    if (typeof (this.message) === 'function') {
                        this.errorMessage = this.message(currentValue, this.threshold);
                    }
                    else if (typeof (this.message) === 'string') {
                        this.errorMessage = this.message;
                    }
                    else
                        throw 'Unable to handle the error message:' + this.message;
                }
                else {
                    this.errorMessage = validation_1.Validation.Locale.translate(this.ruleName, currentValue, this.threshold);
                }
            }
            return result;
        };
        return ValidationRule;
    })();
    exports.ValidationRule = ValidationRule;
    var EmailValidationRule = (function (_super) {
        __extends(EmailValidationRule, _super);
        //https://github.com/chriso/validator.js/blob/master/LICENSE
        function EmailValidationRule() {
            var _this = this;
            this.emailUserUtf8Regex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))$/i;
            this.isFQDN = function (str) {
                var parts = str.split('.');
                for (var part, i = 0; i < parts.length; i++) {
                    part = parts[i];
                    if (part.indexOf('__') >= 0) {
                        return false;
                    }
                    part = part.replace(/_/g, '');
                    if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) {
                        return false;
                    }
                    if (part[0] === '-' || part[part.length - 1] === '-' ||
                        part.indexOf('---') >= 0) {
                        return false;
                    }
                }
                return true;
            };
            _super.call(this, null, function (newValue, threshold) {
                if (/\s/.test(newValue)) {
                    return false;
                }
                var parts = newValue.split('@');
                var domain = parts.pop();
                var user = parts.join('@');
                if (!_this.isFQDN(domain)) {
                    return false;
                }
                return _this.emailUserUtf8Regex.test(user);
            });
        }
        return EmailValidationRule;
    })(ValidationRule);
    exports.EmailValidationRule = EmailValidationRule;
    var MinimumLengthValidationRule = (function (_super) {
        __extends(MinimumLengthValidationRule, _super);
        function MinimumLengthValidationRule(minimumLength) {
            _super.call(this, minimumLength, function (newValue, minimumLength) {
                return newValue.length !== undefined && newValue.length >= minimumLength;
            });
        }
        return MinimumLengthValidationRule;
    })(ValidationRule);
    exports.MinimumLengthValidationRule = MinimumLengthValidationRule;
    var MaximumLengthValidationRule = (function (_super) {
        __extends(MaximumLengthValidationRule, _super);
        function MaximumLengthValidationRule(maximumLength) {
            _super.call(this, maximumLength, function (newValue, maximumLength) {
                return newValue.length !== undefined && newValue.length < maximumLength;
            });
        }
        return MaximumLengthValidationRule;
    })(ValidationRule);
    exports.MaximumLengthValidationRule = MaximumLengthValidationRule;
    var BetweenLengthValidationRule = (function (_super) {
        __extends(BetweenLengthValidationRule, _super);
        function BetweenLengthValidationRule(minimumLength, maximumLength) {
            _super.call(this, { minimumLength: minimumLength, maximumLength: maximumLength }, function (newValue, threshold) {
                return newValue.length !== undefined
                    && newValue.length >= threshold.minimumLength
                    && newValue.length < threshold.maximumLength;
            });
        }
        return BetweenLengthValidationRule;
    })(ValidationRule);
    exports.BetweenLengthValidationRule = BetweenLengthValidationRule;
    var CustomFunctionValidationRule = (function (_super) {
        __extends(CustomFunctionValidationRule, _super);
        function CustomFunctionValidationRule(customFunction, threshold) {
            _super.call(this, threshold, customFunction);
        }
        return CustomFunctionValidationRule;
    })(ValidationRule);
    exports.CustomFunctionValidationRule = CustomFunctionValidationRule;
    var NumericValidationRule = (function (_super) {
        __extends(NumericValidationRule, _super);
        function NumericValidationRule() {
            _super.call(this, null, function (newValue) {
                var numericRegex = validation_1.Validation.Locale.setting('numericRegex');
                var floatValue = parseFloat(newValue);
                return !Number.isNaN(parseFloat(floatValue))
                    && Number.isFinite(floatValue)
                    && numericRegex.test(newValue);
            });
        }
        return NumericValidationRule;
    })(ValidationRule);
    exports.NumericValidationRule = NumericValidationRule;
    var RegexValidationRule = (function (_super) {
        __extends(RegexValidationRule, _super);
        function RegexValidationRule(regex) {
            _super.call(this, regex, function (newValue, regex) {
                return regex.test(newValue);
            });
        }
        return RegexValidationRule;
    })(ValidationRule);
    exports.RegexValidationRule = RegexValidationRule;
    var MinimumValueValidationRule = (function (_super) {
        __extends(MinimumValueValidationRule, _super);
        function MinimumValueValidationRule(minimumValue) {
            _super.call(this, minimumValue, function (newValue, minimumValue) {
                return minimumValue <= newValue;
            });
        }
        return MinimumValueValidationRule;
    })(ValidationRule);
    exports.MinimumValueValidationRule = MinimumValueValidationRule;
    var MaximumValueValidationRule = (function (_super) {
        __extends(MaximumValueValidationRule, _super);
        function MaximumValueValidationRule(maximumValue) {
            _super.call(this, maximumValue, function (newValue, maximumValue) {
                return newValue < maximumValue;
            });
        }
        return MaximumValueValidationRule;
    })(ValidationRule);
    exports.MaximumValueValidationRule = MaximumValueValidationRule;
    var BetweenValueValidationRule = (function (_super) {
        __extends(BetweenValueValidationRule, _super);
        function BetweenValueValidationRule(minimumValue, maximumValue) {
            _super.call(this, { minimumValue: minimumValue, maximumValue: maximumValue }, function (newValue, threshold) {
                return threshold.minimumValue <= newValue && newValue < threshold.maximumValue;
            });
        }
        return BetweenValueValidationRule;
    })(ValidationRule);
    exports.BetweenValueValidationRule = BetweenValueValidationRule;
    var DigitValidationRule = (function (_super) {
        __extends(DigitValidationRule, _super);
        function DigitValidationRule() {
            var _this = this;
            this.digitRegex = /^\d+$/;
            _super.call(this, null, function (newValue, threshold) {
                return _this.digitRegex.test(newValue);
            });
        }
        return DigitValidationRule;
    })(ValidationRule);
    exports.DigitValidationRule = DigitValidationRule;
    var AlphaNumericValidationRule = (function (_super) {
        __extends(AlphaNumericValidationRule, _super);
        function AlphaNumericValidationRule() {
            var _this = this;
            this.alphaNumericRegex = /^[a-z0-9]+$/i;
            _super.call(this, null, function (newValue, threshold) {
                return _this.alphaNumericRegex.test(newValue);
            });
        }
        return AlphaNumericValidationRule;
    })(ValidationRule);
    exports.AlphaNumericValidationRule = AlphaNumericValidationRule;
    var AlphaNumericOrWhitespaceValidationRule = (function (_super) {
        __extends(AlphaNumericOrWhitespaceValidationRule, _super);
        function AlphaNumericOrWhitespaceValidationRule() {
            var _this = this;
            this.alphaNumericRegex = /^[a-z0-9\s]+$/i;
            _super.call(this, null, function (newValue, threshold) {
                return _this.alphaNumericRegex.test(newValue);
            });
        }
        return AlphaNumericOrWhitespaceValidationRule;
    })(ValidationRule);
    exports.AlphaNumericOrWhitespaceValidationRule = AlphaNumericOrWhitespaceValidationRule;
    var StrongPasswordValidationRule = (function (_super) {
        __extends(StrongPasswordValidationRule, _super);
        function StrongPasswordValidationRule(minimumComplexityLevel) {
            var complexityLevel = 4;
            if (minimumComplexityLevel && minimumComplexityLevel > 1 && minimumComplexityLevel < 4)
                complexityLevel = minimumComplexityLevel;
            _super.call(this, complexityLevel, function (newValue, threshold) {
                if (typeof (newValue) !== 'string')
                    return false;
                var strength = 0;
                strength += /[A-Z]+/.test(newValue) ? 1 : 0;
                strength += /[a-z]+/.test(newValue) ? 1 : 0;
                strength += /[0-9]+/.test(newValue) ? 1 : 0;
                strength += /[\W]+/.test(newValue) ? 1 : 0;
                return strength >= threshold;
            });
        }
        return StrongPasswordValidationRule;
    })(ValidationRule);
    exports.StrongPasswordValidationRule = StrongPasswordValidationRule;
    var EqualityValidationRule = (function (_super) {
        __extends(EqualityValidationRule, _super);
        function EqualityValidationRule(otherValue, equality, otherValueLabel) {
            _super.call(this, {
                otherValue: otherValue,
                equality: equality,
                otherValueLabel: otherValueLabel
            }, function (newValue, threshold) {
                if (newValue instanceof Date && threshold.otherValue instanceof Date)
                    return threshold.equality === (newValue.getTime() === threshold.otherValue.getTime());
                return threshold.equality === (newValue === threshold.otherValue);
            });
        }
        return EqualityValidationRule;
    })(ValidationRule);
    exports.EqualityValidationRule = EqualityValidationRule;
    var InCollectionValidationRule = (function (_super) {
        __extends(InCollectionValidationRule, _super);
        function InCollectionValidationRule(collection) {
            _super.call(this, collection, function (newValue, threshold) {
                for (var i = 0; i < collection.length; i++) {
                    if (newValue === collection[i])
                        return true;
                }
                return false;
            });
        }
        return InCollectionValidationRule;
    })(ValidationRule);
    exports.InCollectionValidationRule = InCollectionValidationRule;
});


text:
namespace Microsoft.Azure.Management.RecoveryServices.Backup.Models
{
    using Newtonsoft.Json;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// IaaS VM workload-specific backup item.
    /// </summary>
    public partial class AzureIaaSVMProtectedItem : ProtectedItem
    {
        /// <summary>
        /// Initializes a new instance of the AzureIaaSVMProtectedItem class.
        /// </summary>
        public AzureIaaSVMProtectedItem()
        {
            CustomInit();
        }

        /// <summary>
        /// Initializes a new instance of the AzureIaaSVMProtectedItem class.
        /// </summary>
        /// <param name="backupManagementType">Type of backup managemenent for
        /// the backed up item. Possible values include: 'Invalid',
        /// 'AzureIaasVM', 'MAB', 'DPM', 'AzureBackupServer', 'AzureSql',
        /// 'AzureStorage', 'AzureWorkload', 'DefaultBackup'</param>
        /// <param name="workloadType">Type of workload this item represents.
        /// Possible values include: 'Invalid', 'VM', 'FileFolder',
        /// 'AzureSqlDb', 'SQLDB', 'Exchange', 'Sharepoint', 'VMwareVM',
        /// 'SystemState', 'Client', 'GenericDataSource', 'SQLDataBase',
        /// 'AzureFileShare', 'SAPHanaDatabase'</param>
        /// <param name="containerName">Unique name of container</param>
        /// <param name="sourceResourceId">ARM ID of the resource to be backed
        /// up.</param>
        /// <param name="policyId">ID of the backup policy with which this item
        /// is backed up.</param>
        /// <param name="lastRecoveryPoint">Timestamp when the last (latest)
        /// backup copy was created for this backup item.</param>
        /// <param name="backupSetName">Name of the backup set the backup item
        /// belongs to</param>
        /// <param name="createMode">Create mode to indicate recovery of
        /// existing soft deleted data source or creation of new data source.
        /// Possible values include: 'Invalid', 'Default', 'Recover'</param>
        /// <param name="friendlyName">Friendly name of the VM represented by
        /// this backup item.</param>
        /// <param name="virtualMachineId">Fully qualified ARM ID of the
        /// virtual machine represented by this item.</param>
        /// <param name="protectionStatus">Backup status of this backup
        /// item.</param>
        /// <param name="protectionState">Backup state of this backup item.
        /// Possible values include: 'Invalid', 'IRPending', 'Protected',
        /// 'ProtectionError', 'ProtectionStopped', 'ProtectionPaused'</param>
        /// <param name="healthStatus">Health status of protected item.
        /// Possible values include: 'Passed', 'ActionRequired',
        /// 'ActionSuggested', 'Invalid'</param>
        /// <param name="healthDetails">Health details on this backup
        /// item.</param>
        /// <param name="lastBackupStatus">Last backup operation
        /// status.</param>
        /// <param name="lastBackupTime">Timestamp of the last backup operation
        /// on this backup item.</param>
        /// <param name="protectedItemDataId">Data ID of the protected
        /// item.</param>
        /// <param name="extendedInfo">Additional information for this backup
        /// item.</param>
        public AzureIaaSVMProtectedItem(string backupManagementType = default(string), string workloadType = default(string), string containerName = default(string), string sourceResourceId = default(string), string policyId = default(string), System.DateTime? lastRecoveryPoint = default(System.DateTime?), string backupSetName = default(string), string createMode = default(string), string friendlyName = default(string), string virtualMachineId = default(string), string protectionStatus = default(string), string protectionState = default(string), string healthStatus = default(string), IList<AzureIaaSVMHealthDetails> healthDetails = default(IList<AzureIaaSVMHealthDetails>), string lastBackupStatus = default(string), System.DateTime? lastBackupTime = default(System.DateTime?), string protectedItemDataId = default(string), AzureIaaSVMProtectedItemExtendedInfo extendedInfo = default(AzureIaaSVMProtectedItemExtendedInfo))
            : base(backupManagementType, workloadType, containerName, sourceResourceId, policyId, lastRecoveryPoint, backupSetName, createMode)
        {
            FriendlyName = friendlyName;
            VirtualMachineId = virtualMachineId;
            ProtectionStatus = protectionStatus;
            ProtectionState = protectionState;
            HealthStatus = healthStatus;
            HealthDetails = healthDetails;
            LastBackupStatus = lastBackupStatus;
            LastBackupTime = lastBackupTime;
            ProtectedItemDataId = protectedItemDataId;
            ExtendedInfo = extendedInfo;
            CustomInit();
        }

        /// <summary>
        /// An initialization method that performs custom operations like setting defaults
        /// </summary>
        partial void CustomInit();

        /// <summary>
        /// Gets or sets friendly name of the VM represented by this backup
        /// item.
        /// </summary>
        [JsonProperty(PropertyName = "friendlyName")]
        public string FriendlyName { get; set; }

        /// <summary>
        /// Gets or sets fully qualified ARM ID of the virtual machine
        /// represented by this item.
        /// </summary>
        [JsonProperty(PropertyName = "virtualMachineId")]
        public string VirtualMachineId { get; set; }

        /// <summary>
        /// Gets or sets backup status of this backup item.
        /// </summary>
        [JsonProperty(PropertyName = "protectionStatus")]
        public string ProtectionStatus { get; set; }

        /// <summary>
        /// Gets or sets backup state of this backup item. Possible values
        /// include: 'Invalid', 'IRPending', 'Protected', 'ProtectionError',
        /// 'ProtectionStopped', 'ProtectionPaused'
        /// </summary>
        [JsonProperty(PropertyName = "protectionState")]
        public string ProtectionState { get; set; }

        /// <summary>
        /// Gets or sets health status of protected item. Possible values
        /// include: 'Passed', 'ActionRequired', 'ActionSuggested', 'Invalid'
        /// </summary>
        [JsonProperty(PropertyName = "healthStatus")]
        public string HealthStatus { get; set; }

        /// <summary>
        /// Gets or sets health details on this backup item.
        /// </summary>
        [JsonProperty(PropertyName = "healthDetails")]
        public IList<AzureIaaSVMHealthDetails> HealthDetails { get; set; }

        /// <summary>
        /// Gets or sets last backup operation status.
        /// </summary>
        [JsonProperty(PropertyName = "lastBackupStatus")]
        public string LastBackupStatus { get; set; }

        /// <summary>
        /// Gets or sets timestamp of the last backup operation on this backup
        /// item.
        /// </summary>
        [JsonProperty(PropertyName = "lastBackupTime")]
        public System.DateTime? LastBackupTime { get; set; }

        /// <summary>
        /// Gets or sets data ID of the protected item.
        /// </summary>
        [JsonProperty(PropertyName = "protectedItemDataId")]
        public string ProtectedItemDataId { get; set; }

        /// <summary>
        /// Gets or sets additional information for this backup item.
        /// </summary>
        [JsonProperty(PropertyName = "extendedInfo")]
        public AzureIaaSVMProtectedItemExtendedInfo ExtendedInfo { get; set; }

    }
}


text:
#ifndef SVGA_DEBUG_H
#define SVGA_DEBUG_H

#include "pipe/p_compiler.h"
#include "util/u_debug.h"

#define DEBUG_DMA      0x1
#define DEBUG_TGSI     0x4
#define DEBUG_PIPE     0x8
#define DEBUG_STATE    0x10
#define DEBUG_SCREEN   0x20
#define DEBUG_TEX      0x40
#define DEBUG_SWTNL    0x80
#define DEBUG_CONSTS   0x100
#define DEBUG_VIEWPORT 0x200
#define DEBUG_VIEWS    0x400
#define DEBUG_PERF     0x800    /* print something when we hit any slow path operation */
#define DEBUG_FLUSH    0x1000   /* flush after every draw */
#define DEBUG_SYNC     0x2000   /* sync after every flush */
#define DEBUG_QUERY    0x4000
#define DEBUG_CACHE    0x8000

#ifdef DEBUG
extern int SVGA_DEBUG;
#define DBSTR(x) x
#else
#define SVGA_DEBUG 0
#define DBSTR(x) ""
#endif

static INLINE void
SVGA_DBG( unsigned flag, const char *fmt, ... )
{
#ifdef DEBUG
    if (SVGA_DEBUG & flag)
    {
        va_list args;

        va_start( args, fmt );
        debug_vprintf( fmt, args );
        va_end( args );
    }
#else
    (void)flag;
    (void)fmt;
#endif
}

#endif


text:
function save_options() {
	var id = document.getElementById('appId').value;

	chrome.storage.sync.set({
		injectedChromecastId: id
	}, function() {
		// Update status to let user know options were saved.
		var status = document.getElementById('status');
		status.textContent = 'Options saved.';
		setTimeout(function() {
			status.textContent = '';
		}, 750);
	});
}

// Restore the options when the page is loaded, with a default
// ID of a blank value
function restore_options() {
	chrome.storage.sync.get({
		injectedChromecastId: ''
	}, function(items) {
		document.getElementById('appId').value = items.injectedChromecastId;
	});
}

document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);


text:
from unittest import runner
import sys, time, os, sublime

class DeferringTextTestRunner(runner.TextTestRunner):
    r'''deferred test runner.

    This test runner runs tests in deferred slices.  It gives
    back control to sublime text, such that it can draw views,
    do syntax highlighting and whatever.
    '''

    def run(self, test):
        "Run the given test case or test suite."
        result = self._makeResult()
        runner.registerResult(result)
        result.failfast = self.failfast
        result.buffer = self.buffer
        startTime = time.time()
        startTestRun = getattr(result, 'startTestRun', None)

        if startTestRun is not None:
            startTestRun()

        deferred = test(result)

        def _stop_testing():
            stopTime = time.time()
            timeTaken = stopTime - startTime
            result.printErrors()
            if hasattr(result, 'separator2'):
                self.stream.writeln(result.separator2)
            run = result.testsRun
            self.stream.writeln("Ran %d test%s in %.3fs" %
                                (run, run != 1 and "s" or "", timeTaken))
            self.stream.writeln()

            expectedFails = unexpectedSuccesses = skipped = 0
            try:
                results = map(len, (result.expectedFailures,
                                    result.unexpectedSuccesses,
                                    result.skipped))
            except AttributeError:
                pass
            else:
                expectedFails, unexpectedSuccesses, skipped = results

            infos = []
            if not result.wasSuccessful():
                self.stream.write("FAILED")
                failed, errored = map(len, (result.failures, result.errors))
                if failed:
                    infos.append("failures=%d" % failed)
                if errored:
                    infos.append("errors=%d" % errored)
            else:
                self.stream.write("OK")
            if skipped:
                infos.append("skipped=%d" % skipped)
            if expectedFails:
                infos.append("expected failures=%d" % expectedFails)
            if unexpectedSuccesses:
                infos.append("unexpected successes=%d" % unexpectedSuccesses)
            if infos:
                self.stream.writeln(" (%s)" % (", ".join(infos),))
            else:
                self.stream.write("\n")
            return result

        def _wait_condition():
            result = self.condition()

            if not result:
                assert (time.time() - self.condition_start_time) < 10, "Timeout, waited longer than 10s till condition true"
                sublime.set_timeout(_wait_condition, 10)

            else:
                sublime.set_timeout(_continue_testing, 10)

        def _continue_testing():
            try:
                delay = next(deferred)

                if callable(delay):
                    self.condition = delay
                    self.condition_start_time = time.time()
                    sublime.set_timeout(_wait_condition, 10)

                else:
                    if not isinstance(delay, int):
                        delay = 10

                    sublime.set_timeout(_continue_testing, delay)

            except StopIteration:
                stopTestRun = getattr(result, 'stopTestRun', None)
                if stopTestRun is not None:
                    stopTestRun()

                _stop_testing()

        sublime.set_timeout(_continue_testing, 10)


text:
title: "Filter data before reading with awk and R"
excerpt: Command line tools + R to read large Global Fishing Watch data efficiently.
tagline: "Using awk & R to map Fishing Effort"
category: rstats
tags:
  - rspatial
  - GIS
  - awk
  - sed
  - GFW
  - ggplot2
header:
  overlay_image: /assets/images/featureSig.png
  overlay_filter: 0.5

---

For a recent project I was working with Fishing Effort data from the [Global Fishing Watch](https://globalfishingwatch.org/){:target="_blank"} (GFW) program. Fishing Effort information is shared by GFW as global spatially-explicit, gridded datasets with fishing vessel locations and whereabouts. These data are open (registration required), and available as daily csv files for each year (since 2012).

A folder with lots of files is a good way to practice iteration, and because the 366 files for 2020 totaled 12 GB in size, this is also a good dataset for trying to pre-process the data before reading it into R.

As I was getting ready to work with this source of data, I was searching for "r filter data before reading" and I found the [release post](https://www.tidyverse.org/blog/2019/05/vroom-1-0-0/){:target="_blank"} for vroom 1.0.0. The text mentioned how R can work with connections, and this led me to this [video](https://www.jimhester.com/post/2019-09-26-pipe-connections/){:target="_blank"} by Jim Hester about connections in R. Since R v.1.2.0, we can run system commands from R and have their output available for reading into our workflows. Connections, and in particular those created by the base function `pipe`, allow us to run shell commands inside other functions and access their results directly.

### awk
For working with delimited text files efficiently, the awk console utility is a good option, allowing us to use the AWK semantic analysis language to parse large files without crashing our computers, as we'll see in the example.

My overall goal was to read the data for the whole year, work out monthly fishing effort, and map it. If we have a year's worth of data, organized as one file per day with each file following a _YYYYMMDD_ naming scheme, we can:

**1 - Get all the file paths in the folder**
**2 - Group the files into a list element for each month**
**3 - Read the (29, 30 or 31) files for each month and bind them into a single object**
**4 - Summarize the values for each cell for each month**
**5 – Rasterize the results**
**6 – Plot them with some geographic context and nice colors**

Let's walk through the code.

### 1 - Get all the file paths in the folder
### 2 - Group the files into a list element for each month

With functions from `purrr` and `fs`, we can  get all the file paths in the folder and group the files into a list element for each month like so. If you register and download the 2020 Fishing Effort data (v2, 0.01° resolution), this should be reproducible.

{% highlight r %}
# Load packages (install first if you don't have them)
library(dplyr)              # CRAN v1.0.7
library(fs)                 # CRAN v1.5.0
library(sf)                 # CRAN v0.9-8
library(stringr)            # CRAN v1.4.0
library(purrr)              # CRAN v0.3.4
library(ggplot2)            # CRAN v3.3.3
library(rnaturalearthhires) # [github::ropensci/rnaturalearthhires] v0.2.0
library(terra)              # CRAN v1.2-10
library(stars)              # [github::r-spatial/stars] v0.5-3
library(naniar)             # CRAN v0.6.1
library(readr)              # CRAN v2.0.0
library(ggfx)               # CRAN v1.0.0
library(scico)              # CRAN v1.2.0
library(vroom)              # [github::r-lib/vroom] v1.5.3.9000

# path to the unzipped daily csvs
setwd("/your-path-here/fleet-daily-csvs-100-v2-2020")

# daily file names
dailies <- dir_ls(type = "file")
# months vector
monthsyr <- sprintf("%02d", 1:12)
# each month's files
monthliesTF <- paste0("-", monthsyr, "-") %>% map(~ str_detect(dailies, .x))

# subset iteratively
monthly_paths <- monthliesTF %>%
  map(~ dailies[.x]) %>%
  set_names(month.abb)

{% endhighlight %}

### 3 - Read the (29, 30 or 31) files for each month and bind them into a single object

The part that was new to me came in the third step of this workflow, when I needed to prefilter each csv file before importing it into R. This is where AWK saved my RAM and well-being. awk parses data line-by-line and doesn't need to read the whole file into memory to process it. In AWK, each line in a text file is a record (row) and each line is broken up into a sequence of fields (columns/variables). When awk reads a record, it splits the line into fields based on a delimiter (the input field separator), and we can refer to the fields by position using the dollar sign and their index ($1, $2, $3 hold the values of the first, second, and third fields respectively)

In the command line, we write awk commands like this:

{% highlight awk %}
awk 'statement' input.file
{% endhighlight %}

First we call awk, followed by a statement enclosed in single quotes, then our input file. Inside the statement, goes the {ACTION} to be taken on the records in the input file, enclosed in curly brackets.

To pre-filter csv files before reading them in R, we need a conditional expression to filter the data on the values of some of the columns, and then print them as delimited text which R can consume without issues.

Conditional expressions in the awk statement follow this syntax:

{% highlight awk %}
if (conditional-expression)  action
{% endhighlight %}

In this case, to filter records by maximum and minimum latitude and longitude, we first figure out which fields hold these values, and then build the conditional statement. I knew from the dataset description that latitude is the second column and longitude the third, so we can use relational (greater than, less than, etc.; >, <, ==, !=) and logical operators (and, or; &&, ||) to specify our filter.

The statement takes this general form

{% highlight awk %}
{if (conditional expression) {action} } input.file
{% endhighlight %}

To combine more conditions, we enclose each one in brackets and combine them with a logical operator
{% highlight awk %}
{if ((condition a) && (condition b)) {action} } input.file
{% endhighlight %}

Finally, the complete shell command will include a call to awk and a specification of the delimiter or input field separator using the `-F` option. In this case, a comma because we are working with csv files.

{% highlight awk %}
awk -F ',' '{ if (($2 > 5.89 && $2 < 13.86) && ($3 > -93.51 && $3 < -84.60)) { print } }' input.file
{% endhighlight %}

The maximum and minimum lat/long values for this example define a bounding box I drew manually, for waters off the coasts of El Salvador and Nicaragua. The action to take is to `{print}` the records that meet the conditions.

To do this iteratively for all the paths inside a list, which correspond to days in different months, we define a function that will paste each file path into the awk shell command, which goes inside a `pipe` call as an argument to`vroom::vroom`, along with a vector of column names.

{% highlight r %}

# Manual bounding box created with mapedit::drawFeatures()
# xmin: -93.51716 ymin: 5.892904 xmax: -84.60697 ymax: 13.85601

# define fn to prefilter & read
vroomprefilt <- function(inputfile) {
  colsfleet <- c(
    "date", "cell_ll_lat", "cell_ll_lon",
    "flag", "geartype", "hours", "fishing_hours",
    "mmsi_present"
  )
  vroom(pipe(paste("awk -F ',' '{ if (($2 > 5.89 && $2 < 13.86) && ($3 > -93.51 && $3 < -84.60)) { print } }'", inputfile)),
    col_names = colsfleet
  )
}

{% endhighlight %}

Now we can iterate through all the months and days, drop the empty tibbles (days with no fishing effort recorded in the area) with `discard` and a predicate function.

{% highlight r %}

# read and filter all months
allEffort <- map(month.abb, ~ monthly_paths[[.x]]) %>% map_depth(2, vroomprefilt)
# drop empty tibbles
allEffort <- allEffort %>% map(~ discard(.x, ~ nrow(.x) == 0))

{% endhighlight %}

### 4 - Summarize the values for each cell for each month

We are ready to summarize the monthly data for each grid cell. To summarize by month, we can define a function to bind the daily tibbles into a single object for the whole month and then use `dplyr` to group by lat/long combinations and then summarize the total hours by grid cell. Because the xy data describes the lower left corner of each cell, we can add half the length of a grid cell to each value to shift the coordinates to the cell centers, for rasterization.

{% highlight r %}

# summarize data for each grid cell
get_cellEffort <- function(monthlyEffortdf) {
  monthlydf <- map_df(monthlyEffortdf, bind_rows)
  summ_df <- monthlydf %>%
    mutate(
      cell_ll_lat = cell_ll_lat + 0.005,
      cell_ll_lon = cell_ll_lon + 0.005
    ) %>%
    group_by(cell_ll_lat, cell_ll_lon) %>%
    summarize(
      sum_fishing_hours = sum(fishing_hours, na.rm = T),
      .groups = "drop"
    ) %>%
    naniar::replace_with_na(replace = list(sum_fishing_hours = 0))
}

# summarized data
monthly_xydfs <- map(allEffort, get_cellEffort)

{% endhighlight %}

#### 5 – Rasterize the results

To produce raster files for each month, I defined a function that creates a matrix with the xy coordinates and fishing hours, then creates a raster with `terra::rast` (really fast!), and ultimately coerces the output to a `stars` object. This was simply personal choice. The spatial data can be used as is or as a SpatRaster.

{% highlight r %}

# rasterize each month's df
rasterizedf <- function(xyzdat) {
  xyzobj <- as.matrix.data.frame(xyzdat[c("cell_ll_lon", "cell_ll_lat", "sum_fishing_hours")])
  fishrast <- terra::rast(x = xyzobj, type = "xyz", crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
  st_as_stars(fishrast)
}

# raster for each month
monthly_rasters <- map(monthly_xydfs, rasterizedf)
names(monthly_rasters) <- month.abb

{% endhighlight %}

#### 6 – Plot the rasters with some geographic context and nice colors

Finally, we can plot the data for one or more of the months. I chose June for no particular reason. For this, we just need a coastline from `rnaturalearth` and then we can use `ggplot` to draw the raster and the simple feature coastline. I threw in an outer glow from `ggfx` for a 'firefly map' effect.

{% highlight r %}

# get coastline
library(rnaturalearth) # CRAN v0.1.0
divpol <- ne_countries(scale = 10, country = c("El Salvador", "Nicaragua", "Honduras", "Costa Rica"), returnclass = "sf")
# bounding box
limspat <- st_bbox(st_as_sf(monthly_xydfs[[6]], coords = c("cell_ll_lon", "cell_ll_lat")))
# crop coastline
divpol <- st_crop(divpol, limspat)

# draw
ggplot() +
  geom_sf(data = divpol, size = 0.06, fill = "#212529", color = "white") +
  with_outer_glow(geom_stars(data = monthly_rasters[[6]]), colour = "#ffaa00", sigma = 3, expand = 2) +
  scale_fill_scico(na.value = "transparent", palette =  "lajolla", direction = 1, guide=FALSE) +
  coord_sf(
    expand = FALSE,
    xlim = c(limspat["xmin"], limspat["xmax"]),
    ylim = c(limspat["ymin"], limspat["ymax"])
  ) +
  theme(
    panel.background = element_rect(fill = "#14213d"),
    panel.grid = element_blank()
  )+labs(x="",y="")

{% endhighlight %}

For a quick draft, the map looks good.

Looks OK

This post was mainly a way for me to structure my thoughts and learning process on awk, but it really taught me how 'old-school' command line tools can work with R. Running some benchmarks early on, I found this crazy difference between reading the files with pre-processing versus using `dplyr` after reading each one.

|expr             | mem_alloc| total_time|
|:----------------|---------:|----------:|
|pre-filtering    |     247MB|      2.76m|
|read then filter |      12GB|      4.61m|

Timewise, pre-filtering is faster, but the bigger difference is in memory use. 12GB is enough to crash many laptops.

The amount of Real Data Science™ you can do with grep, cut, awk, sed, and perl -pie is truly astounding.
— Stephen Turner (@strnr) August 5, 2021

I only work with big(ish) data occasionally, but will definitely keep using and learning command line tools and their integration with R moving forward.

Here are some good resources I found on awk:

[https://www.tim-dennis.com/data/tech/2016/08/09/using-awk-filter-rows.html](https://www.tim-dennis.com/data/tech/2016/08/09/using-awk-filter-rows.html){:target="_blank"}
[https://www.thegeeksearch.com/beginners-guide-to-using-awk-in-shell-scripts/](https://www.thegeeksearch.com/beginners-guide-to-using-awk-in-shell-scripts/){:target="_blank"}
[http://linuxhandouts.com/awk-for-beginners/](http://linuxhandouts.com/awk-for-beginners/){:target="_blank"}

awk pic.twitter.com/eNEtB3KueU
— 🔎Julia Evans🔍 (@b0rk) May 27, 2018

And here's an official [guide](https://globalfishingwatch.org/data/working-with-our-downloadable-public-data-in-r/
){:target="_blank"}  for working with GFW fishing effort in R:

Feel free to contact me with any comments or questions.


text:
package de.fips.util.tinybinding;

import static de.fips.util.tinybinding.Observables.observe;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.*;

import java.awt.Container;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import de.fips.util.tinybinding.impl.Observable;
import de.fips.util.tinybinding.pojo.PojoObservable;
import de.fips.util.tinybinding.swing.SwingObservable;

/**
 * Tests {@link Observables}.
 */
@RunWith(JUnit4.class)
public class ObservablesTest {

	@Test
	public void test_observeContainerCreatesSwingObservable() {
		assertThat(observe(mock(Container.class))).isInstanceOf(SwingObservable.class);
	}

	@Test
	public void test_observeObjectCreatesPojoObservable() {
		assertThat(observe(mock(Object.class))).isInstanceOf(PojoObservable.class);
	}

	@Test
	public void test_observeCreatesObservable() {
		assertThat(observe()).isInstanceOf(Observable.class);
	}
}


text:
'use strict';

var Immutable = require('immutable');
var path = require('./path');

var tryParseInt = function(n) {
  var int = parseInt(n);
  return isNaN(int) ? n : int;
};

var primitivePatch = function (op, value) {
  if (op === 'add' || op === 'replace') {
    return value;
  } else if (op === 'remove') {
    return null;
  }
};

var mapPatch = function(map, firstPath, restPath, op, value) {
  if (op === 'add') {
    if (restPath.length > 0 && map.get(firstPath) === undefined) {
      var baseValue = (restPath[0].match(/^\d+$/)) ? Immutable.List() : Immutable.Map();
      return map.set(firstPath, anyPatch(baseValue, restPath, op, value));
    } else {
      return map.set(firstPath, anyPatch(map.get(firstPath), restPath, op, value));
    }
  } else if (op === 'replace') {
    return map.set(firstPath, anyPatch(map.get(firstPath), restPath, op, value));
  } else if (op === 'remove') {
    if (restPath.length > 0) {
      return map.set(firstPath, anyPatch(map.get(firstPath), restPath, op, value));
    } else {
      return map.remove(firstPath);
    }
  } else {
    throw new Error('map patch Error, unknown op: ' + op);
  }
};

var sequencePatch = function(sequence, firstPath, restPath, op, value) {
  firstPath = tryParseInt(firstPath);
  if (op === 'add') {
    if (sequence.get(firstPath) === undefined) {
      if (restPath.length > 0) {
        var baseValue = (restPath[0].match(/^\d+$/)) ? Immutable.List() : Immutable.Map();
        return sequence.set(firstPath, anyPatch(baseValue, restPath, op, value));
      } else {
        // special case, return the value
        return sequence.splice(firstPath, 0, value);
      }
    } else {
      if (restPath.length > 0) {
        return sequence.set(firstPath, anyPatch(sequence.get(firstPath), restPath, op, value));
      } else {
        // special case, return the value
        return sequence.splice(firstPath, 0, value);
      }
    }
  } else if (op === 'replace') {
    return sequence.set(firstPath, anyPatch(sequence.get(firstPath), restPath, op, value));
  } else if (op === 'remove') {
    if (restPath.length > 0) {
      return sequence.set(firstPath, anyPatch(sequence.get(firstPath), restPath, op, value));
    } else {
      return sequence.remove(firstPath);
    }
  } else {
    throw new Error('sequence patch Error, unknown op: ' + op);
  }
};

var anyPatch = function(any, pathArray, op, value) {
  var firstPath, restPath;

  if (Immutable.Iterable.isKeyed(any)) {
    if (pathArray.length === 0) { return any; }
    firstPath = pathArray[0];
    restPath = pathArray.slice(1);
    return mapPatch(any, firstPath, restPath, op, value);
  } else if (Immutable.Iterable.isIndexed(any)) {
    if (pathArray.length === 0) { return any; }
    firstPath = pathArray[0];
    restPath = pathArray.slice(1);
    return sequencePatch(any, firstPath, restPath, op, value);
  } else {
    if (pathArray.length === 0) { return value; }
    return primitivePatch(op, value);
  }
};

var eachPatch = function(value, patches) {
  if (patches.count() === 0) { return value; }
  var firstPatch = patches.get(0);
  var restPatches = patches.slice(1);

  var pathArray = firstPatch.get('path').split('/').slice(1).map(path.unescape);
  var result = anyPatch(value, pathArray, firstPatch.get('op'), firstPatch.get('value'));
  return eachPatch(result, restPatches);
};

module.exports = eachPatch;


text:
namespace Microsoft.Azure.Management.Network.Fluent
{
    using Microsoft.Azure.Management.Network.Fluent.Models;
    using Microsoft.Azure.Management.ResourceManager.Fluent.Core;

    /// <summary>
    /// A client-side representation of an application gateway authentication certificate.
    /// </summary>
    public interface IApplicationGatewayAuthenticationCertificate :
        Microsoft.Azure.Management.ResourceManager.Fluent.Core.IBeta,
        Microsoft.Azure.Management.ResourceManager.Fluent.Core.IHasInner<Models.ApplicationGatewayAuthenticationCertificateInner>,
        Microsoft.Azure.Management.ResourceManager.Fluent.Core.IChildResource<Microsoft.Azure.Management.Network.Fluent.IApplicationGateway>
    {
        /// <summary>
        /// Gets base-64 encoded bytes of the X.509 certificate.
        /// </summary>
        string Data { get; }
    }
}


text:
using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Microsoft.Practices.ObjectBuilder;
using System.Web.Security;
using System.Net.Mail;
using System.Net;

namespace PollGenerator.Shell.Views
{
    public partial class NaujasVartotojas : Microsoft.Practices.CompositeWeb.Web.UI.Page, INaujasVartotojasView
    {
        private NaujasVartotojasPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public NaujasVartotojasPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

            AppSettingsSection conSection = config.AppSettings;

            //string systemName = conSection.Settings["MasterHeader"].Value;

            //if (systemName.Length > 0)
            //{
            //    systemName += " - ";
            //}

            //e.Message.Subject = string.Format("{0}Jūsų vartotojo duomenys", systemName);

            e.Message.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnFailure;

            e.Message.Body = e.Message.Body.Replace("<%PasswordQuestion%>", CreateUserWizard1.Question);
            e.Message.Body = e.Message.Body.Replace("<%PasswordAnswer%>", CreateUserWizard1.Answer);

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

            smtpClient.EnableSsl = true;

            smtpClient.Credentials = new NetworkCredential("isgalvotas@gmail.com", "a39nikotinas");

            //smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;

            e.Message.From = new MailAddress("info@klausiu.lt", "Klausiu.lt");

            e.Message.Sender = new MailAddress("info@klausiu.lt");

            e.Message.ReplyTo = new MailAddress("info@klausiu.lt");

            smtpClient.Send(e.Message);

            e.Cancel = true;

        }

        protected void ContinueButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Poll/ManoApklausos.aspx");
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            //MembershipUser Vartotojas = sender(MembershipUser);
            Roles.AddUserToRole(CreateUserWizard1.UserName, "Registruotas");
        }
}
}