Example #1
0
/*
 * Ver documentacion en header
 */
int data_read(struct data_t* data, struct cl_args_t* args) {
  int result;
  struct buffer_t buffer;
  LOG_DATA_DEBUG("Reading data from input");

  /* Se inicializa el buffer de acumulacion de linea */
  result = buffer_init(&buffer);
  if (!result) {
    LOG_DATA_DEBUG("Unable to initialize buffer on data_read");
    LOG_ERROR("Unable to initialize line buffer");
    return 0;
  }

  /* Se acumulan las lineas de todos los archivos */
  if (args->file_count) {
    int i;
    FILE *input;
    for (i = 0; i < args->file_count; i++) {
      LOG_DATA_DEBUG("Processing new file from command line args");
      LOG_DATA_DEBUG_SVAR(args->files[i]);

      input = fopen(args->files[i], "rb");
      if (!input) {
        printf("Unable to open file %s\n", args->files[i]);
        buffer_cleanup(&buffer);
        return 0;
      }

      result = accumulate_lines(data, input, &buffer);
      if(!result) {
        LOG_DATA_DEBUG("Unable to accumulate lines from multiple files on data_read");
        LOG_ERROR("Unable to accumulate lines from multiple files");
        fclose(input);
        buffer_cleanup(&buffer);
        return 0;
      }

      fclose(input);
    }
  } else {
    LOG_DATA_DEBUG("Processing input from standard input");
    result = accumulate_lines(data, stdin, &buffer);
    if (!result) {
      LOG_DATA_DEBUG("Unable to accumulate lines from file on data_read");
      LOG_ERROR("Unable to accumulate lines from file");
      buffer_cleanup(&buffer);
      return 0;
    }
  }

  /* Se limpian los recursos que no son mas necesarios */
  buffer_cleanup(&buffer);
  data_shrink(data);
  return 1;
}
void buffer_cleanup(int * buffer, int buf_len, int start, int end) {
    //fills parts of buffer with minvalue so we can put gaps in graph.
    
    //int buf_len = sizeof(&buffer)/sizeof(int);

    if (start !=0 || end != -1) {  //Do not write to Serial on initial cleanup, as its not initialised yet
#ifdef DEBUG_RB_DATA
        Serial.print(F("Cleanup:"));
        Serial.print(start);
        Serial.print(F(" - "));
        Serial.print(end);
        Serial.print(F(", size:"));
        Serial.println(buf_len);
#endif
    }

    if (end > buf_len || start == end) {
        return;
    }

    if (end == -1) {
        end = buf_len;
    }
    if(start > end) {  // when cleanup is over the end
        buffer_cleanup(buffer, buf_len, start, buf_len);
        start = 0;
    }
    for (int buf_idx = start; buf_idx < end; buf_idx ++) {
        buffer[buf_idx] = MINVALUE;
    }
}
bool buffer_store(int * buffer, int buf_len, int value, int new_position, int old_position){
    // store 'value' into 'buffer' at 'old_position' position. Clean values between 'old_position' and last stored postition ('new_position')
#ifdef DEBUG_RB_DATA
    Serial.print(F("Storing "));
    Serial.print(value);
    Serial.print(F(", new_position "));
    Serial.print(new_position);
    Serial.print(F(" with old_position "));
    Serial.println(old_position);
#endif
    //int buf_len = sizeof(&buffer)/sizeof(int);
    //stay inside buffer
    //this allows us to log sequence longer than buffer, for example using
    //ordinary timestamp or day-seconds
    while (new_position >= buf_len){
        new_position = new_position % buf_len;
    }

    //purge skipped values. Do not purge if we advanced by one or if we write
    //to the same place again
    //(needed for recalculating average for last day and month value)
    if (old_position != new_position - 1 && old_position != new_position){
        buffer_cleanup(buffer, buf_len, old_position + 1, new_position);
    }

    buffer[new_position] = value;
    old_position = new_position;
    return true;
}
Example #4
0
void pocket_get(const char *key, const char *token) 
{
    struct buffer s;

    buffer_init(&s);
    request(URL_GET, get_auth(key, token), &s, 0);
    buffer_cleanup(&s);
}
Example #5
0
void pocket_add(const char *key, const char *token, const char* url) 
{
    struct buffer s;

    buffer_init(&s);
    request(URL_ADD, add_url(key, token, url), &s, 0);
    buffer_cleanup(&s);
}
Example #6
0
/**
 * Cleanup zone transfer handler.
 *
 */
void
xfrhandler_cleanup(xfrhandler_type* xfrhandler)
{
    if (!xfrhandler) {
        return;
    }
    netio_cleanup_shallow(xfrhandler->netio);
    buffer_cleanup(xfrhandler->packet);
    tcp_set_cleanup(xfrhandler->tcp_set);
    free(xfrhandler);
}
Example #7
0
int
DSound_Cleanup(void)
{

	playing = FALSE;
	if (audio_fd >= 0) {
		SDL_CloseAudio();
		SDL_Quit();
		buffer_cleanup();
		audio_fd = -1;
	}
	return TRUE;
}
/**
 * Cleanup query.
 *
 */
