strncasecmp()函数的源代码

时间:2011-09-04 12:13:08

标签: c windows

有人有此功能的来源吗?我认为它在Unix下是可用的,但我使用的是Windows。


当然我先尝试谷歌,但还没找到解决方案,“strncasecmp.c”的功能只需要2个而不是3个参数。

无法提供更多信息,因为它不是我的代码。

这就是他如何使用它:

int _tcsnicmp(const char *c1, const char *c2, int l) { return strncasecmp(c1,c2,l); }

6 个答案:

答案 0 :(得分:7)

#define strncasecmp(x,y,z) _strnicmp(x,y,z)

答案 1 :(得分:5)

编辑在您更新了问题后,看起来您正在使用原始为Windows编写的内容进行反向移植,而不是移植到UNIX,现在又回到Windows?!?。 _tcsnicmp实际上是在Windows上调用的函数(请参阅上面的链接)。在Windows上将它重定向回strncasecmp(或您自己的版本)没有意义。

在Windows下(至少使用Microsoft编译器,此平台不是真正的问题),您可以使用strnicmp family of functions代替。如果您仍然需要google for gocode,就像其他人建议或查看随Visual Studio一起发布并安装在“\ VC \ crt \ src”下的CRT源代码。

答案 2 :(得分:4)

我找到了here

#include <string.h>
#include <ctype.h>

int 
_DEFUN (strncasecmp, (s1, s2, n),
    _CONST char *s1 _AND
    _CONST char *s2 _AND
    size_t n)
{
  if (n == 0)
    return 0;

  while (n-- != 0 && tolower(*s1) == tolower(*s2))
    {
      if (n == 0 || *s1 == '\0' || *s2 == '\0')
    break;
      s1++;
      s2++;
    }

  return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
}

答案 3 :(得分:0)

如果安装了Visual C ++,则CRT源代码位于安装目录中。

查看此目录:( Visual Studio安装路径)\ VC \ crt \ src

查看此目录中的所有* cmp.c文件(例如wcsnicmp.c,strnicmp.c等等)

答案 4 :(得分:0)

基于here中的代码:

def start(server_name):
    if (is_running):
         clean = False
    else:
         clean = _clean_directories(server_name)
         // start the server


def _clean_directories(server_name):
    directory = // create absolute path to directory with `server_name`
    if os.path.isdir(directory):
        shutil.rmtree(directory)
        return True
    else:
        return False

经过测试。比较原始的英特尔版本,我添加了比较相同的指针。

答案 5 :(得分:-2)

这里有一个便携式版本的功能代码:

https://github.com/HarryR/logpipe/blob/51b51b211ee2b961dd096a5f481b3c4d71a0863a/src/portable/strncasecmp.c https://github.com/HarryR/logpipe/blob/51b51b211ee2b961dd096a5f481b3c4d71a0863a/src/portable/strncasecmp.h

出于完整性考虑,我将在此处发布其内容:

strncasecmp.h:

/*
Copyright (c) 2015, Logicista / H Roberts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by the <organization>.
4. Neither the name of the <organization> nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PORTABLE_STRNCASECMP_H_
#define PORTABLE_STRNCASECMP_H_

#include <stddef.h>

int strncasecmp(const char *s1, const char *s2, size_t n);

#endif

strncasecmp.c:

/*
Copyright (c) 2015, Logicista / H Roberts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by the <organization>.
4. Neither the name of the <organization> nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "strncasecmp.h"

#include <ctype.h>

int
strncasecmp(const char *s1, const char *s2, size_t n)
{
    if (n == 0) return 0;

    while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
        if (n == 0 || *s1 == '\0' || *s2 == '\0')
            break;
        s1++;
        s2++;
    }

    return tolower(*(const unsigned char *)s1)
        - tolower(*(const unsigned char *)s2);
}

从此处复制/粘贴,或从该存储库链接下载。然后将文件放入您的项目中,并调整所有其他必须使用#include“ strncasecmp.h”(而不是#include)的C / C ++相关文件。这将引用您拥有的本地文件。

我在Visual Studio 2017项目中使用了这种方法。到目前为止看来工作还不错。如果发现问题,我将调整我的职位。

相关问题