Exemplo n.º 1
0
int main() {
    std::ifstream ifs( "/home/peter/Downloads/Sheetal09Singular.pdf");
    std::string in( ( std::istreambuf_iterator<char>( ifs)),
                      std::istreambuf_iterator<char>());
    std::string text = "Some text for matching sub-strings in the text.";
    find_duplicates( in.c_str(), in.size());
}
Exemplo n.º 2
0
int app::run(int argc, const char* argv[]) {
  std::cout << "QCon Duplicatetext finder" << std::endl;
  
  try {
    std::list<std::string> paths;
    
    for (auto i = 1; i < argc; i++) {
      if (argv[i][strlen(argv[i])] != '/') {
        paths.insert(paths.end(), string(argv[i]) + "/");
      } else {
        paths.insert(paths.end(), string(argv[i]));
      }
    }
    
    find_duplicates(paths);
    
    print_platform_info();
    
  } catch(std::exception& e) {
    std::cerr << "error: " << e.what() << "\n";
    return 1;
  } catch(...) {
    std::cerr << "Exception of unknown type!\n";
    return 1;
  }
  
  return 0;
}
Exemplo n.º 3
0
int main(){
	int arr[100],*freq,n;	//freq[i] will hold the frequency of the ith element in the list
	
	printf("Number of elements: ");
	scanf("%d",&n);
	freq = (int*)malloc(sizeof(int)*100);
	printf("Fill the array with integers within 0-100:\n");
	for(int i=0;i<n;i++)
		scanf("%d",&arr[i]);
	
	freq = find_duplicates(arr,n);
	for(int i=0;i<100;i++)
		if(freq[i]>0)
			printf("Element '%d' has occured %d times.\n",arr[i],freq[i]);

	return 0;
}
Exemplo n.º 4
0
static inline size_t add_builtins(const struct ast_unit *const unit)
{
    size_t offset = 0;

    for (scope_bcon_id_t id = 0; id < SCOPE_BCON_ID_COUNT; ++id) {
        unit->scope->objs[offset++] = (struct scope_obj) {
            .name = scope_builtin_consts[id].name,
            .obj = SCOPE_OBJ_BCON,
            .bcon_id = id,
        };
    }

    for (scope_bfun_id_t id = 0; id < SCOPE_BFUN_ID_COUNT; ++id) {
        unit->scope->objs[offset++] = (struct scope_obj) {
            .name = scope_builtin_funcs[id].name,
            .obj = SCOPE_OBJ_BFUN,
            .bfun_id = id,
        };
    }

    return offset;
}

