Fix snprintf for M$ compilers
[openjpeg.git] / applications / jpip / libopenjpip / target_manager.c
index 56f23a6f1b5f5878554650a3e6bca190fd7752a5..c3b9c0750a7bc050a5d016771baee9510078001c 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#ifdef _WIN32
+#define _snprintf snprintf /* Visual Studio */
+#include <io.h>
+#else
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <unistd.h>
+#endif
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <time.h>
-#include <curl/curl.h>
 #include "target_manager.h"
 
 #ifdef SERVER
+#include <curl/curl.h>
 #include "fcgi_stdio.h"
 #define logstream FCGI_stdout
 #else
 #define FCGI_stdout stdout
 #define FCGI_stderr stderr
 #define logstream stderr
-#endif //SERVER
-
+#endif /*SERVER*/
 
 targetlist_param_t * gene_targetlist()
 {
@@ -98,10 +102,12 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
   snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
   target->targetname = strdup( targetpath); 
   target->fd = fd;
+#ifdef SERVER
   if( tmpfname[0])
     target->tmpfname = strdup( tmpfname);
   else
     target->tmpfname = NULL;
+#endif
   target->csn = last_csn++;
   target->codeidx = jp2idx;
   target->num_of_use = 0; 
@@ -109,9 +115,9 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
   target->jptstream = isJPTfeasible( *jp2idx);
   target->next=NULL;
 
-  if( targetlist->first) // there are one or more entries
+  if( targetlist->first) /* there are one or more entries*/
     targetlist->last->next = target;
-  else                   // first entry
+  else                   /* first entry*/
     targetlist->first = target;
   targetlist->last = target;
 
@@ -137,10 +143,12 @@ void delete_target( target_param_t **target)
 {
   close( (*target)->fd);
 
+#ifdef SERVER
   if( (*target)->tmpfname){
     fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
     remove( (*target)->tmpfname);
   }
+#endif
 
   if( (*target)->codeidx)
     delete_index ( &(*target)->codeidx);
@@ -239,31 +247,17 @@ target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
   return NULL;
 }
 
-static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
+int open_remotefile( char filepath[], char tmpfname[]);
 
 int open_jp2file( char filepath[], char tmpfname[])
 {
   int fd;
   char *data;
-  CURL *curl_handle;
   
-  // download remote target file to local storage
+  /* download remote target file to local storage*/
   if( strncmp( filepath, "http://", 7) == 0){
-    curl_handle = curl_easy_init();
-    curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
-    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
-    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
-    
-    snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
-    fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
-    if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
-      fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
-      curl_easy_cleanup(curl_handle);
+    if( (fd = open_remotefile( filepath, tmpfname)) == -1)
       return -1;
-    }
-    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
-    curl_easy_perform(curl_handle);
-    curl_easy_cleanup(curl_handle);
   }
   else{
     tmpfname[0] = 0;
@@ -272,14 +266,14 @@ int open_jp2file( char filepath[], char tmpfname[])
       return -1;
     }
   }
-  // Check resource is a JP family file.
+  /* Check resource is a JP family file.*/
   if( lseek( fd, 0, SEEK_SET)==-1){
     close(fd);
     fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
     return -1;
   }
   
-  data = (char *)malloc( 12); // size of header
+  data = (char *)malloc( 12); /* size of header*/
 
   if( read( fd, data, 12) != 12){
     free( data);
@@ -301,6 +295,44 @@ int open_jp2file( char filepath[], char tmpfname[])
   return fd;
 }
 
+#ifdef SERVER
+static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
+#endif
+
+int open_remotefile( char filepath[], char tmpfname[])
+{
+#ifndef SERVER
+  (void)filepath;
+  (void)tmpfname;
+  fprintf( FCGI_stderr, "Remote file can not be opened in local mode\n");
+  return -1;
+
+#else
+
+  CURL *curl_handle;
+  int fd;
+    
+  curl_handle = curl_easy_init();
+  curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
+  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
+  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
+  
+  snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
+  fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
+  if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
+    fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
+    curl_easy_cleanup(curl_handle);
+    return -1;
+  }
+  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
+  curl_easy_perform(curl_handle);
+  curl_easy_cleanup(curl_handle);
+
+  return fd;
+#endif /*SERVER*/
+}
+
+#ifdef SERVER
 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
 {
   int *fd = (int *)stream;
@@ -308,3 +340,4 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
 
   return written;
 }
+#endif /*SERVER*/