Skip to content

Commit

Permalink
Added GetFilePath function
Browse files Browse the repository at this point in the history
  • Loading branch information
towa0131 committed Oct 19, 2018
1 parent 7db5198 commit 413c21d
Showing 1 changed file with 54 additions and 30 deletions.
84 changes: 54 additions & 30 deletions cgss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C"{
}

#include <iostream>
#include <stdlib.h>

#include "php_cgss.hpp"
#include "acbunpack.hpp"
Expand All @@ -23,9 +24,27 @@ using namespace std;

zend_class_entry *cgss_exception;

string GetFilePath(const string &s) {
const char *filePath = s.c_str();
char *realPath;

realPath = realpath(filePath, NULL);
const auto pos = string(realPath).rfind("/");
if(pos == string::npos){
return realPath;
}

string path = string(realPath).substr(0, pos + 1);

return path;
}

string GetFileName(const string &s) {
const auto dpos = s.rfind("/") + 1;
const auto pos = s.rfind(".");
return s.substr(0, pos);
string filename = s.substr(dpos, pos - dpos);

return filename;
}

PHP_FUNCTION(hca2wav)
Expand Down Expand Up @@ -103,47 +122,52 @@ PHP_FUNCTION(acbunpack)
RETURN_FALSE;
}

cgss::CFileStream fileStream(filePath, cgss::FileMode::OpenExisting, cgss::FileAccess::Read);
cgss::CAcbFile acb(&fileStream, filePath);
try {
cgss::CFileStream fileStream(filePath, cgss::FileMode::OpenExisting, cgss::FileAccess::Read);
cgss::CAcbFile acb(&fileStream, filePath);

acb.Initialize();
acb.Initialize();

const string extractDir = "_acb_" + GetFileName(filePath) + "/";
MakeDirectories(extractDir);
const string extractDir = GetFilePath(filePath) + "_acb_" + GetFileName(filePath) + "/";
MakeDirectories(extractDir);

const auto &fileNames = acb.GetFileNames();
const auto &fileNames = acb.GetFileNames();

uint32_t i = 0;
uint32_t i = 0;

for (const auto &fileName : fileNames) {
auto s = fileName;
auto isCueNonEmpty = !s.empty();
for (const auto &fileName : fileNames) {
auto s = fileName;
auto isCueNonEmpty = !s.empty();

if (!isCueNonEmpty) {
s = CAcbFile::GetSymbolicFileNameFromCueId(i);
}
if (!isCueNonEmpty) {
s = CAcbFile::GetSymbolicFileNameFromCueId(i);
}

auto extractPath = extractDir + s;
auto extractPath = extractDir + s;

IStream *stream;
IStream *stream;

if (isCueNonEmpty) {
stream = acb.OpenDataStream(s.c_str());
} else {
stream = acb.OpenDataStream(i);
}
if (isCueNonEmpty) {
stream = acb.OpenDataStream(s.c_str());
} else {
stream = acb.OpenDataStream(i);
}

if (stream) {
cgss::CFileStream fs(extractPath.c_str(), cgss::FileMode::Create, cgss::FileAccess::Write);
CopyStream(stream, &fs);
} else {
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "Cue #%u (%s) cannot be retrieved.", i + 1, s.c_str());
RETURN_FALSE;
}
if (stream) {
cgss::CFileStream fs(extractPath.c_str(), cgss::FileMode::Create, cgss::FileAccess::Write);
CopyStream(stream, &fs);
} else {
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "Cue #%u (%s) cannot be retrieved.", i + 1, s.c_str());
RETURN_FALSE;
}

delete stream;
delete stream;

++i;
++i;
}
} catch (const cgss::CException &ex) {
zend_throw_exception_ex(cgss_exception, ex.GetOpResult() TSRMLS_CC, ex.GetExceptionMessage().c_str());
RETURN_FALSE;
}

RETURN_TRUE;
Expand Down

0 comments on commit 413c21d

Please sign in to comment.