<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "representations.h"

#include "zorbaerrors/error_manager.h"
#include "zorbaerrors/Assert.h"

#include <zorba/util/path.h>

#include "URI.h"

#include "util/ascii_util.h"
#include "util/utf8_util.h"
#include "util/string_util.h"
#include "zorbautils/string_util.h"
#include "zorbatypes/xqpstring.h"


using namespace std;

namespace zorba {

  /*
  ---------------------------------------------------------------------------
  URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  absoluteURI   = scheme ":" ( hier_part | opaque_part )
  relativeURI   = ( net_path | abs_path | rel_path ) [ "?" query ]
  ---------------------------------------------------------------------------
  query         = *uric

  fragment      = *uric
---------------------------------------------------------------------------
  */

/*******************************************************************************
  is the given char a hex allowed one
********************************************************************************/
bool URI::is_hex(uint32_t c)
{
  return (is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_alpha(uint32_t c)
{
  if ((( c >= 'a' ) && ( c <= 'z' )) || (( c >= 'A' ) && ( c <= 'Z' )) )
    return true;

  return false;
}


/*******************************************************************************

********************************************************************************/
bool URI::is_digit(uint32_t c)
{
  if (( c >= '0' ) && ( c <= '9' ))
    return true;
 
  return false;
}


/*******************************************************************************

********************************************************************************/
bool URI::is_alphanum(uint32_t c)
{
  return (is_alpha(c) || is_digit(c));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_unreserved_char(uint32_t c)
{
  std::string lMarkCharacter("-_.!~*'{}");
  return (is_alphanum(c) || (c == ' ') || (c > 127 ) ||
    ((c<128) && lMarkCharacter.find((char)c) != std::string::npos));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_path_character(uint32_t c)
{
  std::string lPathChracters(";/\\:@&=+$,");
  return ((c<128) && lPathChracters.find((char)c) != std::string::npos);
}


/*******************************************************************************

********************************************************************************/
bool URI::is_reservered_or_unreserved_char(uint32_t c)
{
  std::string lMarkOrReservedChars("-_.!~*'();/?:@&=+$,[]");
  return (is_alphanum(c) ||
          (c == ' ') ||
          ((c<128) && lMarkOrReservedChars.find((char)c) != std::string::npos));
}


/*******************************************************************************
  Note: The encode_file_URI and decode_file_URI methods are hacks. Doing it
  properly would require boost-filesystem (or equivalent code).

  The decode_file_URI() method takes a file URI and converts it to a filepath
  in the native OS foramt.
********************************************************************************/
void URI::decode_file_URI(const zstring& uri, zstring& filepath)
{
  // TODO: file://localhost/
#ifdef WIN32
  if (uri.compare(0, 8, "file:///") == 0 && 
      ((uri.compare(9, 1, ":") == 0) || (uri.compare(9, 4, "%3A/") == 0))) 
  {
    zstring tmp(uri.c_str() + 8);
    ascii::uri_decode(filepath);
  }
  else
#endif
    if (uri.compare(0, 8, "file:///") == 0) 
    {
      zstring tmp(uri.c_str() + 7);
      ascii::uri_decode(tmp, &filepath);
    }<commit_msg>Fixing URI resolver on Windows.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "representations.h"

#include "zorbaerrors/error_manager.h"
#include "zorbaerrors/Assert.h"

#include <zorba/util/path.h>

#include "URI.h"

#include "util/ascii_util.h"
#include "util/utf8_util.h"
#include "util/string_util.h"
#include "zorbautils/string_util.h"
#include "zorbatypes/xqpstring.h"


using namespace std;

namespace zorba {

  /*
  ---------------------------------------------------------------------------
  URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  absoluteURI   = scheme ":" ( hier_part | opaque_part )
  relativeURI   = ( net_path | abs_path | rel_path ) [ "?" query ]
  ---------------------------------------------------------------------------
  query         = *uric

  fragment      = *uric
---------------------------------------------------------------------------
  */

/*******************************************************************************
  is the given char a hex allowed one
********************************************************************************/
bool URI::is_hex(uint32_t c)
{
  return (is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_alpha(uint32_t c)
{
  if ((( c >= 'a' ) && ( c <= 'z' )) || (( c >= 'A' ) && ( c <= 'Z' )) )
    return true;

  return false;
}


/*******************************************************************************

********************************************************************************/
bool URI::is_digit(uint32_t c)
{
  if (( c >= '0' ) && ( c <= '9' ))
    return true;
 
  return false;
}


/*******************************************************************************

********************************************************************************/
bool URI::is_alphanum(uint32_t c)
{
  return (is_alpha(c) || is_digit(c));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_unreserved_char(uint32_t c)
{
  std::string lMarkCharacter("-_.!~*'{}");
  return (is_alphanum(c) || (c == ' ') || (c > 127 ) ||
    ((c<128) && lMarkCharacter.find((char)c) != std::string::npos));
}


/*******************************************************************************

********************************************************************************/
bool URI::is_path_character(uint32_t c)
{
  std::string lPathChracters(";/\\:@&=+$,");
  return ((c<128) && lPathChracters.find((char)c) != std::string::npos);
}


/*******************************************************************************

********************************************************************************/
bool URI::is_reservered_or_unreserved_char(uint32_t c)
{
  std::string lMarkOrReservedChars("-_.!~*'();/?:@&=+$,[]");
  return (is_alphanum(c) ||
          (c == ' ') ||
          ((c<128) && lMarkOrReservedChars.find((char)c) != std::string::npos));
}


/*******************************************************************************
  Note: The encode_file_URI and decode_file_URI methods are hacks. Doing it
  properly would require boost-filesystem (or equivalent code).

  The decode_file_URI() method takes a file URI and converts it to a filepath
  in the native OS foramt.
********************************************************************************/
void URI::decode_file_URI(const zstring& uri, zstring& filepath)
{
  // TODO: file://localhost/
#ifdef WIN32
  if (uri.compare(0, 8, "file:///") == 0 && 
      ((uri.compare(9, 1, ":") == 0) || (uri.compare(9, 4, "%3A/") == 0))) 
  {
    zstring tmp(uri.c_str() + 8);
    ascii::uri_decode(tmp, &filepath);
  }
  else
#endif
    if (uri.compare(0, 8, "file:///") == 0) 
    {
      zstring tmp(uri.c_str() + 7);
      ascii::uri_decode(tmp, &filepath);
    }
{'fraction_non_alphanumeric': 0.44631601825690065, 'fraction_numerical': 0.012605955227124538, 'mean_word_length': 3.882228116710875, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7450250', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2378, 'n_words': 579}