static int scope_build_inner(struct ast_node *const node,
    const struct scope *const outer)
{
    assert(node != NULL);

    static struct ast_func *outer_func;
    int error = SCOPE_OK;

    switch (node->an) {
    case AST_AN_FUNC: {
        struct ast_func *const func = ast_data(node, func);
        const size_t objcount = count_objects_block(node);

        assert(func->scope == NULL);

        if (unlikely(!(func->scope = scope_alloc(objcount)))) {
            return NOMEM;
        }

        func->scope->outer = outer;
        func->scope->objcount = objcount;
        size_t offset = 0;

        for (size_t param_idx = 0; param_idx < func->param_count; ++param_idx) {
            func->scope->objs[offset++] = (struct scope_obj) {
                .name = func->params[param_idx].name,
                .obj = SCOPE_OBJ_PARM,
                .type = func->params[param_idx].type,
            };
        }

        outer_func = func;

        for (size_t idx = 0; idx < func->stmt_count; ++idx) {
            struct ast_node *const stmt = func->stmts[idx];

            if (unlikely(!stmt)) {
                continue;
            }

            if (stmt->an == AST_AN_DECL) {
                const struct ast_decl *const decl = ast_data(stmt, decl);

                for (size_t name_idx = 0; name_idx < decl->name_count; ++name_idx) {
                    func->scope->objs[offset++] = (struct scope_obj) {
                        .name = decl->names[name_idx],
                        .obj = SCOPE_OBJ_AVAR,
                        .decl = stmt,
                    };
                }
            } else if (stmt->an == AST_AN_WLAB) {
                aggr_error(&error, add_func_wlab(func, stmt));
            } else {
                aggr_error(&error, scope_build_inner(stmt, func->scope));
            }
        }

        qsort(func->wlabs, func->wlab_count, sizeof(*func->wlabs), cmp_scope_obj);
        identify_wlabs(func);
        aggr_error(&error, find_duplicates(func->scope->objs, objcount));
        qsort(func->scope->objs, objcount, sizeof(struct scope_obj), cmp_scope_obj);
    } break;

    case AST_AN_BLOK:
    case AST_AN_NOIN:
    case AST_AN_WHIL:
    case AST_AN_DOWH: {
        struct scope **scope;
        size_t stmt_count;
        struct ast_node **stmts;
        const size_t objcount = count_objects_block(node);

        if (node->an == AST_AN_BLOK || node->an == AST_AN_NOIN) {
            struct ast_blok *const blok = ast_data(node, blok);
            scope = &blok->scope;
            stmt_count = blok->stmt_count;
            stmts = blok->stmts;
        } else {
            struct ast_whil *const whil = ast_data(node, whil);
            scope = &whil->scope;
            stmt_count = whil->stmt_count;
            stmts = whil->stmts;
        }

        assert(*scope == NULL);

        if (unlikely(!(*scope = scope_alloc(objcount)))) {
            return NOMEM;
        }

        (*scope)->outer = outer;
        (*scope)->objcount = objcount;

        for (size_t idx = 0, offset = 0; idx < stmt_count; ++idx) {
            struct ast_node *const stmt = stmts[idx];

            if (unlikely(!stmt)) {
                continue;
            }

            if (stmt->an == AST_AN_DECL) {
                const struct ast_decl *const decl = ast_data(stmt, decl);

                for (size_t name_idx = 0; name_idx < decl->name_count; ++name_idx) {
                    (*scope)->objs[offset++] = (struct scope_obj) {
                        .name = decl->names[name_idx],
                        .obj = SCOPE_OBJ_AVAR,
                        .decl = stmt,
                    };
                }
            } else if (stmt->an == AST_AN_WLAB) {
                aggr_error(&error, add_func_wlab(outer_func, stmt));
            } else {
                aggr_error(&error, scope_build_inner(stmt, *scope));
            }
        }

        aggr_error(&error, find_duplicates((*scope)->objs, objcount));
        qsort((*scope)->objs, objcount, sizeof(struct scope_obj), cmp_scope_obj);
    } break;

    case AST_AN_COND: {
        struct ast_cond *const cond = ast_data(node, cond);

        aggr_error(&error, scope_build_inner(cond->if_block, outer));

        for (size_t idx = 0; idx < cond->elif_count; ++idx) {
            aggr_error(&error, scope_build_inner(cond->elif[idx].block, outer));
        }

        if (cond->else_block) {
            aggr_error(&error, scope_build_inner(cond->else_block, outer));
        }
    } break;
    }

    return error;
}

int scope_build(struct ast_node *const root)
{
    assert(root != NULL);

    struct ast_unit *const unit = ast_data(root, unit);
    size_t objcount = count_objects_unit(unit);

    assert(unit->scope == NULL);

    if (unlikely(!(unit->scope = scope_alloc(objcount)))) {
        return NOMEM;
    }

    unit->scope->objcount = objcount;
    size_t offset = add_builtins(unit);
    int error = SCOPE_OK;

    for (size_t idx = 0; idx < unit->stmt_count; ++idx) {
        struct ast_node *const stmt = unit->stmts[idx];

        if (unlikely(!stmt)) {
            continue;
        }

        if (stmt->an == AST_AN_DECL) {
            const struct ast_decl *const decl = ast_data(stmt, decl);

            for (size_t name_idx = 0; name_idx < decl->name_count; ++name_idx) {
                unit->scope->objs[offset++] = (struct scope_obj) {
                    .name = decl->names[name_idx],
                    .obj = SCOPE_OBJ_GVAR,
                    .decl = stmt,
                };
            }
        } else if (stmt->an == AST_AN_FUNC) {
            const struct ast_func *const func = ast_data(stmt, func);

            unit->scope->objs[offset++] = (struct scope_obj) {
                .name = func->name,
                .obj = SCOPE_OBJ_FUNC,
                .func = stmt,
            };

            aggr_error(&error, scope_build_inner(stmt, unit->scope));
        }
    }

    aggr_error(&error, find_duplicates(unit->scope->objs, objcount));
    qsort(unit->scope->objs, objcount, sizeof(struct scope_obj), cmp_scope_obj);
    return error;
}

