Ejemplo n.º 1
0
/*
 * csql_db_value_as_string() - convert DB_VALUE to string
 *   return: formatted string
 *   value(in): value to convert
 *   length(out): length of output string
 *   plain_output(in): refine string for plain output
 */
char *
csql_db_value_as_string (DB_VALUE * value, int *length, bool plain_string)
{
  char *result = NULL;
  int len = 0;

  if (value == NULL)
    {
      return (NULL);
    }

  switch (DB_VALUE_TYPE (value))
    {
    case DB_TYPE_BIGINT:
      result =
	bigint_to_string (DB_GET_BIGINT (value), default_bigint_profile.fieldwidth, default_bigint_profile.leadingzeros,
			  default_bigint_profile.leadingsymbol, default_bigint_profile.commas,
			  default_bigint_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_INTEGER:
      result =
	bigint_to_string (DB_GET_INTEGER (value), default_int_profile.fieldwidth, default_int_profile.leadingzeros,
			  default_int_profile.leadingsymbol, default_int_profile.commas, default_int_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_SHORT:
      result =
	bigint_to_string (SHORT_TO_INT (DB_GET_SHORT (value)), default_short_profile.fieldwidth,
			  default_short_profile.leadingzeros, default_short_profile.leadingsymbol,
			  default_short_profile.commas, default_short_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_FLOAT:
      result =
	double_to_string ((double) DB_GET_FLOAT (value), default_float_profile.fieldwidth,
			  default_float_profile.precision, default_float_profile.leadingsign, NULL, NULL,
			  default_float_profile.leadingzeros, default_float_profile.trailingzeros,
			  default_float_profile.commas, default_float_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_DOUBLE:
      result =
	double_to_string (DB_GET_DOUBLE (value), default_double_profile.fieldwidth, default_double_profile.precision,
			  default_double_profile.leadingsign, NULL, NULL, default_double_profile.leadingzeros,
			  default_double_profile.trailingzeros, default_double_profile.commas,
			  default_double_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_NUMERIC:
      result = numeric_to_string (value, default_numeric_profile.commas);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_VARCHAR:
    case DB_TYPE_CHAR:
      {
	int dummy, bytes_size, decomp_size;
	bool need_decomp = false;
	char *str;
	char *decomposed = NULL;

	str = db_get_char (value, &dummy);
	bytes_size = db_get_string_size (value);
	if (bytes_size > 0 && DB_GET_STRING_CODESET (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, '\0', bytes_size, &len, plain_string);

	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }

      }
      break;
    case DB_TYPE_VARNCHAR:
    case DB_TYPE_NCHAR:
      {
	int dummy, bytes_size, decomp_size;
	bool need_decomp = false;
	char *str;
	char *decomposed = NULL;

	str = db_get_char (value, &dummy);
	bytes_size = db_get_string_size (value);
	if (bytes_size > 0 && DB_GET_STRING_CODESET (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, 'N', bytes_size, &len, plain_string);

	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }
      }
      break;
    case DB_TYPE_VARBIT:
    case DB_TYPE_BIT:
      result = bit_to_string (value, default_string_profile.string_delimiter, plain_string);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_OBJECT:
      result = object_to_string (DB_GET_OBJECT (value), get_object_print_format ());
      if (result == NULL)
	{
	  result = duplicate_string ("NULL");
	}
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_VOBJ:
      result = object_to_string (DB_GET_OBJECT (value), get_object_print_format ());
      if (result == NULL)
	{
	  result = duplicate_string ("NULL");
	}
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_SET:
    case DB_TYPE_MULTISET:
    case DB_TYPE_SEQUENCE:
      result =
	set_to_string (value, default_set_profile.begin_notation, default_set_profile.end_notation,
		       default_set_profile.max_entries, plain_string);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_TIME:
      {
	char buf[TIME_BUF_SIZE];
	if (db_time_to_string (buf, sizeof (buf), DB_GET_TIME (value)))
	  {
	    result = duplicate_string (buf);
	  }
	if (result)
	  {
	    len = strlen (result);
	  }
	break;
      }
    case DB_TYPE_TIMETZ:
      {
	char buf[TIMETZ_BUF_SIZE];
	DB_TIMETZ *time_tz = DB_GET_TIMETZ (value);
	if (db_timetz_to_string (buf, sizeof (buf), &(time_tz->time), &time_tz->tz_id))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMELTZ:
      {
	char buf[TIMETZ_BUF_SIZE];

	if (db_timeltz_to_string (buf, sizeof (buf), DB_GET_TIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_MONETARY:
      {
	char *leading_str = NULL;
	char *trailing_str = NULL;
	DB_CURRENCY currency = ((DB_GET_MONETARY (value))->type);

	if (default_monetary_profile.currency_symbol)
	  {
	    if (intl_get_currency_symbol_position (currency) == 1)
	      {
		trailing_str = intl_get_money_symbol_console (currency);
	      }
	    else
	      {
		leading_str = intl_get_money_symbol_console (currency);
	      }
	  }

	result =
	  (DB_GET_MONETARY (value) == NULL) ? NULL : double_to_string ((DB_GET_MONETARY (value))->amount,
								       default_monetary_profile.fieldwidth,
								       default_monetary_profile.decimalplaces,
								       default_monetary_profile.leadingsign,
								       leading_str, trailing_str,
								       default_monetary_profile.leadingzeros,
								       default_monetary_profile.trailingzeros,
								       default_monetary_profile.commas,
								       DOUBLE_FORMAT_DECIMAL);

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATE:
      /* default format for all locales */
      result = date_as_string (DB_GET_DATE (value), default_date_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_UTIME:
      {
	char buf[TIMESTAMP_BUF_SIZE];
	if (db_utime_to_string (buf, sizeof (buf), DB_GET_UTIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMESTAMPTZ:
      {
	char buf[TIMESTAMPTZ_BUF_SIZE];
	DB_TIMESTAMPTZ *ts_tz = DB_GET_TIMESTAMPTZ (value);
	if (db_timestamptz_to_string (buf, sizeof (buf), &(ts_tz->timestamp), &(ts_tz->tz_id)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMESTAMPLTZ:
      {
	char buf[TIMESTAMPTZ_BUF_SIZE];

	if (db_timestampltz_to_string (buf, sizeof (buf), DB_GET_UTIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIME:
      {
	char buf[DATETIME_BUF_SIZE];
	if (db_datetime_to_string (buf, sizeof (buf), DB_GET_DATETIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIMETZ:
      {
	char buf[DATETIMETZ_BUF_SIZE];
	DB_DATETIMETZ *dt_tz = DB_GET_DATETIMETZ (value);
	if (db_datetimetz_to_string (buf, sizeof (buf), &(dt_tz->datetime), &(dt_tz->tz_id)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIMELTZ:
      {
	char buf[DATETIMETZ_BUF_SIZE];

	if (db_datetimeltz_to_string (buf, sizeof (buf), DB_GET_DATETIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_NULL:
      result = duplicate_string ("NULL");
      if (result)
	{
	  len = strlen (result);
	}
      break;

    case DB_TYPE_BLOB:
    case DB_TYPE_CLOB:
      {
	DB_ELO *elo = DB_GET_ELO (value);

	if (elo != NULL)
	  {
	    result = duplicate_string (elo->locator);
	  }

	if (result != NULL)
	  {
	    len = strlen (result);
	  }
      }
      break;

    case DB_TYPE_ENUMERATION:
      {
	int bytes_size, decomp_size;
	bool need_decomp = false;
	const char *str;
	char *decomposed = NULL;

	if (db_get_enum_short (value) == 0 && db_get_enum_string (value) == NULL)
	  {
	    /* ENUM special error value */
	    str = "";
	    bytes_size = 0;
	  }
	else
	  {
	    str = db_get_enum_string (value);
	    bytes_size = db_get_enum_string_size (value);
	  }
	if (bytes_size > 0 && db_get_enum_codeset (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, '\0', bytes_size, &len, plain_string);
	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }
      }
      break;

    default:
      {
	char temp_buffer[256];

	(void) sprintf (temp_buffer, "<%s>", db_get_type_name (DB_VALUE_TYPE (value)));
	result = duplicate_string (temp_buffer);
	if (result)
	  {
	    len = strlen (result);
	  }
      }
    }

  if (length)
    {
      *length = len;
    }
  return result;
}
Ejemplo n.º 2
0
void
grab_email(struct crawlinfo *ci, set *acl_allow, set *acl_deny, char *url, char *sid, size_t contentlen, time_t lastmodified, char *usersid, CURL **curl)
{
	size_t len;

	/* Is it the parent? */
	struct ex_buffer mail;
	struct crawldocumentExistFormat crawldocumentExist;
	struct crawldocumentAddFormat crawldocumentAdd;

	len = strlen(url);

	//printf("%s\n", cur->str);
	crawldocumentExist.documenturi = make_crawl_uri(url, sid);
	crawldocumentExist.lastmodified = lastmodified;
	if (crawldocumentExist.documenturi == NULL) {
		(ci->documentError)(ci->collection ,1, "Could not allocate memory for documenturi");
		return;
	}
	if ((ci->documentExist)(ci->collection, &crawldocumentExist)) {
		// This document already exists
	} else {

		if (ex_getEmail(url, &mail, curl) == NULL) {
			free(crawldocumentExist.documenturi);
			return;
		}

		// Let's add it
		crawldocumentAdd.documenturi = crawldocumentExist.documenturi;
		/* Find the subject */
		//printf("RAW EMAIL:\n%.4096s\n", mail.buf);
		//printf("ANALYZE ON!!\n\n");
		container	*M_header = mail_analyze_header(mail.buf, mail.size-1);
#if 0
		iterator	it = multimap_begin(M_header);
		for (; it.valid; it=multimap_next(it))
		    {
			printf("KEY( %s ): VALUE( %s )\n", (char*)multimap_key(it).ptr, (char*)multimap_val(it).ptr);
		    }
		printf("\nANALYZE OFF!!\n");
		destroy(M_header);
		p = NULL;
#endif

		parse_mail_header(M_header, 
			&crawldocumentAdd.attributes, 
			&crawldocumentAdd.title);
		destroy(M_header);
		

		crawldocumentAdd.documenttype = "eml";
		crawldocumentAdd.doctype = "";
		crawldocumentAdd.document = mail.buf;
		crawldocumentAdd.dokument_size = mail.size-1; // Last byte is string null terminator
		crawldocumentAdd.lastmodified = lastmodified;

		//hvis vi har blitt sendt en user sid så bruker vi den som acl.
		if (usersid == NULL) {
			crawldocumentAdd.acl_allow = set_to_string(acl_allow, ",");
			crawldocumentAdd.acl_denied = set_to_string(acl_deny, ",");
		}
		else {
			crawldocumentAdd.acl_allow = strdup(usersid);
			crawldocumentAdd.acl_denied = strdup("");
		}
#if 0
		buffer		*B = buffer_init(-1);
		int		to_i=1, from_i=1;
		it = multimap_begin(M_header);
		for (; it.valid; it=multimap_next(it))
		    {
			if (!strcmp("from", (char*)multimap_key(it).ptr) && from_i<=4)
			    bprintf(B, "from_%i=%s,", from_i++, multimap_val(it).ptr);
			else if (!strcmp("to", (char*)multimap_key(it).ptr) && to_i<=4)
			    bprintf(B, "to_%i=%s,", to_i++, multimap_val(it).ptr);
		    }

		crawldocumentAdd.attributes = buffer_exit(B);
#endif

		bblog(INFO, "Adding: '%s'", crawldocumentAdd.title);
		bblog(INFO, "usersid \"%s\"acl_allow \"%s\" acl_denied \"%s\"",usersid,crawldocumentAdd.acl_allow , crawldocumentAdd.acl_denied);
		(ci->documentAdd)(ci->collection, &crawldocumentAdd);
		if (crawldocumentAdd.title[0] != '\0')
			free(crawldocumentAdd.title);
		free(crawldocumentAdd.acl_allow);
		free(crawldocumentAdd.acl_denied);
#ifdef EMAIL_ADRESS_AS_ATTRIBUTE
		bblog(INFO, "attributes: %s", crawldocumentAdd.attributes);
		free(crawldocumentAdd.attributes);
#else
		bblog(WARN, "not compiled with attributes");
#endif

		free(mail.buf);
	}
	free(crawldocumentExist.documenturi);
}