/[thuban]/branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2749 by bramz, Thu Mar 22 19:03:27 2007 UTC revision 2751 by bramz, Wed Mar 28 23:30:15 2007 UTC
# Line 547  static void shapefile_dealloc(ShapeFileO Line 547  static void shapefile_dealloc(ShapeFileO
547   */   */
548  static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds)  static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds)
549  {  {
550          char* file;          char* file = NULL;
551          char* mode = "rb";          char* mode = "rb";
552          static char *kwlist[] = {"name", "mode", NULL};          static char *kwlist[] = {"name", "mode", NULL};
553          if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist,  
554                  Py_FileSystemDefaultEncoding, &file, &mode)) return -1;          SHPClose(self->handle);
555                    self->handle = NULL;
556          self->handle = SHPOpen(file, mode);  
557    #if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
558            if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
559                    PyObject *wfile;
560                    if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:ShapeFile", kwlist, &wfile, &mode))
561                    {
562                            PyObject *wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
563                            if (!wmode) return -1;
564                            self->handle = SHPOpenW(PyUnicode_AS_UNICODE(wfile), PyUnicode_AS_UNICODE(wmode));
565                            Py_DECREF(wmode);
566                            if (!self->handle)
567                            {
568                                    PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
569                                    return -1;
570                            }
571                    }
572                    else
573                    {
574                            /* Drop the argument parsing error as narrow
575                               strings are also valid. */
576                            PyErr_Clear();
577                    }
578            }
579    #endif
580    
581          if (!self->handle)          if (!self->handle)
582          {          {
583                  PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);                  if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:ShapeFile", kwlist,
584                            Py_FileSystemDefaultEncoding, &file, &mode)) return -1;
585                    self->handle = SHPOpen(file, mode);
586    
587                    if (!self->handle)
588                    {
589                            PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
590                            PyMem_Free(file);
591                            return -1;
592                    }
593    
594                    PyMem_Free(file);
595          }          }
596    
597          PyMem_Free(file);          return 0;
         return self->handle ? 0 : -1;  
598  }  }
599    
600    
601    
602  static PyObject* shapefile_close(ShapeFileObject* self)  static PyObject* shapefile_close(ShapeFileObject* self)
603  {  {
604          SHPClose(self->handle);          SHPClose(self->handle);
# Line 570  static PyObject* shapefile_close(ShapeFi Line 606  static PyObject* shapefile_close(ShapeFi
606          Py_RETURN_NONE;          Py_RETURN_NONE;
607  }  }
608    
609    
610    
611  static PyObject* shapefile_info(ShapeFileObject* self)  static PyObject* shapefile_info(ShapeFileObject* self)
612  {  {
613          SHPHandle handle = self->handle;          SHPHandle handle = self->handle;
# Line 579  static PyObject* shapefile_info(ShapeFil Line 617  static PyObject* shapefile_info(ShapeFil
617                          handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]);                          handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]);
618  }  }
619    
620    
621    
622  static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args)  static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args)
623  {  {
624          int index;          int index;
# Line 604  static PyObject* shapefile_read_object(S Line 644  static PyObject* shapefile_read_object(S
644          return (PyObject*) result;          return (PyObject*) result;
645  }  }
646    
647    
648    
649  static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args)  static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args)
650  {  {
651          int index, result;          int index, result;
# Line 680  static PyObject* shapelib_create(PyObjec Line 722  static PyObject* shapelib_create(PyObjec
722          char* file;          char* file;
723          int type;          int type;
724          ShapeFileObject* result;          ShapeFileObject* result;
725                    SHPHandle handle = NULL;
726          if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL;          int wideargument = 0;
727            
728    #if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
729            if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
730                    PyObject *wfile;
731                    if (PyArg_ParseTuple(args, "Ui:create", &wfile, &type))
732                    {
733                            wideargument = 1;
734                            handle = SHPCreateW(PyUnicode_AS_UNICODE(wfile), type);
735                            if (!handle)
736                            {
737                                    PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
738                                    return NULL;
739                            }
740                    }
741                    else
742                    {
743                            /* Drop the argument parsing error as narrow
744                               strings are also valid. */
745                            PyErr_Clear();
746                    }
747            }
748    #endif
749            
750            if (!handle)
751            {
752                    if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL;
753                    handle = SHPCreate(file, type);
754                    if (!handle)
755                    {
756                                    PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
757                                    PyMem_Free(file);
758                                    return NULL;
759                    }
760                    PyMem_Free(file);
761            }
762    
763          result = PyObject_New(ShapeFileObject, &ShapeFileType);          result = PyObject_New(ShapeFileObject, &ShapeFileType);
764          if (!result)          if (!result)
765          {          {
766                  PyMem_Free(file);                  SHPClose(handle);
767                  return PyErr_NoMemory();                  return PyErr_NoMemory();
768          }          }
769                    
770          result->handle = SHPCreate(file, type);          result->handle = handle;
         PyMem_Free(file);  
           
         if (!result->handle)  
         {  
                 PyObject_Del((PyObject*)result);  
                 PyErr_SetString(PyExc_RuntimeError, "Failed to create ShapeFile");  
                 return NULL;  
         }  
           
771          return (PyObject*) result;          return (PyObject*) result;
772  }  }
773                    

Legend:
Removed from v.2749  
changed lines
  Added in v.2751

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26