예제 #1
0
파일: itoa.c 프로젝트: syrotkin/practice
char* my_itoa(int input) {
  int is_negative = 0;
  if (input < 0) {
    is_negative = 1;
    input = -1 * input; // Important! Otherwise we get last character incorrectly.
  }
  if (0 == input) {
    char* r1 = (char*)malloc(sizeof(char)*2);
    r1[0] = '0';
    r1[1] = '\0';
    return r1;
  }
  
  int length = number_of_digits(input, is_negative);
  char* result = (char*)malloc(sizeof(char)*(length + 1)); 
  result[length] = '\0';
  int i = length - 1;
  while (input != 0) {
    int last = input % 10;
    input = input / 10;
    result[i] = last + '0'; // Convert the digit to character representation
    --i;
  }
  if (is_negative) {
    result[i] = '-';
  }
  
  return result;
}
예제 #2
0
파일: dump.c 프로젝트: ajinkya93/netbsd-src
void
dump_partition_map(partition_map_header *map, int disk_order)
{
    partition_map * entry;
    int max_type_length;
    int max_name_length;
    int digits;
    char *alternate;

    if (map == NULL) {
	bad_input("No partition map exists");
	return;
    }
    alternate = get_linux_name(map->name);
    if (alternate) {
	printf("\nPartition map (with %d byte blocks) on '%s' (%s)\n",
		map->logical_block, map->name, alternate);
	free(alternate);
    } else {
	printf("\nPartition map (with %d byte blocks) on '%s'\n",
		map->logical_block, map->name);
    }

    digits = number_of_digits(get_max_base_or_length(map));
    if (digits < 6) {
	digits = 6;
    }
    if (aflag) {
	max_type_length = 4;
    } else {
	max_type_length = get_max_type_string_length(map);
	if (max_type_length < 4) {
	    max_type_length = 4;
	}
    }
    max_name_length = get_max_name_string_length(map);
    if (max_name_length < 6) {
	max_name_length = 6;
    }
    printf(" #: %*s %-*s %*s   %-*s ( size )\n",
	    max_type_length, "type",
	    max_name_length, "name",
	    digits, "length", digits, "base");

    if (disk_order) {
	for (entry = map->disk_order; entry != NULL;
		entry = entry->next_on_disk) {

	    dump_partition_entry(entry, max_type_length, max_name_length, digits);
	}
    } else {
	for (entry = map->base_order; entry != NULL;
		entry = entry->next_by_base) {

	    dump_partition_entry(entry, max_type_length, max_name_length, digits);
	}
    }
    dump_block_zero(map);
}
예제 #3
0
int main()
{
    printf("enter positive number:\t");
    long number = get_long();

    printf("%li contains %d digits\n",
        number,
        number_of_digits(number));
    printf("%li\n", number);
}
예제 #4
0
bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
  if (!parse_options(options, errstream)) {
    return false;
  }

  if (_file_count > 0) {
    // compute digits with filecount - 1 since numbers will start from 0
    _file_count_max_digits = number_of_digits(_file_count - 1);
    _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
    _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
  }
예제 #5
0
파일: triplu.c 프로젝트: MarianPreda/Stuff
int main() {
	FILE *fp_in;
	FILE *fp_out;
	int first, last, st, dt, n_one, n_two;

	fp_in = fopen ("triplu.in", "r");
	fp_out = fopen ("triplu.out", "w");

	fscanf (fp_in, "%d", &first);
	fscanf (fp_in, "%d", &last);
	st = ((first * (int)pow (10, number_of_digits (last))) + last);
	dt = ((last * (int)pow (10, number_of_digits (first))) + first);
	if (st >= dt) {
		fprintf (fp_out, "%d", 3 * st);
	} else {
		fprintf (fp_out, "%d", 3 * dt);
	}
	fclose (fp_in);
	fclose (fp_out);

	return 0;

}
예제 #6
0
char				*ft_itoa(int n)
{
	int		n_digits;
	int		aux;
	char	*str;

	n_digits = number_of_digits(n);
	aux = 0;
	if (!(str = (char *)malloc(sizeof(char) * (n_digits + 1))))
		return (NULL);
	str[n_digits] = '\0';
	if (n < 0)
		str[0] = '-';
	while (aux < n_digits && (str[n_digits - aux - 1] != '-' || n > 0))
	{
		str[n_digits - aux - 1] = mod(n) % 10 + '0';
		n = n / 10;
		aux++;
	}
	return (str);
}
static ngx_int_t ngx_restfull_redis_handler(ngx_http_request_t *r)
{
  
  //TODO: deal with allocation problem - return 500
  ngx_chain_t   out;
  out.buf = create_response_buffer(r);;
  out.next = NULL;

  dd("command => %s", command(r));

  char* redis_command = command(r);
  define_content_type(r, "application/json; charset=utf-8");

  redisContext *c = redisConnect("127.0.0.1", 6379);

  if (c->err) {
    dd("Error: %s\n", c->errstr);
    write_to_buffer(out.buf, (u_char*) "Can't connect to redis", strlen("Can't connect to redis"));
  }

  Hash* params = get_params(r);
  
  redisReply *reply;

  if(!strcmp(redis_command, "get"))
  {
    reply = redisCommand(c,"GET %s", params->get(params, "key"));

    dd("Redis reply status: %d", reply->type);
    if(key_not_found(reply))
    {
      not_found(r, out.buf, (u_char*)"not found");
      return ngx_http_output_filter(r, &out);
    }
    dd("reply: %s", reply->str);

    u_char * result = copy(r->pool, reply->str, reply->len);
    write_to_buffer(out.buf, result, reply->len);
    write_header(r, NGX_HTTP_OK, reply->len);

  } else if (!strcmp(redis_command, "set"))
  {
    reply = redisCommand(c,"SET %s %s", params->get(params, "key"), params->get(params, "value"));

    dd("Redis reply status: %d", reply->type);
    if(key_not_found(reply))
    {
      not_found(r, out.buf, (u_char*)"not found");
      return ngx_http_output_filter(r, &out);
    }

    dd("Reply set %s -- %d", reply->str, reply->len);
    write_to_buffer(out.buf, (u_char*) reply->str, reply->len);
    write_header(r, NGX_HTTP_OK, reply->len);
  } else if(!strcmp(redis_command, "incr"))
  {
    reply = redisCommand(c,"INCR %s", params->get(params, "key"));

    dd("Redis reply status: %d", reply->type);

    int len = number_of_digits(reply->integer);

    dd("Reply INCR -- %d - len %d", (int)reply->integer, len);
    u_char* result = ngx_pcalloc(r->pool, sizeof(char)*len);
    sprintf((char*)result,"%d",(int)reply->integer);
    write_to_buffer(out.buf, result, len);
    write_header(r, NGX_HTTP_OK, len);
  }

  freeReplyObject(reply);
  redisFree(c);
  ngx_free(params);
    
  return ngx_http_output_filter(r, &out);
}