struct scope_obj *scope_find_object(const struct scope *scope,
    const struct lex_symbol *const name)
{
    const struct scope_obj needle = { .name = name };

    do {
        struct scope_obj *const found = bsearch(&needle, scope->objs,
            scope->objcount, sizeof(struct scope_obj), cmp_scope_obj);

        if (found && found->obj != SCOPE_OBJ_DUPL) {
            if (found->obj == SCOPE_OBJ_AVAR) {
                const struct ast_decl *const decl = ast_data(found->decl, decl);
                return likely(name->beg > decl->names[0]->beg) ? found : NULL;
            } else {
                return found;
            }
        }
    } while ((scope = scope->outer));

    return NULL;
}

ptrdiff_t scope_find_wlab(const struct ast_func *const func,
    const struct lex_symbol *const name)
{
    const struct scope_obj needle = { .name = name };
    const ssize_t elem_size = sizeof(*func->wlabs);

    const void *const found = bsearch(&needle, func->wlabs, func->wlab_count,
        (size_t) elem_size, cmp_scope_obj);

    return likely(found) ?
        ((char *) found - (char *) func->wlabs) / elem_size : -1;
}
Exemplo n.º 5
0
static struct imgspec* create_imgspec(int argc, char* argv[])
{
	struct imgspec* spec = malloc(sizeof(*spec));
	if (!spec)
		error("failed to allocate an image spec");
	memset(spec, 0, sizeof(*spec));
	
	spec->sp_root = malloc(sizeof(*spec->sp_root));
	if (!spec->sp_root)
		error("failed to allocate the root entry");
	memset(spec->sp_root, 0, sizeof(*spec->sp_root));
	
	spec->sp_name = MICROFS_DEFAULTNAME;
	spec->sp_shareblocks = 1;
	spec->sp_lib = hostprog_lib_find_any();
	if (!spec->sp_lib) {
		error("could not determine which hostprog lib to use");
	}
	
	spec->sp_pagesz = sysconf(_SC_PAGESIZE);
	spec->sp_blksz = MICROFS_DEFAULBLKSZ;
	spec->sp_szpad = spec->sp_pagesz;
	
	if (argc < 2)
		usage(argc > 0 ? argv[0] : "microfsmki", stderr, spec);
	
	/* Check what the user want.
	 */
	
	int option;
	char optionbuffer[3];
	while ((option = getopt(argc, argv, MKI_OPTIONS)) != EOF) {
		size_t len;
		switch (option) {
			case 'h':
				usage(argv[0], stdout, spec);
				break;
			case 'v':
				hostprog_verbosity++;
				break;
			case 'e':
				hostprog_werror = 1;
				break;
			case 'p':
				spec->sp_pad = 1;
				break;
			case 'q':
				spec->sp_squashperms = 1;
				break;
			case 's':
				spec->sp_incsocks = 1;
				break;
			case 'S':
				spec->sp_shareblocks = 0;
				break;
			case 'b':
				opt_strtolx(ul, optiontostr(option, optionbuffer),
					optarg, spec->sp_blksz);
				if (!microfs_ispow2(spec->sp_blksz))
					error("the block size must be a power of two");
				if (spec->sp_blksz < MICROFS_MINBLKSZ ||
					spec->sp_blksz > MICROFS_MAXBLKSZ) {
					error("block size out of boundaries, %llu given;"
						" min=%d, max=%d", spec->sp_blksz,
						MICROFS_MINBLKSZ, MICROFS_MAXBLKSZ);
				}
				break;
			case 'u':
				opt_strtolx(ull, optiontostr(option, optionbuffer),
					optarg, spec->sp_usrupperbound);
				break;
			case 'P':
				opt_strtolx(ull, optiontostr(option, optionbuffer),
					optarg, spec->sp_szpad);
				if (!microfs_ispow2(spec->sp_szpad))
					error("the size padding must be a power of two");
				break;
			case 'n':
				spec->sp_name = optarg;
				len = strlen(spec->sp_name);
				if (len > MICROFS_SBNAME_LENGTH) {
					warning("image name \"%s\" is too long"
						" it will be truncated from %zu to %d bytes",
						spec->sp_name, len, MICROFS_SBNAME_LENGTH);
				}
				break;
			case 'c':
				spec->sp_lib = hostprog_lib_find_byname(optarg);
				if (!spec->sp_lib)
					error("could not find a compression library named %s", optarg);
				break;
			case 'D':
				spec->sp_devtable = optarg;
				break;
			case 'l':
				spec->sp_lib_options = optarg;
				break;
			default:
				/* Ignore it.
				 */
				warning("unrecognized option -%c", option);
				break;
		}
	}
	
	/* The block size should now be correctly set, which means
	 * that the block left shift can be calculated.
	 */
	__u64 blksz = spec->sp_blksz;
	while ((blksz >>= 1) > 0)
		spec->sp_blkshift++;
	
