Skip to content

Commit

Permalink
fixed to create directory for outputfile
Browse files Browse the repository at this point in the history
  • Loading branch information
skondo committed Jan 6, 2017
1 parent 574e9b8 commit 900c1bb
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 26 deletions.
2 changes: 2 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/$ENV{PROJECT_NAME}.di
# Caffe Tools
#
add_executable(caffe2demitasse
create_dirs.cpp
caffe2demitasse.cpp
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/$ENV{PROJECT_NAME}.dir/caffe.pb.cc
network_model_generated.h
Expand Down Expand Up @@ -104,6 +105,7 @@ if (Matlab_FOUND)
link_directories(${Matlab_LIBRARY_DIRS})

add_executable(matconvnet2demitasse
create_dirs.cpp
matconvnet2demitasse.cpp
network_model_generated.h
)
Expand Down
23 changes: 10 additions & 13 deletions tools/caffe2demitasse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include <math.h>

#include <memory>
#include <iostream>
#include <fstream>

#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>

#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
Expand All @@ -49,6 +46,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "flatbuffers/flatbuffers.h"
#include "network_model_generated.h"

#include "create_dirs.h"

using DataInputForm = struct {
int w; // width
int h; // height
Expand Down Expand Up @@ -674,8 +673,12 @@ void write_model_to_file(flatbuffers::FlatBufferBuilder& builder, const std::str

std::ofstream fs(file, std::ios::out | std::ios::binary | std::ios::trunc);

fs.write((const char *)buffer, size);
fs.close();
if (fs.is_open()) {
fs.write((const char *)buffer, size);
fs.close();
} else {
std::cerr << "Failed to open file :" << errno << std::endl;
}

builder.ReleaseBufferPointer();
}
Expand Down Expand Up @@ -803,18 +806,12 @@ void load_caffemodel(const std::string& proto, const std::string& model, const s
demitasse::serialize::FinishModelBuffer(builder, mloc);

// write to the file
std::cerr << "output file:" << outfile << std::endl;
write_model_to_file(builder, outfile);

google::protobuf::ShutdownProtobufLibrary();
}

void create_dirs(const char* path) {
const char *dir_name = dirname((char *)path);

if (strcmp(dir_name, ".") != 0 && strcmp(dir_name, "/") != 0) {
mkdir(dir_name, 0777);
}
}

int main(int argc, char** argv) {

Expand Down
79 changes: 79 additions & 0 deletions tools/create_dirs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (C) 2017 Denso IT Laboratory, Inc.
All Rights Reserved
Denso IT Laboratory, Inc. retains sole and exclusive ownership of all
intellectual property rights including copyrights and patents related to this
Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of the Software and accompanying documentation to use, copy, modify, merge,
publish, or distribute the Software or software derived from it for
non-commercial purposes, such as academic study, education and personal use,
subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#include <libgen.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "create_dirs.h"

int create_dirs(const char* path) {

char *tmp_path = (char *)malloc(sizeof(char) * (strlen(path) + 1));
if (tmp_path == NULL) {
return -1; // mem error
}
strcpy(tmp_path, path);

char *dir_name = dirname(tmp_path);
if (strcmp(dir_name, ".") == 0 || strcmp(dir_name, "/") == 0) {
free(tmp_path);
return 0;
}

for (char *p = dir_name + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
if (mkdir(dir_name, S_IRWXU) != 0) {
if (errno != EEXIST) {
free(tmp_path);
return -1;
}
}
*p = '/';
}
}

if (mkdir(dir_name, S_IRWXU) != 0) {
if (errno != EEXIST) {
free(tmp_path);
return -1;
}
}

free(tmp_path);
return 0;
}
39 changes: 39 additions & 0 deletions tools/create_dirs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright (C) 2017 Denso IT Laboratory, Inc.
All Rights Reserved
Denso IT Laboratory, Inc. retains sole and exclusive ownership of all
intellectual property rights including copyrights and patents related to this
Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of the Software and accompanying documentation to use, copy, modify, merge,
publish, or distribute the Software or software derived from it for
non-commercial purposes, such as academic study, education and personal use,
subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

extern "C" {

int create_dirs(const char* path);

}
22 changes: 9 additions & 13 deletions tools/matconvnet2demitasse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ POSSIBILITY OF SUCH DAMAGE.
#include <math.h>

#include <fstream>

#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>
#include <iostream>

#include "mat.h"
#include "matrix.h"

#include "flatbuffers/flatbuffers.h"
#include "network_model_generated.h"

#include "create_dirs.h"

using DataInputForm = struct {
int w; // width
int h; // height
Expand Down Expand Up @@ -838,8 +837,12 @@ void write_model_to_file(flatbuffers::FlatBufferBuilder& builder, const std::str

std::ofstream fs(file, std::ios::out | std::ios::binary | std::ios::trunc);

fs.write((const char*)buffer, size);
fs.close();
if (fs.is_open()) {
fs.write((const char*)buffer, size);
fs.close();
} else {
std::cerr << "Failed to open file :" << errno << std::endl;
}

builder.ReleaseBufferPointer();
}
Expand Down Expand Up @@ -928,13 +931,6 @@ void load_matconvnet_model(const std::string& model, const std::string& outfile)
write_model_to_file(builder, outfile);
}

void create_dirs(const char* path) {
const char *dir_name = dirname((char *)path);

if (strcmp(dir_name, ".") != 0 && strcmp(dir_name, "/") != 0) {
mkdir(dir_name, 0777);
}
}

int main(int argc, char** argv) {
if (argc < 2) {
Expand Down

0 comments on commit 900c1bb

Please sign in to comment.