Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
adds pattern matching to file extraction
Browse files Browse the repository at this point in the history
done with #27
and info during file extraction for #25
  • Loading branch information
LiquidFenrir committed Jun 25, 2017
1 parent b5517c5 commit 279dba4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 53 deletions.
13 changes: 11 additions & 2 deletions source/file.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "file.h"
#include "stringutils.h"

#include "minizip/unzip.h"

Expand Down Expand Up @@ -125,7 +126,7 @@ Result extractFileFrom7z(const char * archive_file, const char * filename, const
size_t offset = 0;
size_t fileSize = 0;

if (!strcmp(name8, filename)) {
if (!matchPattern(filename, name8)) {
res = SzArEx_Extract(
&db,
&memStream.s,
Expand Down Expand Up @@ -159,6 +160,7 @@ Result extractFileFrom7z(const char * archive_file, const char * filename, const
}
}

printf("Couldn't find a file with a name matching %s in the archive.\n", filename);
ret = EXTRACTION_ERROR_FIND;

finish:
Expand All @@ -168,6 +170,11 @@ Result extractFileFrom7z(const char * archive_file, const char * filename, const
return ret;
}

static int unzipMatchPattern(__attribute__ ((unused)) unzFile file, const char *filename1, const char *filename2)
{
return matchPattern(filename2, filename1);
}

Result extractFileFromZip(const char * archive_file, const char * filename, const char * filepath)
{
if (filename == NULL) {
Expand Down Expand Up @@ -199,7 +206,7 @@ Result extractFileFromZip(const char * archive_file, const char * filename, cons

unz_file_info payloadInfo = {};

ret = unzLocateFile(uf, filename, NULL);
ret = unzLocateFile(uf, filename, &unzipMatchPattern);

if (ret == UNZ_END_OF_LIST_OF_FILE) {
printf("Couldn't find the wanted file in the zip.\n");
Expand All @@ -222,6 +229,7 @@ Result extractFileFromZip(const char * archive_file, const char * filename, cons

ret = unzOpenCurrentFile(uf);
if (ret != UNZ_OK) {
printf("Couldn't open file in the archive to read\n");
ret = EXTRACTION_ERROR_OPEN_IN_ARCHIVE;
goto finish;
}
Expand All @@ -236,6 +244,7 @@ Result extractFileFromZip(const char * archive_file, const char * filename, cons
ret = 0;
}
else {
printf("Couldn't read data from the file in the archive\n");
ret = EXTRACTION_ERROR_READ_IN_ARCHIVE;
goto finish;
}
Expand Down
33 changes: 3 additions & 30 deletions source/gitapi.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
#include "gitapi.h"
#include "stringutils.h"

int matchPattern(const char * pattern, const char * str) {
int p_offset = 0, s_offset = 0;
char current_p_char = '\0', current_s_char = '\0', next_p_char = '\0';

while (str[s_offset] != '\0') {
current_p_char = pattern[p_offset];
current_s_char = str[s_offset];
if (current_p_char == '*' && next_p_char == '\0') {
next_p_char = pattern[p_offset+1];
}
if (next_p_char != '\0') {
if (current_s_char != next_p_char) {
s_offset++;
continue;
}
else {
current_p_char = next_p_char;
p_offset++;
next_p_char = '\0';
}
}
if (current_p_char != current_s_char) return 1;

s_offset++;
p_offset++;
}
return (next_p_char != '\0');
}

char * findTagValue(const char * apiresponse, const char * tagname) {
char * findTagValue(const char * apiresponse, const char * tagname)
{
char * endstring = "\"";
char *tagstart, *tagend, *retstr = NULL;

Expand Down
22 changes: 1 addition & 21 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,7 @@
#include "draw.h"
#include "file.h"
#include "cia.h"

void cleanPath(char * path)
{
for (int i = 0; path[i]; i++) { //replace all spaces and fat32 reserved characters in the path with underscores
switch (path[i]) {
case ' ':
case '"':
case '/':
case '*':
case ':':
case '<':
case '>':
case '?':
case '\\':
case '|':
path[i] = '_';
default:
break;
}
}
}
#include "stringutils.h"

u8 update(entry_t entry)
{
Expand Down
51 changes: 51 additions & 0 deletions source/stringutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "stringutils.h"

int matchPattern(const char * pattern, const char * str)
{
int p_offset = 0, s_offset = 0;
char current_p_char = '\0', current_s_char = '\0', next_p_char = '\0';

while (str[s_offset] != '\0') {
current_p_char = pattern[p_offset];
current_s_char = str[s_offset];
if (current_p_char == '*' && next_p_char == '\0') {
next_p_char = pattern[p_offset+1];
}
if (next_p_char != '\0') {
if (current_s_char != next_p_char) {
s_offset++;
continue;
}
else {
current_p_char = next_p_char;
p_offset++;
next_p_char = '\0';
}
}
if (current_p_char != current_s_char) return 1;

s_offset++;
p_offset++;
}
return (next_p_char != '\0');
}

void cleanPath(char * path)
{
for (int i = 0; path[i]; i++) { //replace all spaces and fat32 reserved characters in the path with underscores
switch (path[i]) {
case ' ':
case '"':
case '*':
case ':':
case '<':
case '>':
case '?':
case '\\':
case '|':
path[i] = '_';
default:
break;
}
}
}
6 changes: 6 additions & 0 deletions source/stringutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "basic.h"

int matchPattern(const char * pattern, const char * str);
void cleanPath(char * path);

0 comments on commit 279dba4

Please sign in to comment.