void
query_cleanup(query_type* q)
{
    allocator_type* allocator = NULL;
    if (!q) {
        return;
    }
    allocator = q->allocator;
    buffer_cleanup(q->buffer, allocator);
    tsig_rr_cleanup(q->tsig_rr);
    allocator_deallocate(allocator, (void*)q);
    allocator_cleanup(allocator);
    return;
}
Example #9
0
char *pocket_auth(const char *key, const char *token)
{ 
    struct buffer s;
    json_object *response;
    char* access_token;

    buffer_init(&s);

    request(URL_AUTHORIZE, create_auth_petition(key, token), &s, 0);

    response = json_tokener_parse(s.ptr);
    json_object_object_get_ex(response, "access_token", &response);
    access_token = (char *)json_object_get_string(response);

    free(response);
    buffer_cleanup(&s);

    return access_token;
}
Example #10
0
char *pocket_get_token(const char *key) 
{
    struct buffer s;
    json_object *response;
    char *token;

    buffer_init(&s);

    request(URL_REQUEST, create_request_petition(key), &s, 0);

    response = json_tokener_parse(s.ptr);
    json_object_object_get_ex(response, "code", &response);
    token = (char *)json_object_get_string(response);

    free(response);
    buffer_cleanup(&s);

    return token;
}
Example #11
0
Buffer* buffer_copy (Buffer* buffer, int32_t start, int32_t length) {
    assert(NULL != buffer);
    assert(NULL != buffer->data);
    assert(0 <= start);
    assert(0 < length);
    assert(start + length <= buffer->length);

    Buffer* copy = buffer_new();

    if (NULL == copy) {
        return NULL;
    } else if (!buffer_init(copy, length, NULL != buffer->mutex)) {
        free(copy);

        return NULL;
    } else if (!buffer_append(copy, buffer->data + start, length)) {
        buffer_cleanup(copy);
        free(copy);

        return NULL;
    }

    return copy;
}
Example #12
0
File: view.c Project: bytbox/iv
void view_close() {
    endwin();
    buffer_cleanup(); /* cleanup buffers */
}
Example #13
0
int main(int argc, char** argv) {
	// read all used flags
	while (1) {
		static struct option long_options[] = {
			{"invert-match", no_argument, 0, 'v'},
			{"line-regexp",  no_argument, 0, 'x'},
			{"count",        no_argument, 0, 'c'},
			{"help",         no_argument, &display_help, 1},
			{"version",      no_argument, 0, 'V'},
			{"cache-limit",  required_argument, 0, OPTION_CACHE_LIMIT},
			{0, 0, 0, 0}
		};
		int option_index = 0;
		int c = getopt_long(argc, argv, "vxcV", long_options, &option_index);
		if (c == -1) break;
		switch (c) {
			case 'v': invert_match = true; break;
			case 'x': whole_lines = true; break;
			case 'c': count_matches = true; break;
			case OPTION_CACHE_LIMIT:
				cache_mem_limit = parse_size_in_kb(optarg);
				if (cache_mem_limit == -1U) {
					print_usage(argv[0]);
					return 2;
				}
				break;
			case 'V':
				display_version = true;
				break;
			case 0: break;
			default:
				print_usage(argv[0]);
				return 2;
		}
	}

	if (display_help) {
		printf(help_message, argv[0]);
		return 0;
	}

	if (display_version) {
		printf(version_message);
		return 0;
	}

	// check if there is exactly one regular expression
	if (optind != argc-1) {
		print_usage(argv[0]);
		return 2;
	}

	char* regex = argv[optind];

	// parse the regular expression to abstract syntax tree
	struct syntree* tree;
	tree = parse(regex, strlen(regex));

	// build nfa for the regular expression
	struct nfa* nfa;
	nfa = build_nfa(tree, whole_lines);
	free_tree(tree);

	// initialize simulation state
	sim_init(&state, nfa, invert_match, cache_mem_limit);

	// initialize buffer
	uintptr_t buffer_size = 65536;
	buffer_init(&buffer, STDIN_FILENO, STDOUT_FILENO, buffer_size);

	// main matching loop
	uintmax_t match_count = 0;
	bool new_line = true;
	bool some_match = false;
	intptr_t res = buffer_next(&buffer);
	if (!count_matches) buffer_mark(&buffer);
	while (res == 1) {
		// save next input byte to ch
		uint_fast8_t ch = buffer_get(&buffer);

		if (ch == '\n') {
			// handle the end of line here
			new_line = true;
			// simulate the special line end character
			if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END);
			if (sim_is_match(&state)) {
				// the last line matched, either print it or count it
				some_match = true;
				if (count_matches) {
					++match_count;
				} else {
					int_fast8_t res = buffer_print(&buffer, true);
					if (res != 1) die(2, (res == -1) ? errno : 0,
							"Error writing to stdout");
				}
			}
			// reset simulation state
			state.dfa_state = state.after_begin;
			// read next character from input
			res = buffer_next(&buffer);
			if (!count_matches) buffer_mark(&buffer);
		} else {
			new_line = false;
			// simulate only if there was no match on the current line
			if (!state.dfa_state->accept) {
				// if the character is not valid ASCII, no transitions will take place
				if (ch > MAX_CHAR) state.dfa_state = state.before_begin;
				// otherwise do the simulation
				else sim_step(&state, ch);
			}
			// read next character from input
			res = buffer_next(&buffer);
		}
	}
	if (res == -1) die(2, errno, "Error reading from stdin");

	// check if there is no \n at the end of the input
	// if it is the case, behave as if it was there
	if (!new_line) {
		if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END);
		if (sim_is_match(&state)) {
			some_match = true;
			if (count_matches) {
				++match_count;
			} else {
				int_fast8_t res = buffer_print(&buffer, false);
				puts("");
				if (res != 1) die(2, (res == -1) ? errno : 0,
						"Error writing to stdout");
			}
		}
	}

	// if -c is used, print the count of matches
	if (count_matches) {
		printf("%" PRIuMAX "\n", match_count);
	}

	// clean up
	buffer_cleanup(&buffer);
	sim_cleanup(&state);
	free_nfa(nfa);

	// return status is based on whether there was a match or not
	return some_match ? 0 : 1;
}