/*******************************************************************\

Module: C Language Conversion

Author: Daniel Kroening, kroening@cs.cmu.edu

\*******************************************************************/

#include <assert.h>

#include <arith_tools.h>
#include <i2string.h>

#include <ansi-c/c_types.h>
#include <ansi-c/unescape_string.h>

#include "convert_character_literal.h"

/*******************************************************************\

Function: convert_character_literal

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void convert_character_literal(
  const std::string &src,
  std::string &dest)
{
  dest="";

  assert(src.size()>=2);
  assert(src[0]=='\'');
  assert(src[src.size()-1]=='\'');

  unescape_string(std::string(src, 1, src.size()-2), dest);
}

/*******************************************************************\

Function: convert_character_literal

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void convert_character_literal(
  const std::string &src,
  exprt &dest)
{
  std::string value;
  convert_character_literal(src, value);
  
  if(value.size()==0)
    throw "empty character literal";
  else if(value.size()==1)
    dest=from_integer(value[0], char_type());
  else if(value.size()>=2 && value.size()<=4)
  {
    mp_integer x=0;
    
    for(unsigned i=0; i<value.size(); i++)
    {
      mp_integer z=(unsigned char)(value[i]);
      z=z<<((value.size()-i-1)*8);
      x+=z;
    }
    
    dest=from_integer(x, int_type());
  }
  else
  {
    std::cout << "XX: " << src << std::endl;
    throw "literals with "+i2string(value.size())+
          " characters are not supported";
  }
}
