Permalink
Browse files

factor utility functions to utils.cpp

  • Loading branch information...
1 parent 32f9128 commit 557b6a73a4aa201cf6305dee0178774bc3420686 @pkrumins committed Aug 15, 2010
Showing with 104 additions and 73 deletions.
  1. +3 −72 src/async_animated_gif.cpp
  2. +88 −0 src/utils.cpp
  3. +12 −0 src/utils.h
  4. +1 −1 wscript
View
@@ -1,12 +1,8 @@
#include <cerrno>
#include <cstdlib>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <dirent.h>
-
#include "common.h"
+#include "utils.h"
#include "gif_encoder.h"
#include "async_animated_gif.h"
@@ -36,22 +32,6 @@ AsyncAnimatedGif::AsyncAnimatedGif(int wwidth, int hheight, buffer_type bbuf_typ
push_id(0), fragment_id(0) {}
int
-file_size(const char *path)
-{
- struct stat moo;
- if (stat(path, &moo) == -1) return -1;
- return moo.st_size;
-}
-
-bool
-is_dir(const char *path)
-{
- struct stat moo;
- if (stat(path, &moo) == -1) return false;
- return S_ISDIR(moo.st_mode);
-}
-
-int
AsyncAnimatedGif::EIO_Push(eio_req *req)
{
push_request *push_req = (push_request *)req->data;
@@ -121,11 +101,13 @@ AsyncAnimatedGif::Push(unsigned char *data_buf, int x, int y, int w, int h)
push_request *push_req = (push_request *)malloc(sizeof(*push_req));
if (!push_req)
throw "malloc in AsyncAnimatedGif::Push failed.";
+
push_req->data = (unsigned char *)malloc(sizeof(*push_req->data)*w*h*3);
if (!push_req->data) {
free(push_req);
throw "malloc in AsyncAnimatedGif::Push failed.";
}
+
memcpy(push_req->data, data_buf, w*h*3);
push_req->push_id = push_id;
push_req->fragment_id = fragment_id++;
@@ -260,57 +242,6 @@ AsyncAnimatedGif::EndPush(const Arguments &args)
return Undefined();
}
-char **
-find_files(const char *path)
-{
- char **files;
- DIR *dp;
- struct dirent *dirp;
-
- if ((dp = opendir(path)) == NULL) {
- return NULL;
- }
- int nfiles = 0;
- while ((dirp = readdir(dp)) != NULL) {
- if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
- continue;
- nfiles++;
- }
- files = (char **)malloc(sizeof(char *) * (nfiles + 1));
- files[nfiles] = NULL;
- int i = 0;
- rewinddir(dp);
- while ((dirp = readdir(dp)) != NULL) {
- if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
- continue;
- int dir_name_len = strlen(dirp->d_name);
- files[i] = (char *)malloc(sizeof(char) * (dir_name_len + 1));
- strcpy(files[i], dirp->d_name);
- i++;
- }
- closedir(dp);
- return files;
-}
-
-void
-free_file_list(char **file_list)
-{
- char **p = file_list;
- while (*p)
- free(*p++);
- free(file_list);
-}
-
-int
-file_list_length(char **file_list)
-{
- char **p = file_list;
- int i = 0;
- while (*p++)
- i++;
- return i;
-}
-
int
fragment_sort(const void *aa, const void *bb)
{
View
@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "utils.h"
+
+char **
+find_files(const char *path)
+{
+ char **files = NULL;
+ DIR *dp;
+ struct dirent *dirp;
+ int i;
+
+ if ((dp = opendir(path)) == NULL) {
+ return NULL;
+ }
+ int nfiles = 0;
+ while ((dirp = readdir(dp)) != NULL) {
+ if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
+ continue;
+ nfiles++;
+ }
+ files = (char **)malloc(sizeof(char *) * (nfiles + 1));
+ if (!files)
+ goto err_1;
+ files[nfiles] = NULL;
+ rewinddir(dp);
+ while ((dirp = readdir(dp)) != NULL) {
+ if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
+ continue;
+ int dir_name_len = strlen(dirp->d_name);
+ files[i] = (char *)malloc(sizeof(char) * (dir_name_len + 1));
+ if (!files[i]) {
+ int j;
+ for (j=0;j<i;j++) free(files[j]);
+ goto err_1;
+ }
+ strcpy(files[i], dirp->d_name);
+ i++;
+ }
+
+ return files;
+
+err_1:
+ closedir(dp);
+ return NULL;
+}
+
+void
+free_file_list(char **file_list)
+{
+ char **p = file_list;
+ while (*p)
+ free(*p++);
+ free(file_list);
+}
+
+int
+file_list_length(char **file_list)
+{
+ char **p = file_list;
+ int i = 0;
+ while (*p++)
+ i++;
+ return i;
+}
+
+int
+file_size(const char *path)
+{
+ struct stat moo;
+ if (stat(path, &moo) == -1) return -1;
+ return moo.st_size;
+}
+
+bool
+is_dir(const char *path)
+{
+ struct stat moo;
+ if (stat(path, &moo) == -1) return false;
+ return S_ISDIR(moo.st_mode);
+}
+
View
@@ -0,0 +1,12 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+char **find_files(const char *path);
+void free_file_list(char **file_list);
+int file_list_length(char **file_list);
+
+int file_size(const char *path);
+bool is_dir(const char *path);
+
+#endif
+
View
@@ -17,7 +17,7 @@ def configure(conf):
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.target = "gif"
- obj.source = "src/common.cpp src/palette.cpp src/quantize.cpp src/gif_encoder.cpp src/gif.cpp src/dynamic_gif_stack.cpp src/animated_gif.cpp src/async_animated_gif.cpp src/module.cpp"
+ obj.source = "src/common.cpp src/utils.cpp src/palette.cpp src/quantize.cpp src/gif_encoder.cpp src/gif.cpp src/dynamic_gif_stack.cpp src/animated_gif.cpp src/async_animated_gif.cpp src/module.cpp"
obj.uselib = "GIF"
obj.cxxflags = ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"]

0 comments on commit 557b6a7

Please sign in to comment.