	if (spec->sp_usrupperbound % spec->sp_blksz != 0)
		error("upper bound must be a multiple of the block size");
	
	if (hostprog_stack_create(&spec->sp_regstack, 64, 64) < 0)
		error("failed to create the regular file stack");
	
	if (spec->sp_squashperms && spec->sp_devtable) {
		warning("both -q and -d are set, this might not be a good idea"
			" - the device table could override some or all permissions");
	}
	
	spec->sp_upperbound = sizeof(struct microfs_sb);
	if (spec->sp_pad)
		spec->sp_upperbound += MICROFS_PADDING;
	
	if ((argc - optind) != 2)
		usage(argv[0], stderr, spec);
	spec->sp_rootdir = argv[optind + 0];
	spec->sp_outfile = argv[optind + 1];
	
	if (!spec->sp_lib->hl_compiled)
		error("%s support has not been compiled", spec->sp_lib->hl_info->li_name);
	if (spec->sp_lib->hl_init(&spec->sp_lib_data, spec->sp_blksz) < 0)
		error("failed to init %s", spec->sp_lib->hl_info->li_name);
	
	lib_options(spec);
	
	spec->sp_upperbound += spec->sp_lib->hl_info->li_dd_sz;
	
	if (spec->sp_lib->hl_info->li_min_blksz == 0 && spec->sp_blksz < spec->sp_pagesz) {
		warning("block size smaller than page size of host"
			" - the resulting image can not be used on this host");
	}
	
	struct stat st;
	if (stat(spec->sp_rootdir, &st) == 0) {
		if (!S_ISDIR(st.st_mode))
			error("\"%s\" is not a directory", spec->sp_rootdir);
	} else
		error("can not stat \"%s\": %s", spec->sp_rootdir, strerror(errno));
	
	struct hostprog_path* path = NULL;
	if (hostprog_path_create(&path, spec->sp_rootdir,
			MICROFS_MAXNAMELEN,	MICROFS_MAXNAMELEN) != 0) {
		error("failed to create the path for the rootdir: %s", strerror(errno));
	}
	
	spec->sp_root->e_mode = st.st_mode;
	spec->sp_root->e_uid = st.st_uid;
	spec->sp_root->e_gid = st.st_gid;
	spec->sp_root->e_size = walk_directory(spec, path,
		&spec->sp_root->e_firstchild);
	
	hostprog_path_destroy(path);
	
	if (spec->sp_shareblocks)
		find_duplicates(spec);
	
	if (spec->sp_devtable)
		devtable_parse(devtable_process_dentry, spec,
			spec->sp_devtable, MICROFS_ISIZE_WIDTH);
	
	if (spec->sp_usrupperbound && spec->sp_upperbound > spec->sp_usrupperbound) {
		warning("the estimated upper bound %llu is larger than the"
			" user requested upper bound %llu", spec->sp_upperbound,
			spec->sp_usrupperbound);
	}
	
	/* Allocate a max block size sized multiple of bytes.
	 */
	spec->sp_upperbound = sz_blkceil(spec->sp_upperbound,
		MICROFS_MAXBLKSZ);
	
	if (spec->sp_upperbound > MICROFS_MAXIMGSIZE) {
		warning("upper bound image size (absolute worst-case scenario)"
			" of %llu bytes is larger than the max image size of %llu bytes,"
			" there might not be room for everything", spec->sp_upperbound,
			MICROFS_MAXIMGSIZE);
		spec->sp_upperbound = MICROFS_MAXIMGSIZE;
	}
	
	int flags = O_RDWR | O_CREAT | O_TRUNC;
	spec->sp_fd = open(spec->sp_outfile, flags, 0666);
	if (spec->sp_fd < 0)
		error("failed to open \"%s\": %s", spec->sp_rootdir, strerror(errno));
	
	/* The worst case compression scenario will always fit in the
	 * buffer since the upper bound for a data size smaller than
	 * a block is always smaller than the upper bound for an entire
	 * block.
	 */
	spec->sp_compressionbufsz = spec->sp_lib->hl_upperbound(
		spec->sp_lib_data, spec->sp_blksz);
	spec->sp_compressionbuf = malloc(spec->sp_compressionbufsz);
	if (!spec->sp_compressionbuf)
		error("failed to allocate the compression buffer");
	
	message(VERBOSITY_1, "Block size: %llu", spec->sp_blksz);
	message(VERBOSITY_1, "Block shift: %llu", spec->sp_blkshift);
	
