SqlString.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/SqlString.cpp#2 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 //
00022 // SqlString
00023 //
00024 // An ascii string library that adheres to the SQL99 standard definitions
00025 */
00026 
00027 #include "fennel/common/CommonPreamble.h"
00028 #include "fennel/calculator/SqlString.h"
00029 
00030 FENNEL_BEGIN_NAMESPACE
00031 
00032 int
00033 SqlStrCat(
00034     char* dest,
00035     int destStorageBytes,
00036     int destLenBytes,
00037     char const * const str,
00038     int strLenBytes)
00039 {
00040     if (destLenBytes + strLenBytes > destStorageBytes) {
00041         // SQL99 Part 2 Section 22.1 22-001 "String Data Right truncation"
00042         throw "22001";
00043     }
00044 
00045     memcpy(dest + destLenBytes, str, strLenBytes);
00046     return destLenBytes + strLenBytes;
00047 }
00048 
00049 
00050 int
00051 SqlStrCat(
00052     char* dest,
00053     int destStorageBytes,
00054     char const * const str1,
00055     int str1LenBytes,
00056     char const * const str2,
00057     int str2LenBytes)
00058 {
00059     if (str1LenBytes + str2LenBytes > destStorageBytes) {
00060         // SQL99 Part 2 Section 22.1 22-001
00061         // "String Data Right truncation"
00062         throw "22001";
00063     }
00064 
00065     memcpy(dest, str1, str1LenBytes);
00066     memcpy(dest + str1LenBytes, str2, str2LenBytes);
00067     return str1LenBytes + str2LenBytes;
00068 }
00069 
00070 int
00071 SqlStrCmp_Bin(
00072     char const * const str1,
00073     int str1LenBytes,
00074     char const * const str2,
00075     int str2LenBytes)
00076 {
00077     // First, check for differences in "common" length. If common length
00078     // are contains same values, declare the longer string "larger".
00079     int minLenBytes =
00080         str1LenBytes > str2LenBytes ? str2LenBytes : str1LenBytes;
00081     int memc = memcmp(str1, str2, minLenBytes);
00082     if (memc > 0) {
00083         // Normalize to -1, 0, 1
00084         return 1;
00085     } else if (memc < 0) {
00086         // Normalize to -1, 0, 1
00087         return -1;
00088     } else if (str1LenBytes == str2LenBytes) {
00089         // memc == 0
00090         // Equal length & contain same data -> equal
00091         return 0;
00092     } else if (str1LenBytes > str2LenBytes) {
00093         // Common contains same data, str1 is longer -> str1 > str2
00094         return 1;
00095     } else {
00096         // Common contains same data, str2 is longer -> str2 > str1
00097         return -1;
00098     }
00099 }
00100 
00101 int
00102 SqlStrCpy_Var(
00103     char* dest,
00104     int destStorageBytes,
00105     char const * const str,
00106     int strLenBytes)
00107 {
00108     if (strLenBytes > destStorageBytes) {
00109         // SQL99 Part 2 Section 22.1 22-001
00110         // "String Data Right truncation"
00111         throw "22001";
00112     }
00113     memcpy(dest, str, strLenBytes);
00114     return strLenBytes;
00115 }
00116 
00117 int
00118 SqlStrLenBit(int strLenBytes)
00119 {
00120     return 8 * strLenBytes;
00121 }
00122 
00123 int
00124 SqlStrLenOct(int strLenBytes)
00125 {
00126     return strLenBytes;
00127 }
00128 
00129 
00130 FENNEL_END_NAMESPACE
00131 
00132 // End SqlString.cpp

Generated on Mon Jun 22 04:00:17 2009 for Fennel by  doxygen 1.5.1