{"action":"created","comment":{"url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/comments/60683250","id":60683250,"diff_hunk":"@@ -41,6 +42,76 @@ NS_CC_BEGIN\n  * @{\n  */\n \n+\n+class ResizableBuffer {\n+public:\n+    virtual ~ResizableBuffer() {}\n+    virtual void resize(size_t size) = 0;\n+    virtual void* buffer() const = 0;\n+};\n+\n+template<typename T>\n+class ResizableBufferAdapter { };\n+\n+\n+template<typename C, typename T, typename A>\n+class ResizableBufferAdapter< std::basic_string<C, T, A> > : public ResizableBuffer {\n+    typedef std::basic_string<C, T, A> BufferType;\n+    BufferType* buffer_;\n+public:\n+    ResizableBufferAdapter(BufferType* buffer) : buffer_(buffer) {}\n+    virtual void resize(size_t size) override {\n+        buffer_->resize((size + sizeof(C)) / sizeof(C));\n+    }\n+    virtual void* buffer() const override {\n+        return &buffer_->front();\n+    }\n+};\n+\n+template<typename T, typename A>\n+class ResizableBufferAdapter< std::vector<T, A> > : public ResizableBuffer {\n+    typedef std::vector<T, A> BufferType;\n+    BufferType* buffer_;\n+public:\n+    ResizableBufferAdapter(BufferType* buffer) : buffer_(buffer) {}\n+    virtual void resize(size_t size) override {\n+        buffer_->resize((size + sizeof(T)) / sizeof(T));\n+    }\n+    virtual void* buffer() const override {\n+        return &buffer_->front();\n+    }\n+};\n+\n+\n+template<>\n+class ResizableBufferAdapter<Data> : public ResizableBuffer {\n+    typedef Data BufferType;\n+    BufferType* buffer_;\n+public:\n+    ResizableBufferAdapter(BufferType* buffer) : buffer_(buffer) {}\n+    virtual void resize(size_t size) override {\n+        if (buffer_->getSize() < size) {\n+            auto old = buffer_->getBytes();\n+            void* buffer = realloc(old, size);\n+            if (buffer)\n+                buffer_->fastSet((unsigned char*)buffer, size);\n+        }\n+    }\n+    virtual void* buffer() const override {\n+        return buffer_->getBytes();\n+    }\n+};\n+\n+enum class FileError\n+{\n+    OK = 0,\n+    NotExists = 0x01, /** File not exists */\n+    OpenFailed = 0x02, /** Open file failed. */\n+    ReadFaild = 0x03, /** Read failed */\n+    NotInitialized = 0x04, /** FileUtils is not initializes */\n+};","path":"cocos/platform/CCFileUtils.h","position":79,"original_position":79,"commit_id":"d17a636286fc97d5a9ada3275ef311d2fc55668a","original_commit_id":"d17a636286fc97d5a9ada3275ef311d2fc55668a","user":{"login":"minggo","id":488203,"avatar_url":"https://avatars.githubusercontent.com/u/488203?v=3","gravatar_id":"","url":"https://api.github.com/users/minggo","html_url":"https://github.com/minggo","followers_url":"https://api.github.com/users/minggo/followers","following_url":"https://api.github.com/users/minggo/following{/other_user}","gists_url":"https://api.github.com/users/minggo/gists{/gist_id}","starred_url":"https://api.github.com/users/minggo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minggo/subscriptions","organizations_url":"https://api.github.com/users/minggo/orgs","repos_url":"https://api.github.com/users/minggo/repos","events_url":"https://api.github.com/users/minggo/events{/privacy}","received_events_url":"https://api.github.com/users/minggo/received_events","type":"User","site_admin":false},"body":"How about move it into FileUtils?","created_at":"2016-04-22T02:12:34Z","updated_at":"2016-04-22T02:12:34Z","html_url":"https://github.com/cocos2d/cocos2d-x/pull/15479#discussion_r60683250","pull_request_url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479","_links":{"self":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/comments/60683250"},"html":{"href":"https://github.com/cocos2d/cocos2d-x/pull/15479#discussion_r60683250"},"pull_request":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479"}}},"pull_request":{"url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479","id":67299423,"html_url":"https://github.com/cocos2d/cocos2d-x/pull/15479","diff_url":"https://github.com/cocos2d/cocos2d-x/pull/15479.diff","patch_url":"https://github.com/cocos2d/cocos2d-x/pull/15479.patch","issue_url":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/15479","number":15479,"state":"open","locked":false,"title":"Add FileUtils::getContents().","user":{"login":"xpol","id":20248,"avatar_url":"https://avatars.githubusercontent.com/u/20248?v=3","gravatar_id":"","url":"https://api.github.com/users/xpol","html_url":"https://github.com/xpol","followers_url":"https://api.github.com/users/xpol/followers","following_url":"https://api.github.com/users/xpol/following{/other_user}","gists_url":"https://api.github.com/users/xpol/gists{/gist_id}","starred_url":"https://api.github.com/users/xpol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xpol/subscriptions","organizations_url":"https://api.github.com/users/xpol/orgs","repos_url":"https://api.github.com/users/xpol/repos","events_url":"https://api.github.com/users/xpol/events{/privacy}","received_events_url":"https://api.github.com/users/xpol/received_events","type":"User","site_admin":false},"body":"Why another file read API?\r\n\r\nCurrent APIs has some problem:\r\n\r\n1. `FileUtils::getDataFormFile()` can't return string, convert from Data to string will make extra copy.\r\n2. `FileUtils::getStringFormFile()` can't read binary file, and it truncate contents.\r\n3. `FileUtils::getDataFormFile()` and `FileUtils::getStringFormFile()` does not tell different from empty file and io errors.\r\n\r\nAdvantage of `FileUtils::getContents()`\r\n\r\n1. Support read into `cocos2d::Data` `std::basic_string` and `std::vector`\r\n2. It has abstract of the output buffer, custom type of buffer object can be adapted by writing subclass `ResizableBuffer`.\r\n3. It returns error code, so that we can know the different form empty file and io error.\r\n","created_at":"2016-04-21T05:37:03Z","updated_at":"2016-04-22T02:12:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"82afe2a9525104fdd5d399ce3eef74c259261ed8","assignee":{"login":"minggo","id":488203,"avatar_url":"https://avatars.githubusercontent.com/u/488203?v=3","gravatar_id":"","url":"https://api.github.com/users/minggo","html_url":"https://github.com/minggo","followers_url":"https://api.github.com/users/minggo/followers","following_url":"https://api.github.com/users/minggo/following{/other_user}","gists_url":"https://api.github.com/users/minggo/gists{/gist_id}","starred_url":"https://api.github.com/users/minggo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minggo/subscriptions","organizations_url":"https://api.github.com/users/minggo/orgs","repos_url":"https://api.github.com/users/minggo/repos","events_url":"https://api.github.com/users/minggo/events{/privacy}","received_events_url":"https://api.github.com/users/minggo/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479/commits","review_comments_url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479/comments","review_comment_url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/15479/comments","statuses_url":"https://api.github.com/repos/cocos2d/cocos2d-x/statuses/d17a636286fc97d5a9ada3275ef311d2fc55668a","head":{"label":"xpol:file-utils-get-contents","ref":"file-utils-get-contents","sha":"d17a636286fc97d5a9ada3275ef311d2fc55668a","user":{"login":"xpol","id":20248,"avatar_url":"https://avatars.githubusercontent.com/u/20248?v=3","gravatar_id":"","url":"https://api.github.com/users/xpol","html_url":"https://github.com/xpol","followers_url":"https://api.github.com/users/xpol/followers","following_url":"https://api.github.com/users/xpol/following{/other_user}","gists_url":"https://api.github.com/users/xpol/gists{/gist_id}","starred_url":"https://api.github.com/users/xpol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xpol/subscriptions","organizations_url":"https://api.github.com/users/xpol/orgs","repos_url":"https://api.github.com/users/xpol/repos","events_url":"https://api.github.com/users/xpol/events{/privacy}","received_events_url":"https://api.github.com/users/xpol/received_events","type":"User","site_admin":false},"repo":{"id":46400139,"name":"cocos2d-x","full_name":"xpol/cocos2d-x","owner":{"login":"xpol","id":20248,"avatar_url":"https://avatars.githubusercontent.com/u/20248?v=3","gravatar_id":"","url":"https://api.github.com/users/xpol","html_url":"https://github.com/xpol","followers_url":"https://api.github.com/users/xpol/followers","following_url":"https://api.github.com/users/xpol/following{/other_user}","gists_url":"https://api.github.com/users/xpol/gists{/gist_id}","starred_url":"https://api.github.com/users/xpol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xpol/subscriptions","organizations_url":"https://api.github.com/users/xpol/orgs","repos_url":"https://api.github.com/users/xpol/repos","events_url":"https://api.github.com/users/xpol/events{/privacy}","received_events_url":"https://api.github.com/users/xpol/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/xpol/cocos2d-x","description":"cocos2d-x for C++","fork":true,"url":"https://api.github.com/repos/xpol/cocos2d-x","forks_url":"https://api.github.com/repos/xpol/cocos2d-x/forks","keys_url":"https://api.github.com/repos/xpol/cocos2d-x/keys{/key_id}","collaborators_url":"https://api.github.com/repos/xpol/cocos2d-x/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/xpol/cocos2d-x/teams","hooks_url":"https://api.github.com/repos/xpol/cocos2d-x/hooks","issue_events_url":"https://api.github.com/repos/xpol/cocos2d-x/issues/events{/number}","events_url":"https://api.github.com/repos/xpol/cocos2d-x/events","assignees_url":"https://api.github.com/repos/xpol/cocos2d-x/assignees{/user}","branches_url":"https://api.github.com/repos/xpol/cocos2d-x/branches{/branch}","tags_url":"https://api.github.com/repos/xpol/cocos2d-x/tags","blobs_url":"https://api.github.com/repos/xpol/cocos2d-x/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/xpol/cocos2d-x/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/xpol/cocos2d-x/git/refs{/sha}","trees_url":"https://api.github.com/repos/xpol/cocos2d-x/git/trees{/sha}","statuses_url":"https://api.github.com/repos/xpol/cocos2d-x/statuses/{sha}","languages_url":"https://api.github.com/repos/xpol/cocos2d-x/languages","stargazers_url":"https://api.github.com/repos/xpol/cocos2d-x/stargazers","contributors_url":"https://api.github.com/repos/xpol/cocos2d-x/contributors","subscribers_url":"https://api.github.com/repos/xpol/cocos2d-x/subscribers","subscription_url":"https://api.github.com/repos/xpol/cocos2d-x/subscription","commits_url":"https://api.github.com/repos/xpol/cocos2d-x/commits{/sha}","git_commits_url":"https://api.github.com/repos/xpol/cocos2d-x/git/commits{/sha}","comments_url":"https://api.github.com/repos/xpol/cocos2d-x/comments{/number}","issue_comment_url":"https://api.github.com/repos/xpol/cocos2d-x/issues/comments{/number}","contents_url":"https://api.github.com/repos/xpol/cocos2d-x/contents/{+path}","compare_url":"https://api.github.com/repos/xpol/cocos2d-x/compare/{base}...{head}","merges_url":"https://api.github.com/repos/xpol/cocos2d-x/merges","archive_url":"https://api.github.com/repos/xpol/cocos2d-x/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/xpol/cocos2d-x/downloads","issues_url":"https://api.github.com/repos/xpol/cocos2d-x/issues{/number}","pulls_url":"https://api.github.com/repos/xpol/cocos2d-x/pulls{/number}","milestones_url":"https://api.github.com/repos/xpol/cocos2d-x/milestones{/number}","notifications_url":"https://api.github.com/repos/xpol/cocos2d-x/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/xpol/cocos2d-x/labels{/name}","releases_url":"https://api.github.com/repos/xpol/cocos2d-x/releases{/id}","deployments_url":"https://api.github.com/repos/xpol/cocos2d-x/deployments","created_at":"2015-11-18T06:34:10Z","updated_at":"2016-04-16T09:09:38Z","pushed_at":"2016-04-21T05:55:01Z","git_url":"git://github.com/xpol/cocos2d-x.git","ssh_url":"git@github.com:xpol/cocos2d-x.git","clone_url":"https://github.com/xpol/cocos2d-x.git","svn_url":"https://github.com/xpol/cocos2d-x","homepage":"http://www.cocos2d-x.org","size":899703,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"v3"}},"base":{"label":"cocos2d:v3","ref":"v3","sha":"9514b51057869e93b3e26f75563f9a2cd8b731a8","user":{"login":"cocos2d","id":234324,"avatar_url":"https://avatars.githubusercontent.com/u/234324?v=3","gravatar_id":"","url":"https://api.github.com/users/cocos2d","html_url":"https://github.com/cocos2d","followers_url":"https://api.github.com/users/cocos2d/followers","following_url":"https://api.github.com/users/cocos2d/following{/other_user}","gists_url":"https://api.github.com/users/cocos2d/gists{/gist_id}","starred_url":"https://api.github.com/users/cocos2d/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cocos2d/subscriptions","organizations_url":"https://api.github.com/users/cocos2d/orgs","repos_url":"https://api.github.com/users/cocos2d/repos","events_url":"https://api.github.com/users/cocos2d/events{/privacy}","received_events_url":"https://api.github.com/users/cocos2d/received_events","type":"Organization","site_admin":false},"repo":{"id":1093228,"name":"cocos2d-x","full_name":"cocos2d/cocos2d-x","owner":{"login":"cocos2d","id":234324,"avatar_url":"https://avatars.githubusercontent.com/u/234324?v=3","gravatar_id":"","url":"https://api.github.com/users/cocos2d","html_url":"https://github.com/cocos2d","followers_url":"https://api.github.com/users/cocos2d/followers","following_url":"https://api.github.com/users/cocos2d/following{/other_user}","gists_url":"https://api.github.com/users/cocos2d/gists{/gist_id}","starred_url":"https://api.github.com/users/cocos2d/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cocos2d/subscriptions","organizations_url":"https://api.github.com/users/cocos2d/orgs","repos_url":"https://api.github.com/users/cocos2d/repos","events_url":"https://api.github.com/users/cocos2d/events{/privacy}","received_events_url":"https://api.github.com/users/cocos2d/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cocos2d/cocos2d-x","description":"cocos2d-x for C++","fork":false,"url":"https://api.github.com/repos/cocos2d/cocos2d-x","forks_url":"https://api.github.com/repos/cocos2d/cocos2d-x/forks","keys_url":"https://api.github.com/repos/cocos2d/cocos2d-x/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cocos2d/cocos2d-x/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cocos2d/cocos2d-x/teams","hooks_url":"https://api.github.com/repos/cocos2d/cocos2d-x/hooks","issue_events_url":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/events{/number}","events_url":"https://api.github.com/repos/cocos2d/cocos2d-x/events","assignees_url":"https://api.github.com/repos/cocos2d/cocos2d-x/assignees{/user}","branches_url":"https://api.github.com/repos/cocos2d/cocos2d-x/branches{/branch}","tags_url":"https://api.github.com/repos/cocos2d/cocos2d-x/tags","blobs_url":"https://api.github.com/repos/cocos2d/cocos2d-x/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cocos2d/cocos2d-x/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cocos2d/cocos2d-x/git/refs{/sha}","trees_url":"https://api.github.com/repos/cocos2d/cocos2d-x/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cocos2d/cocos2d-x/statuses/{sha}","languages_url":"https://api.github.com/repos/cocos2d/cocos2d-x/languages","stargazers_url":"https://api.github.com/repos/cocos2d/cocos2d-x/stargazers","contributors_url":"https://api.github.com/repos/cocos2d/cocos2d-x/contributors","subscribers_url":"https://api.github.com/repos/cocos2d/cocos2d-x/subscribers","subscription_url":"https://api.github.com/repos/cocos2d/cocos2d-x/subscription","commits_url":"https://api.github.com/repos/cocos2d/cocos2d-x/commits{/sha}","git_commits_url":"https://api.github.com/repos/cocos2d/cocos2d-x/git/commits{/sha}","comments_url":"https://api.github.com/repos/cocos2d/cocos2d-x/comments{/number}","issue_comment_url":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/comments{/number}","contents_url":"https://api.github.com/repos/cocos2d/cocos2d-x/contents/{+path}","compare_url":"https://api.github.com/repos/cocos2d/cocos2d-x/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cocos2d/cocos2d-x/merges","archive_url":"https://api.github.com/repos/cocos2d/cocos2d-x/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cocos2d/cocos2d-x/downloads","issues_url":"https://api.github.com/repos/cocos2d/cocos2d-x/issues{/number}","pulls_url":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls{/number}","milestones_url":"https://api.github.com/repos/cocos2d/cocos2d-x/milestones{/number}","notifications_url":"https://api.github.com/repos/cocos2d/cocos2d-x/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cocos2d/cocos2d-x/labels{/name}","releases_url":"https://api.github.com/repos/cocos2d/cocos2d-x/releases{/id}","deployments_url":"https://api.github.com/repos/cocos2d/cocos2d-x/deployments","created_at":"2010-11-18T23:17:00Z","updated_at":"2016-04-21T22:34:22Z","pushed_at":"2016-04-21T14:24:56Z","git_url":"git://github.com/cocos2d/cocos2d-x.git","ssh_url":"git@github.com:cocos2d/cocos2d-x.git","clone_url":"https://github.com/cocos2d/cocos2d-x.git","svn_url":"https://github.com/cocos2d/cocos2d-x","homepage":"http://www.cocos2d-x.org","size":1424334,"stargazers_count":7899,"watchers_count":7899,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5336,"mirror_url":null,"open_issues_count":1109,"forks":5336,"open_issues":1109,"watchers":7899,"default_branch":"v3"}},"_links":{"self":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479"},"html":{"href":"https://github.com/cocos2d/cocos2d-x/pull/15479"},"issue":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/15479"},"comments":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/issues/15479/comments"},"review_comments":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479/comments"},"review_comment":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/pulls/15479/commits"},"statuses":{"href":"https://api.github.com/repos/cocos2d/cocos2d-x/statuses/d17a636286fc97d5a9ada3275ef311d2fc55668a"}}}}