Ejemplo n.º 1
0
int simple_stable::remove(const dtype & key, const istr & column)
{
	int r;
	dtype::ctype type;
	const column_info * c = get_column(column);
	/* does it even exist to begin with? */
	if(!c || !ct_data->find(key, column).exists())
		return 0;
	type = c->type;
	r = adjust_column(column, -1, type);
	if(r < 0)
		return r;
	r = ct_data->remove(key, column);
	if(r < 0)
		adjust_column(column, 1, type);
	return r;
}
Ejemplo n.º 2
0
int simple_stable::insert(const dtype & key, const istr & column, const dtype & value, bool append)
{
	int r;
	bool increment = !ct_data->find(key, column).exists();
	if(increment)
	{
		/* this will check that the type matches */
		r = adjust_column(column, 1, value.type);
		if(r < 0)
			return r;
	}
	else if(column_type(column) != value.type)
		return -EINVAL;
	r = ct_data->insert(key, column, value.flatten(), append);
	if(r < 0 && increment)
		adjust_column(column, -1, value.type);
	return r;
}
Ejemplo n.º 3
0
int simple_stable::remove(const dtype & key)
{
	int r;
	ctable::iter * columns = ct_data->iterator(key);
	if(!columns)
		return 0;
	while(columns->valid())
	{
		const column_info * c = get_column(columns->name());
		r = adjust_column(columns->name(), -1, c->type);
		/* XXX: improve this */
		assert(r >= 0);
		columns->next();
	}
	delete columns;
	r = ct_data->remove(key);
	/* XXX: improve this */
	assert(r >= 0);
	return r;
}
Ejemplo n.º 4
0
extern int fold_main(int argc, char **argv)
{
	/* If nonzero, try to break on whitespace. */
	int break_spaces;

	/* If nonzero, at least one of the files we read was standard input. */
	int have_read_stdin;

	int width = 80;
	int i;
	int optc;
	int errs = 0;

	break_spaces = count_bytes = have_read_stdin = 0;

	/* Turn any numeric options into -w options.  */
	for (i = 1; i < argc; i++) {
		char const *a = argv[i];

		if (a[0] == '-') {
			if (a[1] == '-' && !a[2])
				break;
			if (isdigit(a[1])) {
				char *s = xmalloc(strlen(a) + 2);

				s[0] = '-';
				s[1] = 'w';
				strcpy(s + 2, a + 1);
				argv[i] = s;
			}
		}
	}

	while ((optc = getopt(argc, argv, "bsw:")) > 0) {
		switch (optc) {
			case 'b':		/* Count bytes rather than columns. */
				count_bytes = 1;
				break;
			case 's':		/* Break at word boundaries. */
				break_spaces = 1;
				break;
			case 'w': {	/* Line width. */
				width = bb_xgetlarg(optarg, 10, 1, 10000);
				break;
			}
			default:
				bb_show_usage();
		}
	}

	argv += optind;
	if (!*argv) {
		*--argv = "-";
	}

	do {
		FILE *istream = bb_wfopen_input(*argv);
		if (istream != NULL) {
			int c;
			int column = 0;		/* Screen column where next char will go. */
			int offset_out = 0;	/* Index in `line_out' for next char. */
			static char *line_out = NULL;
			static int allocated_out = 0;

			while ((c = getc(istream)) != EOF) {
				if (offset_out + 1 >= allocated_out) {
					allocated_out += 1024;
					line_out = xrealloc(line_out, allocated_out);
				}

				if (c == '\n') {
					line_out[offset_out++] = c;
					fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
					column = offset_out = 0;
					continue;
				}

rescan:
				column = adjust_column(column, c);

				if (column > width) {
					/* This character would make the line too long.
			 		  Print the line plus a newline, and make this character
					   start the next line. */
					if (break_spaces) {
						/* Look for the last blank. */
						int logical_end;

						for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
							if (isblank(line_out[logical_end])) {
								break;
							}
						}
						if (logical_end >= 0) {
							/* Found a blank.  Don't output the part after it. */
							logical_end++;
							fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
							putchar('\n');
							/* Move the remainder to the beginning of the next line.
							   The areas being copied here might overlap. */
							memmove(line_out, line_out + logical_end, offset_out - logical_end);
							offset_out -= logical_end;
							for (column = i = 0; i < offset_out; i++) {
								column = adjust_column(column, line_out[i]);
							}
							goto rescan;
						}
					} else {
						if (offset_out == 0) {
							line_out[offset_out++] = c;
							continue;
						}
					}
					line_out[offset_out++] = '\n';
					fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
					column = offset_out = 0;
					goto rescan;
				}

				line_out[offset_out++] = c;
			}

			if (offset_out) {
				fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
			}

			if (ferror(istream) || bb_fclose_nonstdin(istream)) {
				bb_perror_msg("%s", *argv);	/* Avoid multibyte problems. */
				errs |= EXIT_FAILURE;
			}
		} else {
			errs |= EXIT_FAILURE;
		}
	} while (*++argv);

	bb_fflush_stdout_and_exit(errs);
}
Ejemplo n.º 5
0
int fold_main(int argc UNUSED_PARAM, char **argv)
{
	char *line_out = NULL;
	const char *w_opt = "80";
	unsigned width;
	smallint exitcode = EXIT_SUCCESS;

	init_unicode();

	if (ENABLE_INCLUDE_SUSv2) {
		/* Turn any numeric options into -w options.  */
		int i;
		for (i = 1; argv[i]; i++) {
			const char *a = argv[i];
			if (*a == '-') {
				a++;
				if (*a == '-' && !a[1]) /* "--" */
					break;
				if (isdigit(*a))
					argv[i] = xasprintf("-w%s", a);
			}
		}
	}

	getopt32(argv, "bsw:", &w_opt);
	width = xatou_range(w_opt, 1, 10000);

	argv += optind;
	if (!*argv)
		*--argv = (char*)"-";

	do {
		FILE *istream = fopen_or_warn_stdin(*argv);
		int c;
		unsigned column = 0;     /* Screen column where next char will go */
		unsigned offset_out = 0; /* Index in 'line_out' for next char */

		if (istream == NULL) {
			exitcode = EXIT_FAILURE;
			continue;
		}

		while ((c = getc(istream)) != EOF) {
			/* We grow line_out in chunks of 0x1000 bytes */
			if ((offset_out & 0xfff) == 0) {
				line_out = xrealloc(line_out, offset_out + 0x1000);
			}
 rescan:
			line_out[offset_out] = c;
			if (c == '\n') {
				write2stdout(line_out, offset_out + 1);
				column = offset_out = 0;
				continue;
			}
			column = adjust_column(column, c);
			if (column <= width || offset_out == 0) {
				/* offset_out == 0 case happens
				 * with small width (say, 1) and tabs.
				 * The very first tab already goes to column 8,
				 * but we must not wrap it */
				offset_out++;
				continue;
			}

			/* This character would make the line too long.
			 * Print the line plus a newline, and make this character
			 * start the next line */
			if (option_mask32 & FLAG_BREAK_SPACES) {
				unsigned i;
				unsigned logical_end;

				/* Look for the last blank. */
				for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
					if (!isblank(line_out[logical_end]))
						continue;

					/* Found a space or tab.
					 * Output up to and including it, and start a new line */
					logical_end++;
					/*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
					write2stdout(line_out, logical_end);
					putchar('\n');
					/* Move the remainder to the beginning of the next line.
					 * The areas being copied here might overlap. */
					memmove(line_out, line_out + logical_end, offset_out - logical_end);
					offset_out -= logical_end;
					for (column = i = 0; i < offset_out; i++) {
						column = adjust_column(column, line_out[i]);
					}
					goto rescan;
				}
				/* No blank found, wrap will split the overlong word */
			}
			/* Output what we accumulated up to now, and start a new line */
			line_out[offset_out] = '\n';
			write2stdout(line_out, offset_out + 1);
			column = offset_out = 0;
			goto rescan;
		} /* while (not EOF) */

		if (offset_out) {
			write2stdout(line_out, offset_out);
		}

		if (fclose_if_not_stdin(istream)) {
			bb_simple_perror_msg(*argv);
			exitcode = EXIT_FAILURE;
		}
	} while (*++argv);

	fflush_stdout_and_exit(exitcode);
}
Ejemplo n.º 6
0
extern int fold_main(int argc, char **argv)
{
	char *w_opt;
	int width = 80;
	int i;
	int errs = 0;


#ifdef CONFIG_FEATURE_SUSv2_OBSOLETE
	/* Turn any numeric options into -w options.  */
	for (i = 1; i < argc; i++) {
		char const *a = argv[i];

		if (a[0] == '-') {
			if (a[1] == '-' && !a[2])
				break;
			if (isdigit(a[1])) {
				char *s = xmalloc(strlen(a) + 2);

				s[0] = '-';
				s[1] = 'w';
				strcpy(s + 2, a + 1);
				argv[i] = s;
			}
		}
	}
#endif

	flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
	if (flags & FLAG_WIDTH)
		width = bb_xgetlarg(w_opt, 10, 1, 10000);

	argv += optind;
	if (!*argv) {
		*--argv = "-";
	}

	do {
		FILE *istream = bb_wfopen_input(*argv);
		if (istream != NULL) {
			int c;
			int column = 0;		/* Screen column where next char will go. */
			int offset_out = 0;	/* Index in `line_out' for next char. */
			static char *line_out = NULL;
			static int allocated_out = 0;

			while ((c = getc(istream)) != EOF) {
				if (offset_out + 1 >= allocated_out) {
					allocated_out += 1024;
					line_out = xrealloc(line_out, allocated_out);
				}

				if (c == '\n') {
					line_out[offset_out++] = c;
					fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
					column = offset_out = 0;
					continue;
				}

rescan:
				column = adjust_column(column, c);

				if (column > width) {
					/* This character would make the line too long.
					   Print the line plus a newline, and make this character
					   start the next line. */
					if (flags & FLAG_BREAK_SPACES) {
						/* Look for the last blank. */
						int logical_end;

						for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
							if (isblank(line_out[logical_end])) {
								break;
							}
						}
						if (logical_end >= 0) {
							/* Found a blank.  Don't output the part after it. */
							logical_end++;
							fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
							putchar('\n');
							/* Move the remainder to the beginning of the next line.
							   The areas being copied here might overlap. */
							memmove(line_out, line_out + logical_end, offset_out - logical_end);
							offset_out -= logical_end;
							for (column = i = 0; i < offset_out; i++) {
								column = adjust_column(column, line_out[i]);
							}
							goto rescan;
						}
					} else {
						if (offset_out == 0) {
							line_out[offset_out++] = c;
							continue;
						}
					}
					line_out[offset_out++] = '\n';
					fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
					column = offset_out = 0;
					goto rescan;
				}

				line_out[offset_out++] = c;
			}

			if (offset_out) {
				fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
			}

			if (ferror(istream) || bb_fclose_nonstdin(istream)) {
				bb_perror_msg("%s", *argv);	/* Avoid multibyte problems. */
				errs |= EXIT_FAILURE;
			}
		} else {
			errs |= EXIT_FAILURE;
		}
	} while (*++argv);

	bb_fflush_stdout_and_exit(errs);
}