	message(VERBOSITY_0, "Compression library: %s", spec->sp_lib->hl_info->li_name);
	message(VERBOSITY_0, "Upper bound image size: %llu bytes", spec->sp_upperbound);
	message(VERBOSITY_0, "Number of files: %llu", spec->sp_files);
	message(VERBOSITY_0, "Directories: %llu", spec->sp_dirnodes);
	message(VERBOSITY_0, "Regular files: %llu", spec->sp_regnodes);
	message(VERBOSITY_0, "Duplicate files: %llu", spec->sp_duplicatenodes);
	message(VERBOSITY_0, "Special files: %llu", spec->sp_specnodes);
	message(VERBOSITY_0, "Skipped files: %llu", spec->sp_skipnodes);
	message(VERBOSITY_1, "Data size: %llu", spec->sp_datasz);
	message(VERBOSITY_1, "Real data size: %llu", spec->sp_realdatasz);
	message(VERBOSITY_1, "Block pointers required: %llu", spec->sp_blkptrs);
	
	if (spec->sp_skipnodes) {
		warning("not all files will be included in the image");
		if (hostprog_verbosity < VERBOSITY_1)
			message(VERBOSITY_0, ">>> use -v to get more information");
	}
	
	return spec;
}
Exemplo n.º 6
0
/*
 * NOTE: We should consider to install an exit handler which does the
 * unlink() of the output file. In case of error we just do exit() and
 * forget about all the clumsy error handling free/close code, which
 * blows up the code significantly and makes it hard to read.
 */
int
main(int argc, char *argv[])
{
	int conf_file, rc;
	struct ffs_chain_t ffs_chain;
	int c;
	int smart_pad = 0;	/* default */
	int notime = 0;
	const char *config_file = "boot_rom.ffs";
	const char *output_file = "boot_rom.bin";

	memset((void *) &ffs_chain, 0, sizeof(struct ffs_chain_t));

	while (1) {
		int option_index = 0;
		static struct option long_options[] = {
			{"romfs-size", 1, 0, 's'},
			{"smart-pad", 0, 0, 'p'},
			{"notime", 0, 0, 'n'},
			{"verbose", 0, 0, 'v'},
			{"help", 1, 0, 'h'},
			{0, 0, 0, 0}
		};
		c = getopt_long(argc, argv, "s:ph?nv", long_options,
				&option_index);
		if (c == -1)
			break;

		switch (c) {
		case 's':
			ffs_chain.romfs_size = str_to_num(optarg);
			break;
		case 'p':
			smart_pad = 1;
			break;
		case 'n':
			notime = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case '?':
		case 'h':
			print_usage();
			return EXIT_SUCCESS;
		default:
			printf("?? getopt returned character code 0%o ??\n", c);
		}
	}

	/* two files must always be specified: config-file and output-file */
	if (optind + 2 != argc) {
		print_usage();
		return EXIT_FAILURE;
	}

	config_file = argv[optind++];
	output_file = argv[optind++];

	dprintf("ROMFS FILESYSTEM CREATION V0.3 (bad parser)\n"
		"Build directory structure...\n"
		"  smart padding %s, maximum romfs size %d bytes\n",
		smart_pad ? "enabled" : "disabled", ffs_chain.romfs_size);

	conf_file = open(config_file, O_RDONLY);
	if (0 >= conf_file) {
		perror("load config file:");
		return EXIT_FAILURE;
	}

	rc = read_config(conf_file, &ffs_chain);
	close(conf_file);
	if (rc < 1) {
		fprintf(stderr, "flash cannot be built due to config errors\n");
		return EXIT_FAILURE;
	}

	rc = EXIT_SUCCESS;

	if (verbose)
		dump_fs_contents(&ffs_chain);
	if (smart_pad)
		/* FIXME: size is only verified during reorder */
		rc = reorder_ffs_chain(&ffs_chain);

	if (rc == EXIT_FAILURE)
		goto out;

	dprintf("Build ffs and write to image file...\n");
	if (build_ffs(&ffs_chain, output_file, notime) != 0) {
		fprintf(stderr, "build ffs failed\n");
		rc = EXIT_FAILURE;
	} else {
		rc = EXIT_SUCCESS;
	}

	/* Check if there are any duplicate entries in the image,
	   print warning if this is the case. */
	find_duplicates(&ffs_chain);
	free_chain_memory(&ffs_chain);
	dprintf("\n");

      out:
	/* If the build failed, remove the target image file */
	if (rc == EXIT_FAILURE)
		unlink(output_file);

	return rc;
}